SlideShare una empresa de Scribd logo
1 de 24
Descargar para leer sin conexión
Operator Overloading
INTRODUCTION
OPERATOR OVERLOADING RESTRICTION
GENERAL RULES FOR OVERLOADING OPERATOR
OVERLOADING UNARY OPERATOR
OVERLOADING BINARY OPERATOR

Compiled By: Kamal Acharya
Introduction
 It gives additional meaning to the C++ operator

when applied to the user defined data types.
 We can create our own language by using the
concept of operator overloading appropriately.
 It require great care when misused program became
very difficult to understand

Compiled By: Kamal Acharya
Operator Overloading Restriction
 Following C++ Operator can’t be overloaded

Class member access operators(. & .*)
Scope Resolution Operator(::)
Sizeof Operator(sizeof())
Conditional Operator(? :)
 Precedence of an operator cannot be changed

 Associativity of an operator cannot be changed
 Arity (number of operands) cannot be changed

Unary operators remain unary, and binary operators remain
binary
 Operators &, *, + and - each have unary and binary versions
 Unary and binary versions can be overloaded separately


Compiled By: Kamal Acharya
 No new operators can be created


Use only existing operators

 No overloading operators for built-in types


Cannot change how two integers are added

Compiled By: Kamal Acharya
General Rules for overloading Operator
 Syntax:

returnType classname::operator op(arguments)
{
body;
}
 Example
void Integer::operator op()
{
Body;
}
Compiled By: Kamal Acharya
Steps
Create a class that is to be used
2. Declare the operator function in the public part of
the class. It may be either member function or
friend function.
3. Define operator function to implement the
operation required
4. Overloaded can be invoked using the syntax such
as:
op x;
1.

Compiled By: Kamal Acharya
Overloading Unary Operator
 Unary operator are those operator which works only

on the single operand. Eg. ++, --, - etc
 Unary operator acts on only one operand and can be
overloaded in two ways:
1.

2.

Using non-static member function with no arguments
Using friend function with one argument where the
argument must be either an object of the class or an
reference to an object of the class

Compiled By: Kamal Acharya
Using non static member function
 Example

class Test
{
…………
public:
void operator op()
{……… }
};

Compiled By: Kamal Acharya

Void main()
{
…….
…….
op obj1; /* Same as
obj1.operator
op()*/
………………..
}
Sample Program
#include<iostream.h>
#include<conio.h>
class increment
{
int m,n;
public:
increment(int x, int y)
{
m=x;
n=y;
}

Compiled By: Kamal Acharya

void display()
{
cout<<"m= "<<m<<
"n="<<n<<endl;
}
void operator ++()
{
m++;n++;
}
};
void main()
{
clrscr();
increment in1(20,30);
in1.display();
++in1;
in1.display();
increment in2(1,2);

Compiled By: Kamal Acharya

in2.display();
in2.operator ++();
in2.display();
getch();
}
OUTPUT

Compiled By: Kamal Acharya
Using Friend Function
 Example

class Test
{
…………
public:
friend void operator
op(Test);
};

Compiled By: Kamal Acharya

Void main()
{
…….
…….
op obj1; /* Same as
operator op(obj1)*/
………………..
}
Sample Program
#include<iostream.h>
#include<conio.h>
class increment
{
int m,n;
public:
increment(int x, int y)
{
m=x; n=y;
}

Compiled By: Kamal Acharya

void display()
{
cout<<"m= "<<m<<
"n="<<n<<endl;
}
friend void operator
++(increment&);
};
void operator
++(increment& x)
{
++x.m;
++x.n;
}
void main()
{
clrscr();
increment in1(20,30);
Compiled By: Kamal Acharya

in1.display();
++in1;
in1.display();
increment in2(1,2);
in2.display();
operator ++(in2);
in2.display();
getch();
}
OUTPUT

Compiled By: Kamal Acharya
Overloading Binary Operator
 Binary operator are those operator which works on

two operands. Eg. +, -,*,/ etc
 Binary operator acts on two operands and can be
overloaded in two ways:
1.

2.

Using non-static member function with single argument.
Using friend function with two arguments where the
arguments must be either an object of the class or an
reference to an object of the class.

Compiled By: Kamal Acharya
Using non static member function
 Example

class Test
{
…………
public:
void operator op(Test)
{……… }
};

Compiled By: Kamal Acharya

Void main()
{
…….
…….
obj1 op obj2; /* Same as
obj1.operator op(obj2)*/
………………..
}
Sample Program
#include<iostream.h>
#include<conio.h>
class add
{
int m,n;
public:
add(int x, int y)
{
m=x; n=y;
}
Compiled By: Kamal Acharya

void display()
{
cout<<"m= "<<m<<"
n="<<n<<endl;
}
void operator +(add);
};
void add::operator +(add
x)
{
m=m+x.m;
n=n+x.n;
}

Compiled By: Kamal Acharya

void main()
{
clrscr();
add
obj1(20,30),obj2(2,3);
obj1.display();
obj2.display();
obj1+obj2;
obj1.display();
getch();
}
Compiled By: Kamal Acharya
Using Friend Function
 Example

class Test
{
…………
public:
friend void operator
op(Test, Test);
};

Compiled By: Kamal Acharya

Void main()
{
…….
…….
obj1 op obj2; /* Same as
operator op(obj1, obj2)*/
………………..
}
Sample Program
#include<iostream.h>
#include<conio.h>
class add
{
int m,n;
public:
add(int x, int y)
{
m=x; n=y;
}
Compiled By: Kamal Acharya

void display()
{
cout<<"m= "<<m<<"
n="<<n<<endl;
}
friend void operator
+(add&,add&);
};
void operator +(add& x,
add& y)
{
x.m=x.m+y.m;
x.n=x.n+y.n;
}

Compiled By: Kamal Acharya

void main()
{
clrscr();
add
obj1(20,30),obj2(2,3);
obj1.display();
obj2.display();
obj1+obj2;
obj1.display();
getch();
}
Compiled By: Kamal Acharya

Más contenido relacionado

La actualidad más candente

08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
Tareq Hasan
 
Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on overloading
Charndeep Sekhon
 
#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
Hadziq Fabroyir
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
abhay singh
 

La actualidad más candente (20)

Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Unary operator overloading
Unary operator overloadingUnary operator overloading
Unary operator overloading
 
operator overloading in C++
operator overloading in C++operator overloading in C++
operator overloading in C++
 
Operator overloading
Operator overloading Operator overloading
Operator overloading
 
Operator overloading and type conversions
Operator overloading and type conversionsOperator overloading and type conversions
Operator overloading and type conversions
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
14 operator overloading
14 operator overloading14 operator overloading
14 operator overloading
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
 
C++ overloading
C++ overloadingC++ overloading
C++ overloading
 
Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on 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
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Operator overloading in C++
Operator  overloading in C++Operator  overloading in C++
Operator overloading in C++
 
c++
c++c++
c++
 
Operator overloading in C++
Operator overloading in C++Operator overloading in C++
Operator overloading in C++
 
operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++
 
Operator_Overloaing_Type_Conversion_OOPC(C++)
Operator_Overloaing_Type_Conversion_OOPC(C++)Operator_Overloaing_Type_Conversion_OOPC(C++)
Operator_Overloaing_Type_Conversion_OOPC(C++)
 

Destacado

Templates in C++
Templates in C++Templates in C++
Templates in C++
Tech_MX
 
This pointer .17
This pointer .17This pointer .17
This pointer .17
myrajendra
 

Destacado (18)

operator overloading in c++
operator overloading in c++operator overloading in c++
operator overloading in c++
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
06. operator overloading
06. operator overloading06. operator overloading
06. operator overloading
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
This pointer .17
This pointer .17This pointer .17
This pointer .17
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Dynamic Polymorphism in C++
Dynamic Polymorphism in C++Dynamic Polymorphism in C++
Dynamic Polymorphism in C++
 
C++ Template
C++ TemplateC++ Template
C++ Template
 
Templates in c++
Templates in c++Templates in c++
Templates in c++
 
Smart Pointers
Smart PointersSmart Pointers
Smart Pointers
 
Статический и динамический полиморфизм в C++, Дмитрий Леванов
Статический и динамический полиморфизм в C++, Дмитрий ЛевановСтатический и динамический полиморфизм в C++, Дмитрий Леванов
Статический и динамический полиморфизм в C++, Дмитрий Леванов
 
Effective stl notes
Effective stl notesEffective stl notes
Effective stl notes
 
High Order Function Computations in c++14 (C++ Dev Meetup Iasi)
High Order Function Computations in c++14 (C++ Dev Meetup Iasi)High Order Function Computations in c++14 (C++ Dev Meetup Iasi)
High Order Function Computations in c++14 (C++ Dev Meetup Iasi)
 
Dependency Injection in C++ (Community Days 2015)
Dependency Injection in C++ (Community Days 2015)Dependency Injection in C++ (Community Days 2015)
Dependency Injection in C++ (Community Days 2015)
 
Effective c++notes
Effective c++notesEffective c++notes
Effective c++notes
 
Templates
TemplatesTemplates
Templates
 
Modern C++
Modern C++Modern C++
Modern C++
 
C traps and pitfalls for C++ programmers
C traps and pitfalls for C++ programmersC traps and pitfalls for C++ programmers
C traps and pitfalls for C++ programmers
 

Similar a Operator overloading

Lec 28 - operator overloading
Lec 28 - operator overloadingLec 28 - operator overloading
Lec 28 - operator overloading
Princess Sam
 
(Polymorphism-OperatorOverLoadingUsingFriendFunction).pdf
(Polymorphism-OperatorOverLoadingUsingFriendFunction).pdf(Polymorphism-OperatorOverLoadingUsingFriendFunction).pdf
(Polymorphism-OperatorOverLoadingUsingFriendFunction).pdf
AakashBerlia1
 

Similar a Operator overloading (20)

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
 
Lec 28 - operator overloading
Lec 28 - operator overloadingLec 28 - operator overloading
Lec 28 - operator overloading
 
21CSC101T best ppt ever OODP UNIT-2.pptx
21CSC101T best ppt ever OODP UNIT-2.pptx21CSC101T best ppt ever OODP UNIT-2.pptx
21CSC101T best ppt ever OODP UNIT-2.pptx
 
cbybalaguruswami-e-180803051831.pptx
cbybalaguruswami-e-180803051831.pptxcbybalaguruswami-e-180803051831.pptx
cbybalaguruswami-e-180803051831.pptx
 
cbybalaguruswami-e-180803051831.pptx
cbybalaguruswami-e-180803051831.pptxcbybalaguruswami-e-180803051831.pptx
cbybalaguruswami-e-180803051831.pptx
 
OOPS-Seminar.pdf
OOPS-Seminar.pdfOOPS-Seminar.pdf
OOPS-Seminar.pdf
 
3d7b7 session4 c++
3d7b7 session4 c++3d7b7 session4 c++
3d7b7 session4 c++
 
Operator overloading2
Operator overloading2Operator overloading2
Operator overloading2
 
Thinking in Functions
Thinking in FunctionsThinking in Functions
Thinking in Functions
 
Mca 2nd sem u-4 operator overloading
Mca 2nd  sem u-4 operator overloadingMca 2nd  sem u-4 operator overloading
Mca 2nd sem u-4 operator overloading
 
(Polymorphism-OperatorOverLoadingUsingFriendFunction).pdf
(Polymorphism-OperatorOverLoadingUsingFriendFunction).pdf(Polymorphism-OperatorOverLoadingUsingFriendFunction).pdf
(Polymorphism-OperatorOverLoadingUsingFriendFunction).pdf
 
Oops
OopsOops
Oops
 
Ch-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdfCh-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdf
 
Operator overloaing
Operator overloaingOperator overloaing
Operator overloaing
 
Cpp (C++)
Cpp (C++)Cpp (C++)
Cpp (C++)
 
C++ language
C++ languageC++ language
C++ language
 
C by balaguruswami - e.balagurusamy
C   by balaguruswami - e.balagurusamyC   by balaguruswami - e.balagurusamy
C by balaguruswami - e.balagurusamy
 
operator overloading & type conversion in cpp
operator overloading & type conversion in cppoperator overloading & type conversion in cpp
operator overloading & type conversion in cpp
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Operator Overloading and Scope of Variable
Operator Overloading and Scope of VariableOperator Overloading and Scope of Variable
Operator Overloading and Scope of Variable
 

Más de Kamal Acharya

Más de Kamal Acharya (20)

Programming the basic computer
Programming the basic computerProgramming the basic computer
Programming the basic computer
 
Computer Arithmetic
Computer ArithmeticComputer Arithmetic
Computer Arithmetic
 
Introduction to Computer Security
Introduction to Computer SecurityIntroduction to Computer Security
Introduction to Computer Security
 
Session and Cookies
Session and CookiesSession and Cookies
Session and Cookies
 
Functions in php
Functions in phpFunctions in php
Functions in php
 
Web forms in php
Web forms in phpWeb forms in php
Web forms in php
 
Making decision and repeating in PHP
Making decision and repeating  in PHPMaking decision and repeating  in PHP
Making decision and repeating in PHP
 
Working with arrays in php
Working with arrays in phpWorking with arrays in php
Working with arrays in php
 
Text and Numbers (Data Types)in PHP
Text and Numbers (Data Types)in PHPText and Numbers (Data Types)in PHP
Text and Numbers (Data Types)in PHP
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
Capacity Planning of Data Warehousing
Capacity Planning of Data WarehousingCapacity Planning of Data Warehousing
Capacity Planning of Data Warehousing
 
Data Warehousing
Data WarehousingData Warehousing
Data Warehousing
 
Search Engines
Search EnginesSearch Engines
Search Engines
 
Web Mining
Web MiningWeb Mining
Web Mining
 
Information Privacy and Data Mining
Information Privacy and Data MiningInformation Privacy and Data Mining
Information Privacy and Data Mining
 
Cluster Analysis
Cluster AnalysisCluster Analysis
Cluster Analysis
 
Association Analysis in Data Mining
Association Analysis in Data MiningAssociation Analysis in Data Mining
Association Analysis in Data Mining
 
Classification techniques in data mining
Classification techniques in data miningClassification techniques in data mining
Classification techniques in data mining
 
Data Preprocessing
Data PreprocessingData Preprocessing
Data Preprocessing
 
Introduction to Data Mining and Data Warehousing
Introduction to Data Mining and Data WarehousingIntroduction to Data Mining and Data Warehousing
Introduction to Data Mining and Data Warehousing
 

Último

Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Último (20)

Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 

Operator overloading

  • 1. Operator Overloading INTRODUCTION OPERATOR OVERLOADING RESTRICTION GENERAL RULES FOR OVERLOADING OPERATOR OVERLOADING UNARY OPERATOR OVERLOADING BINARY OPERATOR Compiled By: Kamal Acharya
  • 2. Introduction  It gives additional meaning to the C++ operator when applied to the user defined data types.  We can create our own language by using the concept of operator overloading appropriately.  It require great care when misused program became very difficult to understand Compiled By: Kamal Acharya
  • 3. Operator Overloading Restriction  Following C++ Operator can’t be overloaded Class member access operators(. & .*) Scope Resolution Operator(::) Sizeof Operator(sizeof()) Conditional Operator(? :)  Precedence of an operator cannot be changed  Associativity of an operator cannot be changed  Arity (number of operands) cannot be changed Unary operators remain unary, and binary operators remain binary  Operators &, *, + and - each have unary and binary versions  Unary and binary versions can be overloaded separately  Compiled By: Kamal Acharya
  • 4.  No new operators can be created  Use only existing operators  No overloading operators for built-in types  Cannot change how two integers are added Compiled By: Kamal Acharya
  • 5. General Rules for overloading Operator  Syntax: returnType classname::operator op(arguments) { body; }  Example void Integer::operator op() { Body; } Compiled By: Kamal Acharya
  • 6. Steps Create a class that is to be used 2. Declare the operator function in the public part of the class. It may be either member function or friend function. 3. Define operator function to implement the operation required 4. Overloaded can be invoked using the syntax such as: op x; 1. Compiled By: Kamal Acharya
  • 7. Overloading Unary Operator  Unary operator are those operator which works only on the single operand. Eg. ++, --, - etc  Unary operator acts on only one operand and can be overloaded in two ways: 1. 2. Using non-static member function with no arguments Using friend function with one argument where the argument must be either an object of the class or an reference to an object of the class Compiled By: Kamal Acharya
  • 8. Using non static member function  Example class Test { ………… public: void operator op() {……… } }; Compiled By: Kamal Acharya Void main() { ……. ……. op obj1; /* Same as obj1.operator op()*/ ……………….. }
  • 9. Sample Program #include<iostream.h> #include<conio.h> class increment { int m,n; public: increment(int x, int y) { m=x; n=y; } Compiled By: Kamal Acharya void display() { cout<<"m= "<<m<< "n="<<n<<endl; } void operator ++() { m++;n++; } };
  • 10. void main() { clrscr(); increment in1(20,30); in1.display(); ++in1; in1.display(); increment in2(1,2); Compiled By: Kamal Acharya in2.display(); in2.operator ++(); in2.display(); getch(); }
  • 12. Using Friend Function  Example class Test { ………… public: friend void operator op(Test); }; Compiled By: Kamal Acharya Void main() { ……. ……. op obj1; /* Same as operator op(obj1)*/ ……………….. }
  • 13. Sample Program #include<iostream.h> #include<conio.h> class increment { int m,n; public: increment(int x, int y) { m=x; n=y; } Compiled By: Kamal Acharya void display() { cout<<"m= "<<m<< "n="<<n<<endl; } friend void operator ++(increment&); };
  • 14. void operator ++(increment& x) { ++x.m; ++x.n; } void main() { clrscr(); increment in1(20,30); Compiled By: Kamal Acharya in1.display(); ++in1; in1.display(); increment in2(1,2); in2.display(); operator ++(in2); in2.display(); getch(); }
  • 16. Overloading Binary Operator  Binary operator are those operator which works on two operands. Eg. +, -,*,/ etc  Binary operator acts on two operands and can be overloaded in two ways: 1. 2. Using non-static member function with single argument. Using friend function with two arguments where the arguments must be either an object of the class or an reference to an object of the class. Compiled By: Kamal Acharya
  • 17. Using non static member function  Example class Test { ………… public: void operator op(Test) {……… } }; Compiled By: Kamal Acharya Void main() { ……. ……. obj1 op obj2; /* Same as obj1.operator op(obj2)*/ ……………….. }
  • 18. Sample Program #include<iostream.h> #include<conio.h> class add { int m,n; public: add(int x, int y) { m=x; n=y; } Compiled By: Kamal Acharya void display() { cout<<"m= "<<m<<" n="<<n<<endl; } void operator +(add); };
  • 19. void add::operator +(add x) { m=m+x.m; n=n+x.n; } Compiled By: Kamal Acharya void main() { clrscr(); add obj1(20,30),obj2(2,3); obj1.display(); obj2.display(); obj1+obj2; obj1.display(); getch(); }
  • 21. Using Friend Function  Example class Test { ………… public: friend void operator op(Test, Test); }; Compiled By: Kamal Acharya Void main() { ……. ……. obj1 op obj2; /* Same as operator op(obj1, obj2)*/ ……………….. }
  • 22. Sample Program #include<iostream.h> #include<conio.h> class add { int m,n; public: add(int x, int y) { m=x; n=y; } Compiled By: Kamal Acharya void display() { cout<<"m= "<<m<<" n="<<n<<endl; } friend void operator +(add&,add&); };
  • 23. void operator +(add& x, add& y) { x.m=x.m+y.m; x.n=x.n+y.n; } Compiled By: Kamal Acharya void main() { clrscr(); add obj1(20,30),obj2(2,3); obj1.display(); obj2.display(); obj1+obj2; obj1.display(); getch(); }