Lab 6
Lab 6
Operator Overloading
1
Table of Contents
• Operators in C++
• Why we need Operator Overloading?
• Operator Overloading Definition
• Defining Operator Overloading in C++
• Overloading Unary Operators
• Postfix and Prefix Notation
• Overloading Binary Operators
• TRY IT YOURSELF
2
❑ Operators in c++
❑ Why we need Operator Overloading?
Add two int numbers Add two strings Add two Objects
} } }
operator overloading refers to giving the normal C++ operators, such as +, *, <=, and +=, additional meanings
when they are applied to user-defined data types.
Normally
a = b + c;
works only with basic types such as int and float, and attempting to apply it when a, b, and c
are objects of a user-defined class will cause complaints from the compiler. However, using
overloading, you can make this statement legal even when a, b, and c are user-defined types.
❑ Defining Operator Overloading in C++
class Counter{
private:
unsigned int count; //count
public: int main()
Counter() : count(0){} //constructor {
Counter(int val): count(val){} //constructor Counter c1, c2; //c1=0, c2=0
cout << "\nc1="<< c1.get_count(); // 0
unsigned int get_count(){ //return count cout << "\nc2="<< c2.get_count(); // 0
return count; ++c1; //c1=1
} c2 = ++c1; //c1=2, c2=2
Counter operator ++ () { //increment count cout << "\n="<< c1.get_count(); // 2
count++; //increment count cout << "\n="<< c2.get_count() << endl; // 2
Counter temp; //make a temporary Counter return 0;
temp.count = count; //give it same value as this obj }
return temp; //return the copy
}
};
int main()
class Counter { {
private: Counter c1, c2;
unsigned int count; //count cout << "\nc1="<< c1.get_count();
public: cout << "\nc1="<< c2.get_count();
Counter() : count(0) //constructor no args ++c1;
{ } c2 = ++c1;
Counter(int c) : count(c) //constructor, one arg cout << "\nc1="<< c1.get_count(); //display
{ } cout << "\nc2="<< c2.get_count();
unsigned int get_count() const //return count c2 = c1++;
{ return count; } cout << "\nc1="<< c1.get_count(); //display again
Counter operator ++ () //increment count (prefix) cout << "\nc2="<< c2.get_count() << endl;
{ //increment count, then return return 0;
return Counter(++count); //an unnamed temporary object }
} //initialized to this count What is the output of this program?
Counter operator ++ (int) //increment count (postfix) c1=0
{ //return an unnamed temporary
c2=0
return Counter(count++); //object initialized to this
} //count, then increment count c1=2
}; c2=2
c1=3
c2=2
8
❑ Overloading Binary Operators Add two objects
#include<iostream>
using namespace std;
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 ) const; //add 2 distances
};
9
❑ Overloading Binary Operators Add two objects
10
❑ Overloading Binary Operators Add two objects
int main()
{
Distance dist1, dist3, dist4; //define distances
dist1.getdist(); //get dist1 from user
Distance dist2(11, 6.25); //define, initialize dist2
dist3 = dist1 + dist2; //single ‘+’ operator
dist4 = dist1 + dist2 + dist3; //multiple ‘+’ operators
// display all lengths
cout << "dist1 = ";
dist1.showdist();
cout << endl;
cout << "dist2 = ";
dist2.showdist();
cout << endl;
cout << "dist3 = ";
dist3.showdist();
cout << endl;
cout << "dist4 = ";
dist4.showdist();
cout << endl;
return 0;
}
11
TRY IT YOURSELF
12
TRY IT YOURSELF
solution
#include<iostream>
using namespace std;
class Distance { //English Distance class
private:
int feet;
float inches;
public:
Distance() : feet(0), inches(0.0) { } //constructor (no args)
Distance(int ft, float in) : feet(ft), inches(in) { } //constructor (two args)
void getdist(){ //get length from user
cout << "\nEnter feet: ";
cin >> feet;
cout << "Enter inches: ";
cin >> inches;
}
void showdist() const { //display distance
cout << feet << "\'-" << inches << '\"';
}
bool operator < ( Distance ) const;
};
13
TRY IT YOURSELF
solution
14
TRY IT YOURSELF
solution int main()
{
Distance dist1; //define Distance dist1
dist1.getdist(); //get dist1 from user
Distance dist2(6, 2.5); //define and initialize dist2
//display distances
cout << "\ndist1 = "; dist1.showdist();
cout << "\ndist2 = "; dist2.showdist();
if( dist1 < dist2 ) //overloaded ‘< operator
cout<< "\ndist1 is less than dist2";
else
cout << "\ndist1 is greater than (or equal to) dist2";
cout << endl;
return 0;
}
15
THANK YOU
16