SlideShare a Scribd company logo
Operator Overloading
Overloading Unary Operators
// countpp1.cpp
// increment counter variable with ++ operator
#include <iostream>
using namespace std;
/////////////////////////////////////////////////////////////
///
class Counter
{
private:
unsigned int count; //count
public:
Counter() : count(0) //constructor
{ }
unsigned int get_count() //return count
{ return count; }
void operator ++ () //increment (prefix)
{
++count;
}
};
Keyword
Overloading Unary Operators
int main()
{
Counter c1, c2; //define and initialize
cout << “nc1=” << c1.get_count(); //display
cout << “nc2=” << c2.get_count();
++c1; //increment c1
++c2; //increment c2
++c2; //increment c2
cout << “nc1=” << c1.get_count(); //display again
cout << “nc2=” << c2.get_count() << endl;
return 0;
}
Output
c1=0 ← counts are initially 0
c2=0
c1=1 ← incremented once
c2=2 ←incremented twice
Overloading Unary Operators
Operator Return Values
void operator ++ () //increment (prefix)
{
++count;
}
Counter c1, c2;
c1 = ++c2; //
Error
Operator Return Values
class Counter
{
private:
unsigned int count; //count
public:
Counter() : count(0) //constructor
{ }
unsigned int get_count() //return count
{ return count; }
Counter operator ++ () //increment count
{
++count; //increment count
Counter temp; //make a temporary Counter
temp.count = count; //give it same value as this obj
return temp; //return the copy
}
};
Operator Return Values
int main()
{
Counter c1, c2; //c1=0, c2=0
cout << “nc1=” << c1.get_count(); //display
cout << “nc2=” << c2.get_count();
++c1; //c1=1
c2 = ++c1; //c1=2, c2=2
cout << “nc1=” << c1.get_count(); //display again
cout << “nc2=” << c2.get_count() << endl;
return 0;
}
Output
c1=0
c2=0
c1=2
c2=2
Nameless Temporary Objects
class Counter
{
private:
unsigned int count; //count
public:
Counter() : count(0) //constructor no args
{ }
Counter(int c) : count(c) //constructor, one arg
{ }
unsigned int get_count() //return count
{ return count; }
Counter operator ++ () //increment count
{
++count; // increment count, then return
return Counter(count); // an unnamed temporary object
} // initialized to this count
};
int main()
{
Counter c1, c2; //c1=0, c2=0
cout << “nc1=” << c1.get_count(); //display
cout << “nc2=” << c2.get_count();
++c1; //c1=1
c2 = ++c1; //c1=2, c2=2
cout << “nc1=” << c1.get_count(); //display again
cout << “nc2=” << c2.get_count() << endl;
return 0;
}
Output
c1=0
c2=0
c1=2
c2=2
Nameless Temporary Objects
class Counter
{
private:
unsigned int count; //count
public:
Counter() : count(0) //constructor no args
{ }
Counter(int c) : count(c) //constructor, one arg
{ }
unsigned int get_count() const //return count
{ return count; }
Counter operator ++ () //increment count (prefix)
{ //increment count, then return
return Counter(++count); //an unnamed temporary object
} //initialized to this count
Counter operator ++ (int) //increment count (postfix)
{ //return an unnamed temporary
return Counter(count++); //object initialized to this
} //count, then increment count
};
Postfix Notation
int main()
{
Counter c1, c2; //c1=0, c2=0
cout << “nc1=” << c1.get_count(); //display
cout << “nc2=” << c2.get_count();
++c1; //c1=1
c2 = ++c1; //c1=2, c2=2 (prefix)
cout << “nc1=” << c1.get_count(); //display
cout << “nc2=” << c2.get_count();
c2 = c1++; //c1=3, c2=2 (postfix)
cout << “nc1=” << c1.get_count(); //display again
cout << “nc2=” << c2.get_count() << endl;
return 0;
}
Postfix Notation
Output
c1=0
c2=0
c1=2
c2=2
c1=3
c2=2
Arithmetic Operators
class Distance //English Distance class
{
private: int feet; float inches;
public: //constructor (no args)
Distance() : feet(0), inches(0.0)
{ } //constructor (two args)
Distance(int ft, float in) : feet(ft), inches(in)
{ }
void getdist() //get length from user
{
cout << “nEnter feet: “; cin >> feet;
cout << “Enter inches: “; cin >> inches;
}
void showdist() const //display distance
{ cout << feet << “’-” << inches << ‘”’; }
Distance operator + ( Distance ); //add 2 distances
};
Overloading Binary Operators
Distance Distance::operator + (Distance d2) //return sum
{
int f = feet + d2.feet; //add the feet
float i = inches + d2.inches; //add the inches
if(i >= 12.0) //if total exceeds 12.0,
{ //then decrease inches
i -= 12.0; //by 12.0 and
f++; //increase feet by 1
} //return a temporary Distance
return Distance(f,i); //initialized to sum
}
Overloading Binary Operators
int main()
{
Distance dist1, dist3; //define distances
dist1.getdist(); //get dist1 from user
Distance dist2(11, 6.25); //define, initialize dist2
dist3 = dist1 + dist2; //single ‘+’ operator
//display all lengths
cout << “dist1 = “; dist1.showdist(); cout << endl;
cout << “dist2 = “; dist2.showdist(); cout << endl;
cout << “dist3 = “; dist3.showdist(); cout << endl;
return 0;
}
Overloading Binary Operators
Output
Enter feet: 10
Enter inches: 6.5
dist1 = 10’-6.5” ← from user
dist2 = 11’-6.25” ← initialized in program
dist3 = 22’-0.75” ← dist1+dist2
dist3 = dist1 + dist2;
Overloading Binary Operators
Comparison Operators
bool operator < (Distance d2)
{
float bf1 = feet + inches/12;
float bf2 = d2.feet + d2.inches/12;
return (bf1 < bf2) ? true : false;
}
Operator overloading restrictions
• The following operators cannot be overloaded
– the member access or dot operator (.)
– the scope resolution operator (::)
– the conditional operator (?:)
– the pointer-to-member operator (->)
• One can’t create new operators ,only existing
operators can be overloaded

More Related Content

PPTX
Object Oriented Programming using C++: Ch08 Operator Overloading.pptx
PPTX
operator overloading
PDF
Object Oriented Programming (OOP) using C++ - Lecture 3
PPT
Lecture#5 Operators in C++
PDF
c++ practical Digvajiya collage Rajnandgaon
DOC
Pads lab manual final
PDF
Object Oriented Programming (OOP) using C++ - Lecture 2
DOCX
Pratik Bakane C++
Object Oriented Programming using C++: Ch08 Operator Overloading.pptx
operator overloading
Object Oriented Programming (OOP) using C++ - Lecture 3
Lecture#5 Operators in C++
c++ practical Digvajiya collage Rajnandgaon
Pads lab manual final
Object Oriented Programming (OOP) using C++ - Lecture 2
Pratik Bakane C++

Similar to Mastering Operator Overloading in C++... (20)

PDF
C++ TUTORIAL 4
PDF
#ifndef RATIONAL_H   if this compiler macro is not defined #def.pdf
PPT
Class ‘increment’
PPTX
Functions in C++ programming language.pptx
PDF
C++ L05-Functions
DOCX
Assignement of programming & problem solving ass.(3)
PPTX
Operator overloading2
DOCX
PPTX
Object Oriented Programming using C++: Ch09 Inheritance.pptx
PPTX
pointers.pptx
PDF
C++ normal assignments by maharshi_jd.pdf
PPTX
Lec 2.pptx programing errors \basic of programing
PDF
Final DAA_prints.pdf
PDF
C++ TUTORIAL 2
PPTX
Pointers in c++ programming presentation
PDF
Chainer-Compiler 動かしてみた
DOCX
Tugas praktikukm pemrograman c++
PDF
Modify the Time classattached to be able to work with Date.pdf
C++ TUTORIAL 4
#ifndef RATIONAL_H   if this compiler macro is not defined #def.pdf
Class ‘increment’
Functions in C++ programming language.pptx
C++ L05-Functions
Assignement of programming & problem solving ass.(3)
Operator overloading2
Object Oriented Programming using C++: Ch09 Inheritance.pptx
pointers.pptx
C++ normal assignments by maharshi_jd.pdf
Lec 2.pptx programing errors \basic of programing
Final DAA_prints.pdf
C++ TUTORIAL 2
Pointers in c++ programming presentation
Chainer-Compiler 動かしてみた
Tugas praktikukm pemrograman c++
Modify the Time classattached to be able to work with Date.pdf
Ad

Recently uploaded (20)

PPTX
ognitive-behavioral therapy, mindfulness-based approaches, coping skills trai...
PPTX
2. Earth - The Living Planet earth and life
PDF
Formation of Supersonic Turbulence in the Primordial Star-forming Cloud
PPTX
2. Earth - The Living Planet Module 2ELS
PDF
VARICELLA VACCINATION: A POTENTIAL STRATEGY FOR PREVENTING MULTIPLE SCLEROSIS
PPTX
G5Q1W8 PPT SCIENCE.pptx 2025-2026 GRADE 5
PPTX
INTRODUCTION TO EVS | Concept of sustainability
PPTX
Comparative Structure of Integument in Vertebrates.pptx
PPTX
2Systematics of Living Organisms t-.pptx
PPTX
EPIDURAL ANESTHESIA ANATOMY AND PHYSIOLOGY.pptx
PDF
HPLC-PPT.docx high performance liquid chromatography
PPTX
TOTAL hIP ARTHROPLASTY Presentation.pptx
PPTX
Introduction to Fisheries Biotechnology_Lesson 1.pptx
PPTX
Cell Membrane: Structure, Composition & Functions
PPTX
The KM-GBF monitoring framework – status & key messages.pptx
PDF
ELS_Q1_Module-11_Formation-of-Rock-Layers_v2.pdf
DOCX
Q1_LE_Mathematics 8_Lesson 5_Week 5.docx
PDF
The scientific heritage No 166 (166) (2025)
PPTX
ECG_Course_Presentation د.محمد صقران ppt
PPTX
cpcsea ppt.pptxssssssssssssssjjdjdndndddd
ognitive-behavioral therapy, mindfulness-based approaches, coping skills trai...
2. Earth - The Living Planet earth and life
Formation of Supersonic Turbulence in the Primordial Star-forming Cloud
2. Earth - The Living Planet Module 2ELS
VARICELLA VACCINATION: A POTENTIAL STRATEGY FOR PREVENTING MULTIPLE SCLEROSIS
G5Q1W8 PPT SCIENCE.pptx 2025-2026 GRADE 5
INTRODUCTION TO EVS | Concept of sustainability
Comparative Structure of Integument in Vertebrates.pptx
2Systematics of Living Organisms t-.pptx
EPIDURAL ANESTHESIA ANATOMY AND PHYSIOLOGY.pptx
HPLC-PPT.docx high performance liquid chromatography
TOTAL hIP ARTHROPLASTY Presentation.pptx
Introduction to Fisheries Biotechnology_Lesson 1.pptx
Cell Membrane: Structure, Composition & Functions
The KM-GBF monitoring framework – status & key messages.pptx
ELS_Q1_Module-11_Formation-of-Rock-Layers_v2.pdf
Q1_LE_Mathematics 8_Lesson 5_Week 5.docx
The scientific heritage No 166 (166) (2025)
ECG_Course_Presentation د.محمد صقران ppt
cpcsea ppt.pptxssssssssssssssjjdjdndndddd
Ad

Mastering Operator Overloading in C++...

  • 2. Overloading Unary Operators // countpp1.cpp // increment counter variable with ++ operator #include <iostream> using namespace std; ///////////////////////////////////////////////////////////// /// class Counter { private: unsigned int count; //count public: Counter() : count(0) //constructor { } unsigned int get_count() //return count { return count; } void operator ++ () //increment (prefix) { ++count; } }; Keyword
  • 3. Overloading Unary Operators int main() { Counter c1, c2; //define and initialize cout << “nc1=” << c1.get_count(); //display cout << “nc2=” << c2.get_count(); ++c1; //increment c1 ++c2; //increment c2 ++c2; //increment c2 cout << “nc1=” << c1.get_count(); //display again cout << “nc2=” << c2.get_count() << endl; return 0; } Output c1=0 ← counts are initially 0 c2=0 c1=1 ← incremented once c2=2 ←incremented twice
  • 5. Operator Return Values void operator ++ () //increment (prefix) { ++count; } Counter c1, c2; c1 = ++c2; // Error
  • 6. Operator Return Values class Counter { private: unsigned int count; //count public: Counter() : count(0) //constructor { } unsigned int get_count() //return count { return count; } Counter operator ++ () //increment count { ++count; //increment count Counter temp; //make a temporary Counter temp.count = count; //give it same value as this obj return temp; //return the copy } };
  • 7. Operator Return Values int main() { Counter c1, c2; //c1=0, c2=0 cout << “nc1=” << c1.get_count(); //display cout << “nc2=” << c2.get_count(); ++c1; //c1=1 c2 = ++c1; //c1=2, c2=2 cout << “nc1=” << c1.get_count(); //display again cout << “nc2=” << c2.get_count() << endl; return 0; } Output c1=0 c2=0 c1=2 c2=2
  • 8. Nameless Temporary Objects class Counter { private: unsigned int count; //count public: Counter() : count(0) //constructor no args { } Counter(int c) : count(c) //constructor, one arg { } unsigned int get_count() //return count { return count; } Counter operator ++ () //increment count { ++count; // increment count, then return return Counter(count); // an unnamed temporary object } // initialized to this count };
  • 9. int main() { Counter c1, c2; //c1=0, c2=0 cout << “nc1=” << c1.get_count(); //display cout << “nc2=” << c2.get_count(); ++c1; //c1=1 c2 = ++c1; //c1=2, c2=2 cout << “nc1=” << c1.get_count(); //display again cout << “nc2=” << c2.get_count() << endl; return 0; } Output c1=0 c2=0 c1=2 c2=2 Nameless Temporary Objects
  • 10. class Counter { private: unsigned int count; //count public: Counter() : count(0) //constructor no args { } Counter(int c) : count(c) //constructor, one arg { } unsigned int get_count() const //return count { return count; } Counter operator ++ () //increment count (prefix) { //increment count, then return return Counter(++count); //an unnamed temporary object } //initialized to this count Counter operator ++ (int) //increment count (postfix) { //return an unnamed temporary return Counter(count++); //object initialized to this } //count, then increment count }; Postfix Notation
  • 11. int main() { Counter c1, c2; //c1=0, c2=0 cout << “nc1=” << c1.get_count(); //display cout << “nc2=” << c2.get_count(); ++c1; //c1=1 c2 = ++c1; //c1=2, c2=2 (prefix) cout << “nc1=” << c1.get_count(); //display cout << “nc2=” << c2.get_count(); c2 = c1++; //c1=3, c2=2 (postfix) cout << “nc1=” << c1.get_count(); //display again cout << “nc2=” << c2.get_count() << endl; return 0; } Postfix Notation Output c1=0 c2=0 c1=2 c2=2 c1=3 c2=2
  • 12. Arithmetic Operators class Distance //English Distance class { private: int feet; float inches; public: //constructor (no args) Distance() : feet(0), inches(0.0) { } //constructor (two args) Distance(int ft, float in) : feet(ft), inches(in) { } void getdist() //get length from user { cout << “nEnter feet: “; cin >> feet; cout << “Enter inches: “; cin >> inches; } void showdist() const //display distance { cout << feet << “’-” << inches << ‘”’; } Distance operator + ( Distance ); //add 2 distances }; Overloading Binary Operators
  • 13. Distance Distance::operator + (Distance d2) //return sum { int f = feet + d2.feet; //add the feet float i = inches + d2.inches; //add the inches if(i >= 12.0) //if total exceeds 12.0, { //then decrease inches i -= 12.0; //by 12.0 and f++; //increase feet by 1 } //return a temporary Distance return Distance(f,i); //initialized to sum } Overloading Binary Operators
  • 14. int main() { Distance dist1, dist3; //define distances dist1.getdist(); //get dist1 from user Distance dist2(11, 6.25); //define, initialize dist2 dist3 = dist1 + dist2; //single ‘+’ operator //display all lengths cout << “dist1 = “; dist1.showdist(); cout << endl; cout << “dist2 = “; dist2.showdist(); cout << endl; cout << “dist3 = “; dist3.showdist(); cout << endl; return 0; } Overloading Binary Operators Output Enter feet: 10 Enter inches: 6.5 dist1 = 10’-6.5” ← from user dist2 = 11’-6.25” ← initialized in program dist3 = 22’-0.75” ← dist1+dist2
  • 15. dist3 = dist1 + dist2; Overloading Binary Operators
  • 16. Comparison Operators bool operator < (Distance d2) { float bf1 = feet + inches/12; float bf2 = d2.feet + d2.inches/12; return (bf1 < bf2) ? true : false; }
  • 17. Operator overloading restrictions • The following operators cannot be overloaded – the member access or dot operator (.) – the scope resolution operator (::) – the conditional operator (?:) – the pointer-to-member operator (->) • One can’t create new operators ,only existing operators can be overloaded