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
Friend function
PPT
Function overloading(c++)
PDF
Constructors and Destructors
PPTX
Abstract class in c++
PPTX
[OOP - Lec 07] Access Specifiers
PPTX
Types of Constructor in C++
PPTX
[OOP - Lec 19] Static Member Functions
PPTX
Access specifier
Friend function
Function overloading(c++)
Constructors and Destructors
Abstract class in c++
[OOP - Lec 07] Access Specifiers
Types of Constructor in C++
[OOP - Lec 19] Static Member Functions
Access specifier

What's hot (20)

PPTX
Function overloading
PDF
Constructor and Destructor
PPTX
Pointers,virtual functions and polymorphism cpp
PPT
friend function(c++)
PPTX
Member Function in C++
PPTX
Operator overloading
PPTX
Operator overloading
PPT
Functions in c++
PPTX
Friend function & friend class
PPTX
Data members and member functions
PPTX
Polymorphism In c++
PPTX
PDF
Operator overloading C++
PPTX
classes and objects in C++
PPTX
Inheritance
PPTX
Pointers, virtual function and polymorphism
PPTX
Functions in c
PPTX
Interface in java
PPTX
array of object pointer in c++
PDF
Friend function in c++
Function overloading
Constructor and Destructor
Pointers,virtual functions and polymorphism cpp
friend function(c++)
Member Function in C++
Operator overloading
Operator overloading
Functions in c++
Friend function & friend class
Data members and member functions
Polymorphism In c++
Operator overloading C++
classes and objects in C++
Inheritance
Pointers, virtual function and polymorphism
Functions in c
Interface in java
array of object pointer in c++
Friend function in c++
Ad

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

PDF
Operator overloading
PPT
Lec 28 - operator overloading
PPT
Lec 26.27-operator overloading
PPTX
Operator overloading
PPTX
Operator overloading
PPTX
C++ language
PPTX
PDF
Ch-4-Operator Overloading.pdf
PPTX
c++ introduction, array, pointers included.pptx
PDF
Operator_Overloaing_Type_Conversion_OOPC(C++)
PPTX
Operator overloading and type conversions
PDF
overloading in C++
PDF
Basics _of_Operator Overloading_Somesh_Kumar_SSTC
PDF
NIKUL SURANI
PDF
Data types, operators and control structures unit-2.pdf
PDF
22 scheme OOPs with C++ BCS306B_module3.pdf
PPT
Operator overloading
PPTX
Operator overloading2
PPTX
Introduction to c++
DOCX
Bc0037
Operator overloading
Lec 28 - operator overloading
Lec 26.27-operator overloading
Operator overloading
Operator overloading
C++ language
Ch-4-Operator Overloading.pdf
c++ introduction, array, pointers included.pptx
Operator_Overloaing_Type_Conversion_OOPC(C++)
Operator overloading and type conversions
overloading in C++
Basics _of_Operator Overloading_Somesh_Kumar_SSTC
NIKUL SURANI
Data types, operators and control structures unit-2.pdf
22 scheme OOPs with C++ BCS306B_module3.pdf
Operator overloading
Operator overloading2
Introduction to c++
Bc0037
Ad

More from rajshreemuthiah (20)

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

Recently uploaded (20)

PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Approach and Philosophy of On baking technology
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
KodekX | Application Modernization Development
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Cloud computing and distributed systems.
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Electronic commerce courselecture one. Pdf
Unlocking AI with Model Context Protocol (MCP)
Building Integrated photovoltaic BIPV_UPV.pdf
Approach and Philosophy of On baking technology
Encapsulation_ Review paper, used for researhc scholars
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
20250228 LYD VKU AI Blended-Learning.pptx
Review of recent advances in non-invasive hemoglobin estimation
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Network Security Unit 5.pdf for BCA BBA.
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
CIFDAQ's Market Insight: SEC Turns Pro Crypto
“AI and Expert System Decision Support & Business Intelligence Systems”
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
KodekX | Application Modernization Development
Per capita expenditure prediction using model stacking based on satellite ima...
Cloud computing and distributed systems.
Digital-Transformation-Roadmap-for-Companies.pptx
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Electronic commerce courselecture one. Pdf

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;