SlideShare a Scribd company logo
OPERATOR OVERLOADING &
TYPE CONVERSION
Name : Yaksh Jethva
Contents
● 7.1 Defining operator Overloading
● 7.2 Overloading Unary Operator
● 7.3 Overloading Binary Operator
● 7.4 Overloading Operator Using Friend function
● 7.6 Type Conversion
Introduction
● C++ has ability to provide the operators with a special meanings for a data type.
● The mechanism of giving such special meaning to an operator is known as operator overloading.
Why Operator Overloading?
● Readable code
● Make operators sensitive to context
General Format for operator overloading
returnType operator operator_symbol(parameters);
➔ Return type may be whatever the operator returns
➔ Including a reference to the object of the operand
➔ Operator symbol may be any overloadable operator from the list.
Restrictions on Operator Overloading
● C++ operators that can be overloaded.
● C++ Operators that cannot be overloaded Operators that cannot be overloaded .
Overloading Unary Operator
#include <iostream.h>
class temp
{ Output Count: 6
private:
int count;
Public:
temp():count(5) { }
void operator ++()
{ count=count+1;}
void Display()
{ cout<<"Count: "<<count; }
};
int main()
{
temp t;
++t; /* operator function void operator ++() is called */
t.Display();
return 0;
}
Note
Operator overloading cannot be used to change the way operator works on built-in types. Operator
overloading only allows to redefine the meaning of operator for user-defined types.
Only existing operators can be overloaded. New operators cannot be created.
The overloaded operator must have at least one operand that is of user-defined type.
We cannot change the basic meaning of an operator.
Overloaded operators follow the syntax rules of the original operators.
Overloading Binary Operator
#include<iostream.h>
#include<conio.h>
class complex
{
int a,b;
public:
void getvalue() void display()
{ { cout<<a<<"+"<<b<<"i" <<"n"; }
cout<<"Enter the value of Complex Numbers a,b:"; };
cin>>a>>b;
}
complex operator+(complex ob)
{
complex t;
t.a=ob.a +a;
t.b=ob.b+b;
return(t);
}
complex operator-(complex ob)
{
complex t;
t.a=ob.a - a;
t.b=ob.b -b;
return(t);
}
Binary operator
void main()
{
clrscr();
complex obj1,obj2,result,result1;
obj1.getvalue();
obj2.getvalue();
result = obj1+obj2;
result1=obj1-obj2;
cout<<"Input Values:n";
obj1.display();
obj2.display();
cout<<"Result:";
result.display();
result1.display();
getch();
}
Overloading operators using Friend
● Friend function using operator overloading offers better flexibility to the class.
● These functions are not a members of the class and they do not have 'this' pointer.
● When you overload a unary operator you have to pass one argument.
● When you overload a binary operator you have to pass two arguments.
● Friend function can access private members of a class directly.
● friend return-type operator operator-symbol (Variable 1, Varibale2)
{
//Statements;
}
#include<iostream>
class UnaryFriend
{
int a=10;
int b=20;
int c=30;
public:
void getvalues()
{cout<<"Values of A, B & Cn";
cout<<a<<"n"<<b<<"n"<<c<<"n
"<<endl;
}
void show()
{cout<<a<<"n"<<b<<"n"<<c<<"
n"<<endl;
}
void friend operator-(UnaryFriend &x);
};
void operator-(UnaryFriend &x)
{
x.a = -x.a;
x.b = -x.b;
x.c = -x.c;
}
int main()
{
UnaryFriend x1;
x1.getvalues();
cout<<"Before Overloadingn";
x1.show();
cout<<"After Overloading n";
-x1;
x1.show();
return 0;
}
class time
{
Private:
int hours,minutes;
Public:
time( )
{
hours=0;
minutes=0;
}
time(int x,int y)
{
hours=x;
minutes=y;
}
void display( )
{
cout<<endl<<hours<<" hours and
"<<minutes<<" minutes.";
}
friend time operator + (time, time);
};
time operator + (time y, time z)
{
int h = y.hours + z.hours;
int m = y.minutes + z.minutes;
while (m>=60)
{
m = m-60;
h = h+1;
}
return time(h,m);
}
int main( )
{
time t1(2,40);
time t2(3,30);
time t3;
t3 = t1+t2;
t3.display( );
return 0;
}
class time
{
Private:
int hours,minutes;
Public:
time( )
{
hours=0;
minutes=0;
}
time(int x,int y)
{
hours=x;
minutes=y;
}
void display( )
{
cout<<endl<<hours<<" hours and
"<<minutes<<" minutes.";
}
friend time operator + (time, time);
};
time operator + (time y, time z)
{
int h = y.hours + z.hours;
int m = y.minutes + z.minutes;
while (m>=60)
{
m = m-60;
h = h+1;
}
return time(h,m);
}
int main( )
{
time t1(2,40);
time t2(3,30);
time t3;
t3 = t1+t2;
t3.display( );
return 0;
}
Type Conversions
● the type conversions are automatic only when the data types involved are built-in types.
● int m; float x = 3.14159; m = x;// convert x to integer before its value is assigned // to m.
● For user defined data types, the compiler does not support automatic type conversions.
● Different situations of data conversion between incompatible types.
● Conversion from basic type to class type.
● Conversion from class type to basic type.
● Conversion from one class type to another class type.
Basic to Class Type
A constructor to build a string type object from a char * type variable.
string : : string(char *a)
{
length = strlen(a);
P = new char[length+1];
strcpy(P,a);
}
The variables length and p are data members of the class string.
Class To Basic Type
● A constructor function do not support type conversion from a class type to a basic type.
● An overloaded casting operator is used to convert a class type data to a basic type. It is also
referred to as conversion function.
● operator typename( )
{ … ( function statements ) … }
● This function converts a class type data to typename.
vector : : operator double( )
{
double sum = 0;
for (int i=0; i < size ; i++)
sum = sum + v[i] * v[i];
return sqrt (sum);
}
● This function converts a vector to the square root of the sum of squares of its components.
Continue...
The casting operator function should satisfy the following conditions:
● It must be a class member.
● It must not specify a return type.
● It must not have any arguments.
One Class To Another Class Type
● Type objX = objY ; // objects of different types objX is an object of class X and objY is an object of
class Y.
● The class Y type data is converted to the class X type data and the converted value is assigned to
the objX.
● Conversion is takes place from class Y to class X.
● Y is known as source class.
● X is known as destination class.
Thank you!!!

More Related Content

PPTX
Operator overloading and type conversions
PPT
Operator overloading in C++
PPTX
Operator overloading and type conversion in cpp
PPTX
Bca 2nd sem u-4 operator overloading
PPTX
Operator overloading
PPTX
operator overloading & type conversion in cpp over view || c++
PPTX
Operator overloading
Operator overloading and type conversions
Operator overloading in C++
Operator overloading and type conversion in cpp
Bca 2nd sem u-4 operator overloading
Operator overloading
operator overloading & type conversion in cpp over view || c++
Operator overloading

What's hot (20)

PPT
Lec 26.27-operator overloading
PPTX
Operator overloadng
PPTX
PPTX
Presentation on overloading
PDF
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
PPT
Operator overloading
PPTX
Operator overloading
PPT
14 operator overloading
PPTX
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
PPTX
Operator overloading
PPTX
Data Type Conversion in C++
PPTX
OPERATOR OVERLOADING IN C++
PDF
Operator overloading
PPTX
Operator Overloading & Type Conversions
PPT
Operator Overloading
PPT
Operator overloading
PPT
Lecture5
PPTX
Unary operator overloading
PPT
C++ overloading
PPT
Lec 28 - operator overloading
Lec 26.27-operator overloading
Operator overloadng
Presentation on overloading
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
Operator overloading
Operator overloading
14 operator overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
Operator overloading
Data Type Conversion in C++
OPERATOR OVERLOADING IN C++
Operator overloading
Operator Overloading & Type Conversions
Operator Overloading
Operator overloading
Lecture5
Unary operator overloading
C++ overloading
Lec 28 - operator overloading
Ad

Similar to Operator_Overloaing_Type_Conversion_OOPC(C++) (20)

PDF
Operator overloading
PDF
NIKUL SURANI
PPTX
Operator overloading2
PDF
Ch-4-Operator Overloading.pdf
PPTX
Operator overloaing
PPTX
3. Polymorphism.pptx
PDF
Basics _of_Operator Overloading_Somesh_Kumar_SSTC
PPTX
oprators in cpp,types with example and details.pptx
PPTX
Mca 2nd sem u-4 operator overloading
PDF
Polymorphism and Type Conversion.pdf pot
PPTX
B.sc CSIT 2nd semester C++ Unit4
PPTX
PDF
Object Oriented Programming using C++ - Part 3
PDF
Lec 8.pdf a
PPTX
Operator Overloading
PPT
Unary operator overloading
PDF
Unit3_OOP-converted.pdf
PDF
M11 operator overloading and type conversion
PDF
22 scheme OOPs with C++ BCS306B_module3.pdf
Operator overloading
NIKUL SURANI
Operator overloading2
Ch-4-Operator Overloading.pdf
Operator overloaing
3. Polymorphism.pptx
Basics _of_Operator Overloading_Somesh_Kumar_SSTC
oprators in cpp,types with example and details.pptx
Mca 2nd sem u-4 operator overloading
Polymorphism and Type Conversion.pdf pot
B.sc CSIT 2nd semester C++ Unit4
Object Oriented Programming using C++ - Part 3
Lec 8.pdf a
Operator Overloading
Unary operator overloading
Unit3_OOP-converted.pdf
M11 operator overloading and type conversion
22 scheme OOPs with C++ BCS306B_module3.pdf
Ad

More from Yaksh Jethva (7)

PDF
About Markets (Types of markets) - Economics
PDF
Cost and Various Cost Types
PDF
Logic Families ( Digital Electronics )
PDF
ANSI-SPARC Architecture - (3-Tier Architecture)
PDF
Transaction Properties(ACID Properties)
PDF
STACK ( LIFO STRUCTURE) - Data Structure
PDF
AVL tree ( Balanced Binary Search Tree)-Data Structure
About Markets (Types of markets) - Economics
Cost and Various Cost Types
Logic Families ( Digital Electronics )
ANSI-SPARC Architecture - (3-Tier Architecture)
Transaction Properties(ACID Properties)
STACK ( LIFO STRUCTURE) - Data Structure
AVL tree ( Balanced Binary Search Tree)-Data Structure

Recently uploaded (20)

DOCX
573137875-Attendance-Management-System-original
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
Well-logging-methods_new................
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
UNIT 4 Total Quality Management .pptx
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
Geodesy 1.pptx...............................................
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
573137875-Attendance-Management-System-original
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Embodied AI: Ushering in the Next Era of Intelligent Systems
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Well-logging-methods_new................
bas. eng. economics group 4 presentation 1.pptx
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
UNIT 4 Total Quality Management .pptx
Model Code of Practice - Construction Work - 21102022 .pdf
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
CH1 Production IntroductoryConcepts.pptx
Geodesy 1.pptx...............................................
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf

Operator_Overloaing_Type_Conversion_OOPC(C++)

  • 1. OPERATOR OVERLOADING & TYPE CONVERSION Name : Yaksh Jethva
  • 2. Contents ● 7.1 Defining operator Overloading ● 7.2 Overloading Unary Operator ● 7.3 Overloading Binary Operator ● 7.4 Overloading Operator Using Friend function ● 7.6 Type Conversion
  • 3. Introduction ● C++ has ability to provide the operators with a special meanings for a data type. ● The mechanism of giving such special meaning to an operator is known as operator overloading. Why Operator Overloading? ● Readable code ● Make operators sensitive to context
  • 4. General Format for operator overloading returnType operator operator_symbol(parameters); ➔ Return type may be whatever the operator returns ➔ Including a reference to the object of the operand ➔ Operator symbol may be any overloadable operator from the list.
  • 5. Restrictions on Operator Overloading ● C++ operators that can be overloaded. ● C++ Operators that cannot be overloaded Operators that cannot be overloaded .
  • 6. Overloading Unary Operator #include <iostream.h> class temp { Output Count: 6 private: int count; Public: temp():count(5) { } void operator ++() { count=count+1;} void Display() { cout<<"Count: "<<count; } }; int main() { temp t; ++t; /* operator function void operator ++() is called */ t.Display(); return 0; }
  • 7. Note Operator overloading cannot be used to change the way operator works on built-in types. Operator overloading only allows to redefine the meaning of operator for user-defined types. Only existing operators can be overloaded. New operators cannot be created. The overloaded operator must have at least one operand that is of user-defined type. We cannot change the basic meaning of an operator. Overloaded operators follow the syntax rules of the original operators.
  • 8. Overloading Binary Operator #include<iostream.h> #include<conio.h> class complex { int a,b; public: void getvalue() void display() { { cout<<a<<"+"<<b<<"i" <<"n"; } cout<<"Enter the value of Complex Numbers a,b:"; }; cin>>a>>b; } complex operator+(complex ob) { complex t; t.a=ob.a +a; t.b=ob.b+b; return(t); } complex operator-(complex ob) { complex t; t.a=ob.a - a; t.b=ob.b -b; return(t); }
  • 9. Binary operator void main() { clrscr(); complex obj1,obj2,result,result1; obj1.getvalue(); obj2.getvalue(); result = obj1+obj2; result1=obj1-obj2; cout<<"Input Values:n"; obj1.display(); obj2.display(); cout<<"Result:"; result.display(); result1.display(); getch(); }
  • 10. Overloading operators using Friend ● Friend function using operator overloading offers better flexibility to the class. ● These functions are not a members of the class and they do not have 'this' pointer. ● When you overload a unary operator you have to pass one argument. ● When you overload a binary operator you have to pass two arguments. ● Friend function can access private members of a class directly. ● friend return-type operator operator-symbol (Variable 1, Varibale2) { //Statements; }
  • 11. #include<iostream> class UnaryFriend { int a=10; int b=20; int c=30; public: void getvalues() {cout<<"Values of A, B & Cn"; cout<<a<<"n"<<b<<"n"<<c<<"n "<<endl; } void show() {cout<<a<<"n"<<b<<"n"<<c<<" n"<<endl; } void friend operator-(UnaryFriend &x); }; void operator-(UnaryFriend &x) { x.a = -x.a; x.b = -x.b; x.c = -x.c; } int main() { UnaryFriend x1; x1.getvalues(); cout<<"Before Overloadingn"; x1.show(); cout<<"After Overloading n"; -x1; x1.show(); return 0; }
  • 12. class time { Private: int hours,minutes; Public: time( ) { hours=0; minutes=0; } time(int x,int y) { hours=x; minutes=y; } void display( ) { cout<<endl<<hours<<" hours and "<<minutes<<" minutes."; } friend time operator + (time, time); }; time operator + (time y, time z) { int h = y.hours + z.hours; int m = y.minutes + z.minutes; while (m>=60) { m = m-60; h = h+1; } return time(h,m); } int main( ) { time t1(2,40); time t2(3,30); time t3; t3 = t1+t2; t3.display( ); return 0; }
  • 13. class time { Private: int hours,minutes; Public: time( ) { hours=0; minutes=0; } time(int x,int y) { hours=x; minutes=y; } void display( ) { cout<<endl<<hours<<" hours and "<<minutes<<" minutes."; } friend time operator + (time, time); }; time operator + (time y, time z) { int h = y.hours + z.hours; int m = y.minutes + z.minutes; while (m>=60) { m = m-60; h = h+1; } return time(h,m); } int main( ) { time t1(2,40); time t2(3,30); time t3; t3 = t1+t2; t3.display( ); return 0; }
  • 14. Type Conversions ● the type conversions are automatic only when the data types involved are built-in types. ● int m; float x = 3.14159; m = x;// convert x to integer before its value is assigned // to m. ● For user defined data types, the compiler does not support automatic type conversions. ● Different situations of data conversion between incompatible types. ● Conversion from basic type to class type. ● Conversion from class type to basic type. ● Conversion from one class type to another class type.
  • 15. Basic to Class Type A constructor to build a string type object from a char * type variable. string : : string(char *a) { length = strlen(a); P = new char[length+1]; strcpy(P,a); } The variables length and p are data members of the class string.
  • 16. Class To Basic Type ● A constructor function do not support type conversion from a class type to a basic type. ● An overloaded casting operator is used to convert a class type data to a basic type. It is also referred to as conversion function. ● operator typename( ) { … ( function statements ) … } ● This function converts a class type data to typename. vector : : operator double( ) { double sum = 0; for (int i=0; i < size ; i++) sum = sum + v[i] * v[i]; return sqrt (sum); } ● This function converts a vector to the square root of the sum of squares of its components.
  • 17. Continue... The casting operator function should satisfy the following conditions: ● It must be a class member. ● It must not specify a return type. ● It must not have any arguments.
  • 18. One Class To Another Class Type ● Type objX = objY ; // objects of different types objX is an object of class X and objY is an object of class Y. ● The class Y type data is converted to the class X type data and the converted value is assigned to the objX. ● Conversion is takes place from class Y to class X. ● Y is known as source class. ● X is known as destination class.