C++ Lecture 24
C++ Lecture 24
02
LECTURE 24
04
Today’s Agenda
01 Overloading of Binary Operator
Practical Examples
04
05 Assignment
05
05
Overloading of Binary Operators
Output
Output
D4 = D1 + D2 + D3;
D1.operator+(D2) + D3
Temp + D3
Temp.operator+(D3);
Temp + D3
How the following line is actually handled by the compiler?
cout<<a;
cout.operator<<(a);
How the following line is actually handled by the compiler?
int a;
cin>>a;
cin.operator>>(a);
Overloading Relational Operators
Comparing 2 objects of
class Distance
feet 1
D1
inches 0
feet 0
D2
inches 12
Overloading Relational Operators
class Distance int Distance::operator==(const Distance & Q)
{ {
private: int x, y;
int feet, inches; x = feet * 12 + inches;
public: y = Q.feet * 12 + Q.inches;
void get() if(x == y)
{ return 1;
cout<<"Enter feet and inches: "; else
cin>>feet>>inches; return 0;
} }
void show() int main() if(D1.operator == (D2))
{ {
cout<<feet<<", "<<inches<<endl; Distance D1, D2;
} D1.get();
int operator==(const Distance &); D2.get();
}; D1.show();
D2.show();
if(D1 == D2)
cout<<"Distances are equal"<<endl;
else
cout<<"Distances are not equal"<<endl;
return 0;
}
Output of The Previous Code
Output
Thank you