SlideShare a Scribd company logo
3
Most read
11
Most read
17
Most read
D.SHANMUGAPRIYA
I- M.Sc(IT)
OPERATOR OVERLOADING AND
TYPE CONVERSION
OPERATOR OVERLOADING AND
TYPE CONVERSION
 INTRODUCTION
 DEFINING OPERATOR OVERLOADING
 OVERLOADING UNARY OPERATORS
 OVERLOADI NG BINARY OPERATORS
 OVERLOADING BINARY OPERATORS USING
FRIENDS
 SOME OTHER OPERATOR OVERLOADING
EXAMPLES
 RULES FOR OVERLOADING OPERATORS
 TYPE CONVERSION
INTRODUCTION
Operator overloading is one of the many
exciting features of the C++ language.
Operator overloading provides a flexible option
for the creation of new definition for most of the
C++ operators.
Class member access operator(.,.*)
Scope resolution operator(::)
Size operator(size of)
Conditional operator(?:)
DEFINNING OPERATOR
OVERLOADING
We must specify what it means in relation to the class
to which the operator is applied.
Syntax:
Return type classname::operator op(arglist)
{
function body//task defined
}
 Operator functions must be either member functions or friend
functions:
 vector operator+(vector);//vectoraddition
 Vector operator-(vector)://unary minus
CONTINUE….
 Friend vector operator+(vector;vector); //vector addition
 Friend vector operator-(vector); //unary minus
 Vector operator-(vector&a); //subtraction
 Int operator==(vector,vector); //comparison
 The process of overloading involves the following steps:
 Create a class that defines the datatype that is to be used in the
overloading operation.
 Declare the operator functions operator op() in the public part of the
class.
 UNARY OPERATOR=X OP Y
 BINARY OPERATOR=OPERATOR OP(X)
OVERLOADING UNARY
OPERATORS
 A minus operator when use as a unary takes just one
operand.
 Remember a statement like
 It is possible to overload a unary minus operator using a friend
function as follows:
Friend void operator-(space&s);
void operator-(space&s)
S2=-S1;
S.X=-S.X;
S.Y=-S.Y;
S.Z=-S.Z;
Example:
OVERLOADING BINARY
OPERATORS
 The same mechanism can be used to overload a binary
operator.
 C1 takes the responsibility of invoking the function
and c2 plays the role of an argument that is passed to
the function.
 Both the objects are available for the function.
C=SUM(A,B);
C3=C1.OPERATOR+(C2);
TEMP.X=X+C.X;
Example:
IMPLEMENTED OF THE OVERLOAD BINARY
OPERATOR:
OVERLOADING BINARY
OPERATORS USING FRIENDS
 Friend functions may be used in the place of member functions
for overloading a binary operator.
1) Replace the member function declartion by the friend
function declaration
2) [friend complex operation+(complex,complex);
3) Redefine the operator function as followa:
[complex opearor+(complex a,complex b)
{
return complex((a.x+b.x),(a.y+b.y));
}
In this case the statement
C3=C1+C2;
C3=OPERATOR+(C1+C2);
MANIPULATION OF STRINGS
USING OPERATORS
 ANSI C implements strings using character arrays,pointers and string
functions.
 EXAMPLE:
 Thus we must create string objects that we can hold these two pieces of
information.
Class string
{
Char*p;
Int len;
Public:
}
String3=string1+string2;
If(string 1>=string2)string=string1;
MATHEMATICAL OPERATIONS ON
STRING:
SUB TOPICS
OVERLOADING THE SUBSCRIPT OPERATOR
o The subscript operator is normally used to access and
modify a specific element in an array.
OVERLOADING THE POINTER TO MEMBER -
>OPERATOR:
o The pointer to member operator is normally used to
conjunction with an object pointer to access any of the
objects member.
RULES FOR OVERLOADING
OPERATOR
 There are certain restrictions and limitations in
overloading them,some of them are listed below
Only existing operators can be overloaded
Overloaded operators follow the syntax rules of the
original operators.
There are some operators that cannot be overloaded.
SIZE OF SIZE OF OPERATOR
. Membership operator
.* pointer to member operator
:: scope resolution operator
?: conditional operator
TYPE CONVERSION
 The type of data to the right of an assignment
operator is automatically converted to the type of
the variable on the left.
BASIC TO CLASS TYPE:
1) It may be recalled that the use of constructor was
illustrated in a number of examples to initialize
objects.
int m;
float x=3.14159;
m=x;
Continue…
 Following constructor:
string::string(char*a)
{
length=strlength(a);
p=new char[length+1];
strcpy(p,a);
}
The statement
s1=string(name 1);
First converts name1 from char* type and then values the
assign the strings type values to the objects S1
S2=Name 2;
T
H
A
N
K
y
O
U

More Related Content

PPTX
Pointers,virtual functions and polymorphism cpp
rajshreemuthiah
 
PPTX
classes and objects in C++
HalaiHansaika
 
PPTX
Functions in c++
Rokonuzzaman Rony
 
PPTX
Constructors in C++
RubaNagarajan
 
PDF
Constructors and Destructors
Dr Sukhpal Singh Gill
 
PPTX
Static keyword ppt
Vinod Kumar
 
PPTX
Constructors and Destructor in C++
International Institute of Information Technology (I²IT)
 
PPTX
This pointer
Kamal Acharya
 
Pointers,virtual functions and polymorphism cpp
rajshreemuthiah
 
classes and objects in C++
HalaiHansaika
 
Functions in c++
Rokonuzzaman Rony
 
Constructors in C++
RubaNagarajan
 
Constructors and Destructors
Dr Sukhpal Singh Gill
 
Static keyword ppt
Vinod Kumar
 
This pointer
Kamal Acharya
 

What's hot (20)

PPTX
Static Data Members and Member Functions
MOHIT AGARWAL
 
PPTX
Operator overloading
Burhan Ahmed
 
PPTX
INLINE FUNCTION IN C++
Vraj Patel
 
PDF
Function overloading ppt
Prof. Dr. K. Adisesha
 
PDF
Classes and objects
Nilesh Dalvi
 
PPSX
Function in c
savitamhaske
 
PPTX
Inheritance in c++
Vineeta Garg
 
PPTX
Pointer in C++
Mauryasuraj98
 
PPTX
Destructors
DeepikaT13
 
PPTX
Classes and objects
rajveer_Pannu
 
PPTX
Data types in c++
Venkata.Manish Reddy
 
PPTX
Call by value or call by reference in C++
Sachin Yadav
 
PPT
Functions in c++
Maaz Hasan
 
PDF
Javascript Arrow function
tanerochris
 
PPT
Method overriding
Azaz Maverick
 
PPTX
Encapsulation C++
Hashim Hashim
 
PPTX
Operator Overloading & Function Overloading
Meghaj Mallick
 
PPTX
Classes, objects in JAVA
Abhilash Nair
 
PPT
Operator overloading
abhay singh
 
PDF
06. operator overloading
Haresh Jaiswal
 
Static Data Members and Member Functions
MOHIT AGARWAL
 
Operator overloading
Burhan Ahmed
 
INLINE FUNCTION IN C++
Vraj Patel
 
Function overloading ppt
Prof. Dr. K. Adisesha
 
Classes and objects
Nilesh Dalvi
 
Function in c
savitamhaske
 
Inheritance in c++
Vineeta Garg
 
Pointer in C++
Mauryasuraj98
 
Destructors
DeepikaT13
 
Classes and objects
rajveer_Pannu
 
Data types in c++
Venkata.Manish Reddy
 
Call by value or call by reference in C++
Sachin Yadav
 
Functions in c++
Maaz Hasan
 
Javascript Arrow function
tanerochris
 
Method overriding
Azaz Maverick
 
Encapsulation C++
Hashim Hashim
 
Operator Overloading & Function Overloading
Meghaj Mallick
 
Classes, objects in JAVA
Abhilash Nair
 
Operator overloading
abhay singh
 
06. operator overloading
Haresh Jaiswal
 
Ad

Similar to Operator overloading and type conversion in cpp (20)

PDF
Operator overloading
Pranali Chaudhari
 
PPT
Lec 28 - operator overloading
Princess Sam
 
PPT
Lec 26.27-operator overloading
Princess Sam
 
PPTX
Operator overloading
ramya marichamy
 
PPTX
Operator overloading
Garima Singh Makhija
 
PPTX
C++ language
Hamza Asif
 
PPTX
Oops
ankush_kumar
 
PDF
Ch-4-Operator Overloading.pdf
esuEthopi
 
PPTX
c++ introduction, array, pointers included.pptx
fn723290
 
PDF
Operator_Overloaing_Type_Conversion_OOPC(C++)
Yaksh Jethva
 
PPTX
Operator overloading and type conversions
Amogh Kalyanshetti
 
PDF
overloading in C++
Prof Ansari
 
PDF
Basics _of_Operator Overloading_Somesh_Kumar_SSTC
drsomeshdewangan
 
PDF
NIKUL SURANI
Nikul4470
 
PDF
Data types, operators and control structures unit-2.pdf
gurpreetk8199
 
PDF
22 scheme OOPs with C++ BCS306B_module3.pdf
sindhus795217
 
PPT
Operator overloading
ArunaDevi63
 
PPTX
Operator overloading2
zindadili
 
PPTX
Introduction to c++
somu rajesh
 
DOCX
Bc0037
hayerpa
 
Operator overloading
Pranali Chaudhari
 
Lec 28 - operator overloading
Princess Sam
 
Lec 26.27-operator overloading
Princess Sam
 
Operator overloading
ramya marichamy
 
Operator overloading
Garima Singh Makhija
 
C++ language
Hamza Asif
 
Ch-4-Operator Overloading.pdf
esuEthopi
 
c++ introduction, array, pointers included.pptx
fn723290
 
Operator_Overloaing_Type_Conversion_OOPC(C++)
Yaksh Jethva
 
Operator overloading and type conversions
Amogh Kalyanshetti
 
overloading in C++
Prof Ansari
 
Basics _of_Operator Overloading_Somesh_Kumar_SSTC
drsomeshdewangan
 
NIKUL SURANI
Nikul4470
 
Data types, operators and control structures unit-2.pdf
gurpreetk8199
 
22 scheme OOPs with C++ BCS306B_module3.pdf
sindhus795217
 
Operator overloading
ArunaDevi63
 
Operator overloading2
zindadili
 
Introduction to c++
somu rajesh
 
Bc0037
hayerpa
 
Ad

More from rajshreemuthiah (20)

PPTX
oracle
rajshreemuthiah
 
PPTX
quality
rajshreemuthiah
 
PPTX
bigdata
rajshreemuthiah
 
PPTX
polymorphism
rajshreemuthiah
 
PPTX
solutions and understanding text analytics
rajshreemuthiah
 
PPTX
interface
rajshreemuthiah
 
PPTX
Testing &ampdebugging
rajshreemuthiah
 
PPTX
concurrency control
rajshreemuthiah
 
PPTX
Education
rajshreemuthiah
 
PPTX
Formal verification
rajshreemuthiah
 
PPTX
Transaction management
rajshreemuthiah
 
PPTX
Multi thread
rajshreemuthiah
 
PPTX
System testing
rajshreemuthiah
 
PPTX
software maintenance
rajshreemuthiah
 
PPTX
exception handling
rajshreemuthiah
 
PPTX
e governance
rajshreemuthiah
 
PPTX
recovery management
rajshreemuthiah
 
PPTX
Implementing polymorphism
rajshreemuthiah
 
PPSX
Buffer managements
rajshreemuthiah
 
PPTX
os linux
rajshreemuthiah
 
polymorphism
rajshreemuthiah
 
solutions and understanding text analytics
rajshreemuthiah
 
interface
rajshreemuthiah
 
Testing &ampdebugging
rajshreemuthiah
 
concurrency control
rajshreemuthiah
 
Education
rajshreemuthiah
 
Formal verification
rajshreemuthiah
 
Transaction management
rajshreemuthiah
 
Multi thread
rajshreemuthiah
 
System testing
rajshreemuthiah
 
software maintenance
rajshreemuthiah
 
exception handling
rajshreemuthiah
 
e governance
rajshreemuthiah
 
recovery management
rajshreemuthiah
 
Implementing polymorphism
rajshreemuthiah
 
Buffer managements
rajshreemuthiah
 
os linux
rajshreemuthiah
 

Recently uploaded (20)

PDF
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 
PDF
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
PDF
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
PDF
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PDF
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
PDF
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
PPTX
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PDF
Software Development Methodologies in 2025
KodekX
 
PDF
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
PDF
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
Software Development Methodologies in 2025
KodekX
 
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 

Operator overloading and type conversion in cpp

  • 2. OPERATOR OVERLOADING AND TYPE CONVERSION  INTRODUCTION  DEFINING OPERATOR OVERLOADING  OVERLOADING UNARY OPERATORS  OVERLOADI NG BINARY OPERATORS  OVERLOADING BINARY OPERATORS USING FRIENDS  SOME OTHER OPERATOR OVERLOADING EXAMPLES  RULES FOR OVERLOADING OPERATORS  TYPE CONVERSION
  • 3. INTRODUCTION Operator overloading is one of the many exciting features of the C++ language. Operator overloading provides a flexible option for the creation of new definition for most of the C++ operators. Class member access operator(.,.*) Scope resolution operator(::) Size operator(size of) Conditional operator(?:)
  • 4. DEFINNING OPERATOR OVERLOADING We must specify what it means in relation to the class to which the operator is applied. Syntax: Return type classname::operator op(arglist) { function body//task defined }  Operator functions must be either member functions or friend functions:  vector operator+(vector);//vectoraddition  Vector operator-(vector)://unary minus
  • 5. CONTINUE….  Friend vector operator+(vector;vector); //vector addition  Friend vector operator-(vector); //unary minus  Vector operator-(vector&a); //subtraction  Int operator==(vector,vector); //comparison  The process of overloading involves the following steps:  Create a class that defines the datatype that is to be used in the overloading operation.  Declare the operator functions operator op() in the public part of the class.  UNARY OPERATOR=X OP Y  BINARY OPERATOR=OPERATOR OP(X)
  • 6. OVERLOADING UNARY OPERATORS  A minus operator when use as a unary takes just one operand.  Remember a statement like  It is possible to overload a unary minus operator using a friend function as follows: Friend void operator-(space&s); void operator-(space&s) S2=-S1; S.X=-S.X; S.Y=-S.Y; S.Z=-S.Z;
  • 8. OVERLOADING BINARY OPERATORS  The same mechanism can be used to overload a binary operator.  C1 takes the responsibility of invoking the function and c2 plays the role of an argument that is passed to the function.  Both the objects are available for the function. C=SUM(A,B); C3=C1.OPERATOR+(C2); TEMP.X=X+C.X;
  • 10. IMPLEMENTED OF THE OVERLOAD BINARY OPERATOR:
  • 11. OVERLOADING BINARY OPERATORS USING FRIENDS  Friend functions may be used in the place of member functions for overloading a binary operator. 1) Replace the member function declartion by the friend function declaration 2) [friend complex operation+(complex,complex); 3) Redefine the operator function as followa: [complex opearor+(complex a,complex b) { return complex((a.x+b.x),(a.y+b.y)); } In this case the statement C3=C1+C2; C3=OPERATOR+(C1+C2);
  • 12. MANIPULATION OF STRINGS USING OPERATORS  ANSI C implements strings using character arrays,pointers and string functions.  EXAMPLE:  Thus we must create string objects that we can hold these two pieces of information. Class string { Char*p; Int len; Public: } String3=string1+string2; If(string 1>=string2)string=string1;
  • 14. SUB TOPICS OVERLOADING THE SUBSCRIPT OPERATOR o The subscript operator is normally used to access and modify a specific element in an array. OVERLOADING THE POINTER TO MEMBER - >OPERATOR: o The pointer to member operator is normally used to conjunction with an object pointer to access any of the objects member.
  • 15. RULES FOR OVERLOADING OPERATOR  There are certain restrictions and limitations in overloading them,some of them are listed below Only existing operators can be overloaded Overloaded operators follow the syntax rules of the original operators. There are some operators that cannot be overloaded. SIZE OF SIZE OF OPERATOR . Membership operator .* pointer to member operator :: scope resolution operator ?: conditional operator
  • 16. TYPE CONVERSION  The type of data to the right of an assignment operator is automatically converted to the type of the variable on the left. BASIC TO CLASS TYPE: 1) It may be recalled that the use of constructor was illustrated in a number of examples to initialize objects. int m; float x=3.14159; m=x;
  • 17. Continue…  Following constructor: string::string(char*a) { length=strlength(a); p=new char[length+1]; strcpy(p,a); } The statement s1=string(name 1); First converts name1 from char* type and then values the assign the strings type values to the objects S1 S2=Name 2;