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

What's hot (20)

PPTX
C Structures and Unions
Dhrumil Patel
 
PPT
friend function(c++)
Ritika Sharma
 
PPTX
07. Virtual Functions
Haresh Jaiswal
 
PPTX
Polymorphism In c++
Vishesh Jha
 
PPTX
Pure virtual function and abstract class
Amit Trivedi
 
PPTX
Pointers in c++
sai tarlekar
 
PPTX
Oop c++class(final).ppt
Alok Kumar
 
PPSX
C++ Programming Language
Mohamed Loey
 
PPT
08 c++ Operator Overloading.ppt
Tareq Hasan
 
PPTX
Inline Functions and Default arguments
Nikhil Pandit
 
PDF
Python functions
Prof. Dr. K. Adisesha
 
PPTX
Static Data Members and Member Functions
MOHIT AGARWAL
 
PPTX
Virtual function in C++ Pure Virtual Function
Kamlesh Makvana
 
PPT
Object and class
mohit tripathi
 
PPTX
encapsulation
shalini392
 
PDF
Operator overloading C++
Lahiru Dilshan
 
PPTX
Object Oriented Programming Using C++
Muhammad Waqas
 
PPTX
Functions in c
sunila tharagaturi
 
ODP
OOP java
xball977
 
PPTX
Operator overloading and type conversions
Amogh Kalyanshetti
 
C Structures and Unions
Dhrumil Patel
 
friend function(c++)
Ritika Sharma
 
07. Virtual Functions
Haresh Jaiswal
 
Polymorphism In c++
Vishesh Jha
 
Pure virtual function and abstract class
Amit Trivedi
 
Pointers in c++
sai tarlekar
 
Oop c++class(final).ppt
Alok Kumar
 
C++ Programming Language
Mohamed Loey
 
08 c++ Operator Overloading.ppt
Tareq Hasan
 
Inline Functions and Default arguments
Nikhil Pandit
 
Python functions
Prof. Dr. K. Adisesha
 
Static Data Members and Member Functions
MOHIT AGARWAL
 
Virtual function in C++ Pure Virtual Function
Kamlesh Makvana
 
Object and class
mohit tripathi
 
encapsulation
shalini392
 
Operator overloading C++
Lahiru Dilshan
 
Object Oriented Programming Using C++
Muhammad Waqas
 
Functions in c
sunila tharagaturi
 
OOP java
xball977
 
Operator overloading and type conversions
Amogh Kalyanshetti
 

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

PPTX
Operator overloading
ramya marichamy
 
PDF
NIKUL SURANI
Nikul4470
 
PDF
Operator_Overloaing_Type_Conversion_OOPC(C++)
Yaksh Jethva
 
PPT
Operator Overloading
Nilesh Dalvi
 
PPTX
Operator overloadng
preethalal
 
PPTX
OPERATOR OVERLOADING IN C++
Aabha Tiwari
 
PPTX
Operator overloading2
zindadili
 
PPTX
Bca 2nd sem u-4 operator overloading
Rai University
 
PDF
22 scheme OOPs with C++ BCS306B_module3.pdf
sindhus795217
 
PPTX
Mca 2nd sem u-4 operator overloading
Rai University
 
PDF
Polymorphism and Type Conversion.pdf pot
e13225064
 
PDF
Operators in C++
Padma Kannan
 
PPTX
Operator overloaing
zindadili
 
PPTX
B.sc CSIT 2nd semester C++ Unit4
Tekendra Nath Yogi
 
PDF
Lec 8.pdf a
aliashraf9689
 
PPT
Operator overloading
ArunaDevi63
 
PDF
M11 operator overloading and type conversion
NabeelaNousheen
 
PPTX
Operator overloading
Kumar
 
PPTX
Cpp (C++)
Jay Patel
 
PPTX
Oops
ankush_kumar
 
Operator overloading
ramya marichamy
 
NIKUL SURANI
Nikul4470
 
Operator_Overloaing_Type_Conversion_OOPC(C++)
Yaksh Jethva
 
Operator Overloading
Nilesh Dalvi
 
Operator overloadng
preethalal
 
OPERATOR OVERLOADING IN C++
Aabha Tiwari
 
Operator overloading2
zindadili
 
Bca 2nd sem u-4 operator overloading
Rai University
 
22 scheme OOPs with C++ BCS306B_module3.pdf
sindhus795217
 
Mca 2nd sem u-4 operator overloading
Rai University
 
Polymorphism and Type Conversion.pdf pot
e13225064
 
Operators in C++
Padma Kannan
 
Operator overloaing
zindadili
 
B.sc CSIT 2nd semester C++ Unit4
Tekendra Nath Yogi
 
Lec 8.pdf a
aliashraf9689
 
Operator overloading
ArunaDevi63
 
M11 operator overloading and type conversion
NabeelaNousheen
 
Operator overloading
Kumar
 
Cpp (C++)
Jay Patel
 
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
 
Ad

Recently uploaded (20)

PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
PDF
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
July Patch Tuesday
Ivanti
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
July Patch Tuesday
Ivanti
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 

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;