SlideShare a Scribd company logo
Unit-IV
Operator Overloading and Type Conversions
INTRODUCTION
Operator overloading is one of the many exciting features of C++ language. C++ permits us
to add two variables of user-defined types with the same syntax that is applied to the basic types.
This means that C++ has the ability to provide the operators with a special meaning for a data type.
The mechanism of giving such special meanings to an operator is known as operator overloading.
Operator overloading provides a flexible option for the creation of new definitions for most of the
C++ operators. We can overload all the C++ operators except
• Class member access operators
• Scope resolution operator
• Size operator
• Conditional operator
DEFINING OPERATOR OVERLOADING
To define an additional task to an operator, we must specify what it means in relation to the
class to which the operator is applied. This is done with the help of a special function, called
operator function, which describes the task. The general form of an operator function is
return type classname :: operator (op-arglist)
{
Function body
}
Where return type is the type of value returned by the specified operation and op is the operator
being overloaded. The op is preceded by the keyword operator. operator op is the function name.
Operator functions must be either member functions (non-static) or friend function.
OVERLOADING UNARY AND BINARY OPERATORS
Let us consider the unary minus operator. A minus operator when used as a unary, takes just one
opearand. We know that this operator changes the sign of an operand when applied to a basic data
item. We will see here how to overload this operator so that it can be applied to an object in much
the same way as is applied to an int or float variable. The unary minus when applied to an object
should change the sign of each of its data items. The same mechanism can be used to overload a
binary operator. The complex statement
C = sum(A, B);
Was previously used to add two complex numbers using a friend function. The above functional
notation can be replaced by a natural expression
C = A + B;
by overloading the + operator using an operator +( ) function.
OVERLOADING BINARY OPERATORS USING FRIENDS
friend functions may be used in the place of member functions for overloading a binary
operator, the only difference being that a friend function requires two arguments to be explicitily
passed to it, while amember function requires only one.
RULES FOR OVERLOADING OPERATORS
There are certain rstrictions and limitations in overloading the operators. Some of them are
listed below.
1. Only exsisting operators can be overloaded. New operators cannot be created.
2. The overloaded operator must have at least one operand that is of user defined type.
3. We cannot change the basic meaning of an operator.
4. Overloaded operators follow the syntax rules of the original operators. They cannot be
overridden.
5. There are some operators that cannot be overloaded.
6. We cannot use friend functions to overload certain operators. However member functions
can be used to overload them.
TYPE CONVERSIONS
We know that when constants and variables of different types are mixed in an expression,
C applies automatic type conversion to the operands as per certain rules. Similarly, an assignment
operation also causes the automatic type conversion. The type of data to the right of an assignment
operator is automatically converted to the type of vasriable on the left.
The three types of situations in the data conversion are
1. Conversion from basic type to class type.
2. Conversion from class type to basic type.
3. Conversion from one class type to another class type.
DATA CONVERSION EXAMPLE
#include <iostream.h>
#include <conio.h>
class invent1
{
int code;
int items;
float price;
public:
invent1()
{}
invent1(int a,int b,int c)
{
code=a;
items=b;
price=c;
}
void display()
{
cout<<"nCode : "<<code;
cout<<"nItems : "<<items;
cout<<"nPrice : "<<price;
}
int getcode()
{return code;}
int getitem()
{return items;}
int getprice()
{return price;}
};
class invent2
{
int code;
floatvalue;
public:
invent2()
{
code=0;
value=0;
}
invent2(int x,float y)
{
code=x;
value=y;
}
void display()
{
cout<<"Code : "<<code<<endl;
cout<<"Value : "<<value<<endl;
}
invent2(invent1 p)
{
code=p.getcode();
value=p.getitem()*p.getprice();
}
};
void main()
{
clrscr();
invent1 s1(100,5,140);
invent2 d1;
d1=s1; //Invoke Constructor in Invent2 for conversion
cout<<"nProduct details - Invent1 type";
s1.display();
cout<<"nnnProduct details - Invent2 typen";
d1.display();
getch();
}
The compiler does not support automatic type conversions for the user defined data types.
We can use casting operator functions to achieve this. The casting operator function should satisfy
the following conditions.
1. It must be a class member.
2. It must not specify a return type.
3. It must not have any arguments.

More Related Content

Similar to NIKUL SURANI

OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++
Aabha Tiwari
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingKumar
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Pranali Chaudhari
 
Operator overloading (binary)
Operator overloading (binary)Operator overloading (binary)
Operator overloading (binary)
Tirthika Bandi
 
Ch-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdfCh-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdf
esuEthopi
 
overloading in C++
overloading in C++overloading in C++
overloading in C++
Prof Ansari
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
Juginder Pal Singh
 
Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloadingPrincess Sam
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.pptTareq Hasan
 
08 c-operator-overloadingppt2563
08 c-operator-overloadingppt256308 c-operator-overloadingppt2563
08 c-operator-overloadingppt2563Youth For Peace
 
OOP_UnitIII.pdf
OOP_UnitIII.pdfOOP_UnitIII.pdf
OOP_UnitIII.pdf
SuyogSabale1
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Burhan Ahmed
 
Operator oveerloading
Operator oveerloadingOperator oveerloading
Operator oveerloadingyatinnarula
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
ArunaDevi63
 
Operator overloading and type conversion in cpp
Operator overloading and type conversion in cppOperator overloading and type conversion in cpp
Operator overloading and type conversion in cpp
rajshreemuthiah
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
murugeswariSenthilku
 
Operator overloading C++
Operator overloading C++Operator overloading C++
Operator overloading C++
Lahiru Dilshan
 
C operators
C operatorsC operators
C operators
GPERI
 

Similar to NIKUL SURANI (20)

Oops
OopsOops
Oops
 
OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Operator overloading (binary)
Operator overloading (binary)Operator overloading (binary)
Operator overloading (binary)
 
Ch-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdfCh-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdf
 
overloading in C++
overloading in C++overloading in C++
overloading in C++
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloading
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
 
08 c-operator-overloadingppt2563
08 c-operator-overloadingppt256308 c-operator-overloadingppt2563
08 c-operator-overloadingppt2563
 
OOP_UnitIII.pdf
OOP_UnitIII.pdfOOP_UnitIII.pdf
OOP_UnitIII.pdf
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Operator oveerloading
Operator oveerloadingOperator oveerloading
Operator oveerloading
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Operator overloading and type conversion in cpp
Operator overloading and type conversion in cppOperator overloading and type conversion in cpp
Operator overloading and type conversion in cpp
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Operator overloading C++
Operator overloading C++Operator overloading C++
Operator overloading C++
 
C_plus_plus
C_plus_plusC_plus_plus
C_plus_plus
 
C operators
C operatorsC operators
C operators
 

Recently uploaded

Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 

Recently uploaded (20)

Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 

NIKUL SURANI

  • 1. Unit-IV Operator Overloading and Type Conversions INTRODUCTION Operator overloading is one of the many exciting features of C++ language. C++ permits us to add two variables of user-defined types with the same syntax that is applied to the basic types. This means that C++ has the ability to provide the operators with a special meaning for a data type. The mechanism of giving such special meanings to an operator is known as operator overloading. Operator overloading provides a flexible option for the creation of new definitions for most of the C++ operators. We can overload all the C++ operators except • Class member access operators • Scope resolution operator • Size operator • Conditional operator DEFINING OPERATOR OVERLOADING To define an additional task to an operator, we must specify what it means in relation to the class to which the operator is applied. This is done with the help of a special function, called operator function, which describes the task. The general form of an operator function is return type classname :: operator (op-arglist) { Function body } Where return type is the type of value returned by the specified operation and op is the operator being overloaded. The op is preceded by the keyword operator. operator op is the function name. Operator functions must be either member functions (non-static) or friend function. OVERLOADING UNARY AND BINARY OPERATORS Let us consider the unary minus operator. A minus operator when used as a unary, takes just one opearand. We know that this operator changes the sign of an operand when applied to a basic data item. We will see here how to overload this operator so that it can be applied to an object in much the same way as is applied to an int or float variable. The unary minus when applied to an object should change the sign of each of its data items. The same mechanism can be used to overload a binary operator. The complex statement C = sum(A, B);
  • 2. Was previously used to add two complex numbers using a friend function. The above functional notation can be replaced by a natural expression C = A + B; by overloading the + operator using an operator +( ) function. OVERLOADING BINARY OPERATORS USING FRIENDS friend functions may be used in the place of member functions for overloading a binary operator, the only difference being that a friend function requires two arguments to be explicitily passed to it, while amember function requires only one. RULES FOR OVERLOADING OPERATORS There are certain rstrictions and limitations in overloading the operators. Some of them are listed below. 1. Only exsisting operators can be overloaded. New operators cannot be created. 2. The overloaded operator must have at least one operand that is of user defined type. 3. We cannot change the basic meaning of an operator. 4. Overloaded operators follow the syntax rules of the original operators. They cannot be overridden. 5. There are some operators that cannot be overloaded. 6. We cannot use friend functions to overload certain operators. However member functions can be used to overload them. TYPE CONVERSIONS We know that when constants and variables of different types are mixed in an expression, C applies automatic type conversion to the operands as per certain rules. Similarly, an assignment operation also causes the automatic type conversion. The type of data to the right of an assignment operator is automatically converted to the type of vasriable on the left. The three types of situations in the data conversion are 1. Conversion from basic type to class type. 2. Conversion from class type to basic type. 3. Conversion from one class type to another class type. DATA CONVERSION EXAMPLE #include <iostream.h> #include <conio.h> class invent1 { int code; int items;
  • 3. float price; public: invent1() {} invent1(int a,int b,int c) { code=a; items=b; price=c; } void display() { cout<<"nCode : "<<code; cout<<"nItems : "<<items; cout<<"nPrice : "<<price; } int getcode() {return code;} int getitem() {return items;} int getprice() {return price;} }; class invent2 { int code; floatvalue; public: invent2() { code=0; value=0; } invent2(int x,float y) { code=x; value=y; } void display() { cout<<"Code : "<<code<<endl; cout<<"Value : "<<value<<endl; } invent2(invent1 p) { code=p.getcode(); value=p.getitem()*p.getprice(); }
  • 4. }; void main() { clrscr(); invent1 s1(100,5,140); invent2 d1; d1=s1; //Invoke Constructor in Invent2 for conversion cout<<"nProduct details - Invent1 type"; s1.display(); cout<<"nnnProduct details - Invent2 typen"; d1.display(); getch(); } The compiler does not support automatic type conversions for the user defined data types. We can use casting operator functions to achieve this. The casting operator function should satisfy the following conditions. 1. It must be a class member. 2. It must not specify a return type. 3. It must not have any arguments.