Recommended
PPTX
Object Oriented Programming using C++: Ch08 Operator Overloading.pptx
PPTX
PPTX
PPT
Lec 26.27-operator overloading
PPT
Unary operator overloading
PDF
PPT
Lec 28 - operator overloading
PPTX
PDF
Ch-4-Operator Overloading.pdf
PDF
Basics _of_Operator Overloading_Somesh_Kumar_SSTC
PPTX
Bca 2nd sem u-4 operator overloading
PDF
PDF
Polymorphism and Type Conversion.pdf pot
PPTX
Mca 2nd sem u-4 operator overloading
PDF
Object Oriented Programming using C++ - Part 3
PPT
PPTX
PDF
22 scheme OOPs with C++ BCS306B_module3.pdf
PPTX
PPT
PPT
OOP OOOOOverloading Concept lecture12.ppt
PPT
operator overloading concept in oops lecture12.ppt
PPT
PPT
Operator overloading in c++ is the most required.
PPT
Binary operator overloading
PPT
PPTX
PPTX
PDF
Possible identification of the Luna 9 Moon landing site using a novel machine...
PDF
Polymer [ बहुलक ] Chemistry Notes PDF - Irfanullah Mehar - JJ Sir Chemistry.pdf
More Related Content
PPTX
Object Oriented Programming using C++: Ch08 Operator Overloading.pptx
PPTX
PPTX
PPT
Lec 26.27-operator overloading
PPT
Unary operator overloading
PDF
PPT
Lec 28 - operator overloading
PPTX
Similar to Mastering Operator Overloading in C++...
PDF
Ch-4-Operator Overloading.pdf
PDF
Basics _of_Operator Overloading_Somesh_Kumar_SSTC
PPTX
Bca 2nd sem u-4 operator overloading
PDF
PDF
Polymorphism and Type Conversion.pdf pot
PPTX
Mca 2nd sem u-4 operator overloading
PDF
Object Oriented Programming using C++ - Part 3
PPT
PPTX
PDF
22 scheme OOPs with C++ BCS306B_module3.pdf
PPTX
PPT
PPT
OOP OOOOOverloading Concept lecture12.ppt
PPT
operator overloading concept in oops lecture12.ppt
PPT
PPT
Operator overloading in c++ is the most required.
PPT
Binary operator overloading
PPT
PPTX
PPTX
Recently uploaded
PDF
Possible identification of the Luna 9 Moon landing site using a novel machine...
PDF
Polymer [ बहुलक ] Chemistry Notes PDF - Irfanullah Mehar - JJ Sir Chemistry.pdf
PPTX
Antigens, Haptens and epitopes in immunology.pptx
PPTX
Gene Editing_ Rewriting the Code of Life.pptx
PPTX
PHIVOLCS FAULT FINDER grade - 7 power point
PPT
CLASSIFICATION-OF-POISONS.ppt ,
PDF
Virtualization 1: Virtual Machine, Hypervisor (Virtual Machine Monitor)
PPT
Thermodynamics Chapter-2 by Cengel and Boles
PPT
Electric Fields and capacitance AQA A-level Physics
PPTX
Digestive system grade 5 Cambridge basic sicence
PPTX
Cell Transport Powerpoint.pptx for high school students
PDF
Rapid diagnosis and contamination of seafood and aquaculture products
PPTX
Mount and repair a fishing net for fishi
PPTX
Grade 9 - Science Fourth Quarter Physics
PPTX
HUBUNGAN EKOLOGI DENGAN KONSERVASI .pptx
PPTX
Cell cycle ( mitosis and meiosis)Msc.pptx
PPTX
OECD 204 (Acute Dermal Toxicity) .pptx
PPTX
Anaerobic respiration (Fermentation, Types, Pasteur effect, Warburg effect, R...
PPTX
Mass selection is one of the oldest methods of plant breeding Commonly use_20...
PPTX
Chimeric Antigen Receptors T-cell (CAR-T) & Bispecific T- cell engager (BiTEs)
Mastering Operator Overloading in C++... 1. 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
4. 5. 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. 16. 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