SlideShare a Scribd company logo
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

More Related Content

PPTX
Operator overloadng
PPTX
Operator overloading
PPTX
Operator overloading and type conversion in cpp
PPTX
OPERATOR OVERLOADING IN C++
PPTX
Operator overloading
PPT
Lec 26.27-operator overloading
PPTX
Operator overloading
PPTX
Bca 2nd sem u-4 operator overloading
Operator overloadng
Operator overloading
Operator overloading and type conversion in cpp
OPERATOR OVERLOADING IN C++
Operator overloading
Lec 26.27-operator overloading
Operator overloading
Bca 2nd sem u-4 operator overloading

What's hot (20)

PPT
Operator overloading
PPT
Operator Overloading
PPTX
Unary operator overloading
PPTX
PPT
Operator overloading
PPTX
Operator overloading and type conversions
PPTX
Operator overloading
PPT
14 operator overloading
PPT
08 c++ Operator Overloading.ppt
PPT
C++ overloading
PPTX
Presentation on overloading
PPTX
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
PPTX
operator overloading
PPT
Operator overloading
PPTX
Operator overloading
PPT
Operator overloading in C++
PDF
Operator overloading in C++
PPTX
operator overloading & type conversion in cpp over view || c++
PDF
Operator_Overloaing_Type_Conversion_OOPC(C++)
Operator overloading
Operator Overloading
Unary operator overloading
Operator overloading
Operator overloading and type conversions
Operator overloading
14 operator overloading
08 c++ Operator Overloading.ppt
C++ overloading
Presentation on overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
operator overloading
Operator overloading
Operator overloading
Operator overloading in C++
Operator overloading in C++
operator overloading & type conversion in cpp over view || c++
Operator_Overloaing_Type_Conversion_OOPC(C++)
Ad

Viewers also liked (18)

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

Similar to Operator overloading (20)

PPTX
Operator overloaing
PDF
Object Oriented Programming using C++ - Part 3
PPTX
PDF
Operator Overloading Introduction and Concept of OV
PPTX
Week7a.pptx
PPTX
Mca 2nd sem u-4 operator overloading
PPT
Polymorphism and function overloading_new.ppt
PDF
Ch-4-Operator Overloading.pdf
PPTX
Object Oriented Programming using C++: Ch08 Operator Overloading.pptx
PPTX
3. Polymorphism.pptx
PPTX
Cpp (C++)
PPTX
Operator Overloading
PPT
Unary operator overloading
PPT
Lec 28 - operator overloading
PDF
Operator overloading C++
PDF
Polymorphism and Type Conversion.pdf pot
PPTX
B.sc CSIT 2nd semester C++ Unit4
PDF
NIKUL SURANI
PDF
22 scheme OOPs with C++ BCS306B_module3.pdf
PPT
Mastering Operator Overloading in C++...
Operator overloaing
Object Oriented Programming using C++ - Part 3
Operator Overloading Introduction and Concept of OV
Week7a.pptx
Mca 2nd sem u-4 operator overloading
Polymorphism and function overloading_new.ppt
Ch-4-Operator Overloading.pdf
Object Oriented Programming using C++: Ch08 Operator Overloading.pptx
3. Polymorphism.pptx
Cpp (C++)
Operator Overloading
Unary operator overloading
Lec 28 - operator overloading
Operator overloading C++
Polymorphism and Type Conversion.pdf pot
B.sc CSIT 2nd semester C++ Unit4
NIKUL SURANI
22 scheme OOPs with C++ BCS306B_module3.pdf
Mastering Operator Overloading in C++...

More from Kamal Acharya (20)

PPTX
Programming the basic computer
PPTX
Computer Arithmetic
PPTX
Introduction to Computer Security
PPTX
Session and Cookies
PPTX
Functions in php
PPTX
Web forms in php
PPTX
Making decision and repeating in PHP
PPTX
Working with arrays in php
PPTX
Text and Numbers (Data Types)in PHP
PPTX
Introduction to PHP
PPTX
Capacity Planning of Data Warehousing
PPTX
Data Warehousing
PPTX
Search Engines
PPTX
Web Mining
PPTX
Information Privacy and Data Mining
PPTX
Cluster Analysis
PPTX
Association Analysis in Data Mining
PPTX
Classification techniques in data mining
PPTX
Data Preprocessing
PPTX
Introduction to Data Mining and Data Warehousing
Programming the basic computer
Computer Arithmetic
Introduction to Computer Security
Session and Cookies
Functions in php
Web forms in php
Making decision and repeating in PHP
Working with arrays in php
Text and Numbers (Data Types)in PHP
Introduction to PHP
Capacity Planning of Data Warehousing
Data Warehousing
Search Engines
Web Mining
Information Privacy and Data Mining
Cluster Analysis
Association Analysis in Data Mining
Classification techniques in data mining
Data Preprocessing
Introduction to Data Mining and Data Warehousing

Recently uploaded (20)

PDF
Computing-Curriculum for Schools in Ghana
PDF
01-Introduction-to-Information-Management.pdf
PPTX
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Yogi Goddess Pres Conference Studio Updates
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
What if we spent less time fighting change, and more time building what’s rig...
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PPTX
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Classroom Observation Tools for Teachers
PPTX
Cell Structure & Organelles in detailed.
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Cell Types and Its function , kingdom of life
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Computing-Curriculum for Schools in Ghana
01-Introduction-to-Information-Management.pdf
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
Anesthesia in Laparoscopic Surgery in India
Microbial diseases, their pathogenesis and prophylaxis
Yogi Goddess Pres Conference Studio Updates
Weekly quiz Compilation Jan -July 25.pdf
Complications of Minimal Access Surgery at WLH
What if we spent less time fighting change, and more time building what’s rig...
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Paper A Mock Exam 9_ Attempt review.pdf.
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
Final Presentation General Medicine 03-08-2024.pptx
2.FourierTransform-ShortQuestionswithAnswers.pdf
Classroom Observation Tools for Teachers
Cell Structure & Organelles in detailed.
Module 4: Burden of Disease Tutorial Slides S2 2025
Cell Types and Its function , kingdom of life
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx

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(); }