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

What's hot

Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloadingPrincess Sam
 
Operator overloadng
Operator overloadngOperator overloadng
Operator overloadngpreethalal
 
operator overloading in C++
operator overloading in C++operator overloading in C++
Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on overloading Charndeep Sekhon
 
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyC++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
GrejoJoby1
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingabhay singh
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingKumar
 
14 operator overloading
14 operator overloading14 operator overloading
14 operator overloading
Docent Education
 
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator OverloadingHadziq Fabroyir
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Garima Singh Makhija
 
Data Type Conversion in C++
Data Type Conversion in C++Data Type Conversion in C++
Data Type Conversion in C++Danial Mirza
 
OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++
Aabha Tiwari
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingKamal Acharya
 
Operator Overloading & Type Conversions
Operator Overloading & Type ConversionsOperator Overloading & Type Conversions
Operator Overloading & Type Conversions
Rokonuzzaman Rony
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
Nilesh Dalvi
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
ArunaDevi63
 
Unary operator overloading
Unary operator overloadingUnary operator overloading
Unary operator overloading
Md. Ashraful Islam
 
C++ overloading
C++ overloadingC++ overloading
C++ overloading
sanya6900
 
Lec 28 - operator overloading
Lec 28 - operator overloadingLec 28 - operator overloading
Lec 28 - operator overloadingPrincess Sam
 

What's hot (20)

Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloading
 
Operator overloadng
Operator overloadngOperator overloadng
Operator overloadng
 
operator overloading in C++
operator overloading in C++operator overloading in C++
operator overloading in C++
 
Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on overloading
 
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyC++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
14 operator overloading
14 operator overloading14 operator overloading
14 operator overloading
 
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Data Type Conversion in C++
Data Type Conversion in C++Data Type Conversion in C++
Data Type Conversion in C++
 
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 & Type Conversions
Operator Overloading & Type ConversionsOperator Overloading & Type Conversions
Operator Overloading & Type Conversions
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Lecture5
Lecture5Lecture5
Lecture5
 
Unary operator overloading
Unary operator overloadingUnary operator overloading
Unary operator overloading
 
C++ overloading
C++ overloadingC++ overloading
C++ overloading
 
Lec 28 - operator overloading
Lec 28 - operator overloadingLec 28 - operator overloading
Lec 28 - operator overloading
 

Similar to Operator_Overloaing_Type_Conversion_OOPC(C++)

Operator overloading
Operator overloadingOperator overloading
Operator overloading
Pranali Chaudhari
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
Pranali Chaudhari
 
chapter-8-function-overloading.pdf
chapter-8-function-overloading.pdfchapter-8-function-overloading.pdf
chapter-8-function-overloading.pdf
study material
 
Operator overloaing
Operator overloaingOperator overloaing
Operator overloaing
zindadili
 
Ch-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdfCh-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdf
esuEthopi
 
C++ tutorials
C++ tutorialsC++ tutorials
C++ tutorials
Divyanshu Dubey
 
overloading in C++
overloading in C++overloading in C++
overloading in C++
Prof Ansari
 
parameter passing in c#
parameter passing in c#parameter passing in c#
parameter passing in c#
khush_boo31
 
operator overloading
operator overloadingoperator overloading
operator overloading
Nishant Joshi
 
Object Oriented Programming using C++ - Part 3
Object Oriented Programming using C++ - Part 3Object Oriented Programming using C++ - Part 3
Object Oriented Programming using C++ - Part 3
University College of Engineering Kakinada, JNTUK - Kakinada, India
 
Unit 4.pdf
Unit 4.pdfUnit 4.pdf
Unit 4.pdf
thenmozhip8
 
Operator overloading2
Operator overloading2Operator overloading2
Operator overloading2
zindadili
 
3. Polymorphism.pptx
3. Polymorphism.pptx3. Polymorphism.pptx
3. Polymorphism.pptx
ChhaviCoachingCenter
 
Reference Parameter, Passing object by reference, constant parameter & Defaul...
Reference Parameter, Passing object by reference, constant parameter & Defaul...Reference Parameter, Passing object by reference, constant parameter & Defaul...
Reference Parameter, Passing object by reference, constant parameter & Defaul...
Meghaj Mallick
 
object oriented programming language.pptx
object oriented programming language.pptxobject oriented programming language.pptx
object oriented programming language.pptx
syedabbas594247
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
Juginder Pal Singh
 

Similar to Operator_Overloaing_Type_Conversion_OOPC(C++) (20)

Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
3d7b7 session4 c++
3d7b7 session4 c++3d7b7 session4 c++
3d7b7 session4 c++
 
chapter-8-function-overloading.pdf
chapter-8-function-overloading.pdfchapter-8-function-overloading.pdf
chapter-8-function-overloading.pdf
 
Operator overloaing
Operator overloaingOperator overloaing
Operator overloaing
 
Oops
OopsOops
Oops
 
Ch-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdfCh-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdf
 
C++ tutorials
C++ tutorialsC++ tutorials
C++ tutorials
 
overloading in C++
overloading in C++overloading in C++
overloading in C++
 
parameter passing in c#
parameter passing in c#parameter passing in c#
parameter passing in c#
 
operator overloading
operator overloadingoperator overloading
operator overloading
 
Object Oriented Programming using C++ - Part 3
Object Oriented Programming using C++ - Part 3Object Oriented Programming using C++ - Part 3
Object Oriented Programming using C++ - Part 3
 
Unit 4.pdf
Unit 4.pdfUnit 4.pdf
Unit 4.pdf
 
Operator overloading2
Operator overloading2Operator overloading2
Operator overloading2
 
3. Polymorphism.pptx
3. Polymorphism.pptx3. Polymorphism.pptx
3. Polymorphism.pptx
 
Reference Parameter, Passing object by reference, constant parameter & Defaul...
Reference Parameter, Passing object by reference, constant parameter & Defaul...Reference Parameter, Passing object by reference, constant parameter & Defaul...
Reference Parameter, Passing object by reference, constant parameter & Defaul...
 
object oriented programming language.pptx
object oriented programming language.pptxobject oriented programming language.pptx
object oriented programming language.pptx
 
Object Oriented Programming with C++
Object Oriented Programming with C++Object Oriented Programming with C++
Object Oriented Programming with C++
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Overloading
OverloadingOverloading
Overloading
 

More from Yaksh Jethva

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

More from Yaksh Jethva (7)

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

Recently uploaded

WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
AafreenAbuthahir2
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
Jayaprasanna4
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
SamSarthak3
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
ssuser9bd3ba
 
addressing modes in computer architecture
addressing modes  in computer architectureaddressing modes  in computer architecture
addressing modes in computer architecture
ShahidSultan24
 
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdfCOLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
Kamal Acharya
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
AJAYKUMARPUND1
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
gdsczhcet
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
abh.arya
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
Divya Somashekar
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
Kamal Acharya
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
gerogepatton
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
VENKATESHvenky89705
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
ankuprajapati0525
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 

Recently uploaded (20)

WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
 
addressing modes in computer architecture
addressing modes  in computer architectureaddressing modes  in computer architecture
addressing modes in computer architecture
 
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdfCOLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
road safety engineering r s e unit 3.pdf
road safety engineering  r s e unit 3.pdfroad safety engineering  r s e unit 3.pdf
road safety engineering r s e unit 3.pdf
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..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.