Operator
Overloading
Operator Overloading
● Consider the following examples
[Link](d1, d2); or
d3 = [Link](d2);
can be changed to the much more readable
d3 = d1 + d2;
● The term operator overloading refers to giving the normal C++
operators, such as +, *, <=, and +=, additional meanings when they
are applied to user-defined data types
2
Operator Overloading Cont..
● Another kind of operation, data type conversion, is closely connected
with operator overloading
● C++ handles the conversion of simple types, such as int and float,
automatically, but conversions involving user-defined types require
some work on the programmer’s part
3
Restriction on Operator
Overloading
● With operator overloading we cannot change
○ How operators act on native data types
■ Cannot change integer addition
○ Precedence of operator
■ Use parentheses to force order-of-operations
○ Associativity (left-to-right or right-to-left)
○ Number of operands
■ = is binary, only acts on two operands
● Cannot create new operators
● Operators must be overloaded explicitly
○ Overloading + does not overload +=
4
Operator Overloading Cont..
5
Overloading Unary Operators
● Consider an scenario in which you are required to increment Private
data member
class Counter int main(){
{ Counter c1;
private: cout << “c1=” <<
int count; c1.get_count();
public: c1.inc_count();
Counter() : count(0) {} c1.inc_count();
void inc_count() { count++; } c1.inc_count();
int get_count() { return cout << “c1=” <<
count; } c1.get_count();
}; cout << endl;
}
6
Overloading Unary Operators
Cont..
7
Another Example
class Counter{ int main(){
private: Counter c1, c2;
int count; cout << “c1=” << c1.get_count();
public: cout << “c2=” << c2.get_count();
Counter() : count(0) { } ++c1;
int get_count() { return c2 = ++c1;
count; } cout << “c1=” << c1.get_count();
Counter operator ++ (){ cout << “c2=” << c2.get_count() <<
++count; endl;
Counter temp; }
[Link] = count; Output
return temp; c1= 0
} c2= 0
}; c1= 2
c2= 2
8
Overloading Unary Operator
Cont..
• We have used
■++c1
• But will this statement works?
■c1++
9
Overloading Unary Operator
Cont..
class Counter void main()
{ {
private: Counter c1, c2;
int count; cout << “c1=” <<
public: c1.get_count();
Counter() : count(0){ } cout << “c2=” <<
Counter(int c) : count(c) { } c2.get_count();
int get_count() const { ++c1;
return count; }
c2 = ++c1;
cout << “c1=” <<
Counter operator ++ () {
c1.get_count();
return Counter(++count); }
cout << “c2=” <<
Counter operator ++ (int){
c2.get_count();
return Counter(count++); }
c2 = c1++;
}; cout << “c1=” <<
c1.get_count();
cout << “c2=” << c2.get_count() 10
<< endl;
Overloading Unary Operator
Cont..
Output
c1= 0
c2= 0
c1= 2
c2= 2
c1= 3
c2= 2
11
Overloading Unary Operator
Cont..
Overload – operator in a distance class so that it can change distance
into negative
e.g. if input is feet = 10, inch = -5, the result should be feet = -10 inch = 5.
class Distance{ void displayDistance()
private: {
int feet, inches; cout << "F: " << feet << " I:" <<
inches
public: }
Distance(){ Distance operator- ()
feet = 0; {
inches = 0; feet = -feet;
} inches = -inches;
Distance(int f, int i){ return Distance(feet, inches);
feet = f; }
inches = i; };
} 12
Overloading Unary Operator
Cont..
int main()
{
Distance D1(11, 10), D2(-5,
11);
-D1;
[Link]();
-D2;
[Link]();
return 0;
}
13
Overloading Binary Operators
• Suppose we are to convert
dist3.add_dist(dist1, dist2);
To
dist3 = dist1 + dist2;
14
Overloading Binary Operators
Cont..
class Distance { void Distance::add_dist(Distance d2,
private: Distance d3)
{
int feet;
inches = [Link] + [Link];
float inches; feet = 0;
public: if(inches >= 12.0) {
Distance() : feet(0), inches(0.0) { } inches -= 12.0;
Distance(int ft, float in) : feet(ft), inches(in) { }
void getdist(){ feet++;
}
cout << “\nEnter feet: “; cin >> feet;
feet += [Link] + [Link];
cout << “Enter inches: “; cin >> inches; }
}
void showdist()
{cout << feet << “\’-” << inches << ‘\”’;}
void add_dist( Distance, Distance ){};
};
15
Overloading Binary Operators
Cont..
void main(){
Distance dist1, dist3;
Output
Distance dist2(11, 6.25);
[Link](); Enter feet: 17
Enter inches: 5.75
dist3.add_dist(dist1, dist2);
dist1 = 17’-5.75”
cout << “\ndist1 = “; [Link](); dist2 = 11’-6.25”
cout << “\ndist2 = “; [Link](); dist3 = 29’-0”
cout << “\ndist3 = “; [Link]();
cout << endl;
}
16
Overloading Binary Operators
Cont..
class Distance { Distance Distance::operator +
private: (Distance d2)
int feet; {
float inches; int f = feet + [Link];
public: float i = inches + [Link];
Distance() : feet(0), inches(0.0) { } if(i >= 12.0)
Distance(int ft, float in) : feet(ft), inches(in) { } {
void getdist(){
i -= 12.0;
cout << “\nEnter feet: “; cin >> feet;
f++;
cout << “Enter inches: “; cin >> inches;
}
}
void showdist()
return Distance(f,i);
{cout << feet << “\’-” << inches << ‘\”’;} }
void add_dist( Distance, Distance ){};
Distance operator + ( Distance ){};
};
17
Overloading Binary Operators
Cont..
void main(){
Distance dist1, dist3, dist4;
[Link]();
Distance dist2(11, 6.25);
dist3 = dist1 + dist2;
dist4 = dist1 + dist2 + dist3;
cout << “dist1 = “; [Link](); cout <<
endl;
cout << “dist2 = “; [Link](); cout <<
endl;
cout << “dist3 = “; [Link](); cout <<
endl;
cout << “dist4 = “; [Link](); cout <<
endl;
}
18
Overloading Binary Operators
Cont..
•Write a Box class that contains Length, width and height data
members. Implement following functions
■ Double getVolume()
■ Double setLenght(double)
■ Double setWidth(double)
■ Double setHeight(double)
• Overload + operator so that volume of 2 boxes can be added
• Now overload < operator to determine which box is larger
19
Overloading Assignment
Operator
■ The assignment operator is used to copy the values
from one object to another already existing object
Distance dist2(dist1);Assignment or not?
Distance dist3 = dist1;
20
Overloading Assignment
Operator Cont..
Distance d3;
Distance dist2(dist1);
Distance dist3 = dist1;
• Syntax
Return_Type operator = (const Class_Name &)
21
Overloading Assignment
Operator
class Marks{
Cont.. void Display() {
cout<<"Marks in 1st
Subject:"<<m1;
private: cout<<"Marks in 2nd
Subject:"<<m2;
int m1, m2; }
void main(){
public: };
Marks Mark1(45, 89);
Marks() {m1 = 0; m2 = 0;} Marks Mark2(36, 59);
cout << " Marks of first student :
Marks(int i, int j) {m1 = i; m2";= j;}
[Link]();
void operator=(const Marks &M ){ cout << " Marks of Second
m1 = M.m1; student :";
[Link]();
m2 = M.m2; Mark1 = Mark2;
cout << " Mark in 1st Subject :";
} [Link]();
} 22
Overloading Assignment
Operator Cont..
• Implement a distance class which contains inches and feet as private
data and also contains
○ Copy constructor
○ Overloaded assignment operator
○ Display function before and after assignment operator
23
Overloading [] Operator
class safearay{ int main() {
private: safearay A;
int arr[10]; cout << "Value of A[2] : " <<
public:
A[2];
cout << "Value of A[5] : " <<
safearay() {
A[5];
int i;
cout << "Value of A[12]: " <<
for(i = 0; i < 10; i++){ A[12]; return 0;
arr[i] = i; } }
}
int operator[](int i){
if( i > 10) {
cout << "Index out of bounds“;
return arr[0];
}
else
return arr[i]; }
};
24
Overloading input/output
Operator
int x;
cout<< x;
But if x is an object then
cout<< x // wrong
• Syntax
○ friend ostream &operator<<( ostream &out, const ReturnType & var )
similarly
○ friend istream &operator>>( istream &in, const ReturnType & var )
25
Overloading input/output
Operator Cont..
• Implement distance class that contains inches and feet as private
member. Overload << and >> to input and display the objects data
class Distance{ friend ostream &operator<<( ostream
private: &output,const Distance &D ){
int feet; output << "F : " << [Link] << " I :
int inches; " << [Link];
public: return output;
Distance(){ }
feet = 0; friend istream &operator>>( istream &input,
inches = 0; Distance &D ) {
} input >> [Link] >> [Link];
Distance(int f, int i){ return input;
feet = f; }
inches = i; };
}
26
Overloading input/output
Operator Cont..
void main(){
Distance D1(11, 10), D2(5, 11), D3;
cout << "Enter the value of object : " << endl;
cin >> D3;
cout << "First Distance : " << D1 << endl;
cout << "Second Distance :" << D2 << endl;
cout << "Third Distance :" << D3 << endl;
}
27
Overloading input/output
Operator Cont..
• Overload stream extraction and stream insertion operator so that
user is able input price of computer and display it
28