More Related Content Similar to Operator Overloading (20) More from Sardar Alam (17) Operator Overloading 2. Stream InsertionStream Insertion
operatoroperator
►Often we need to display the dataOften we need to display the data
on the screenon the screen
►Example:Example:
int i=1, j=2;int i=1, j=2;
cout << “i= ”<< i << “n”;cout << “i= ”<< i << “n”;
Cout << “j= ”<< j << “n”;Cout << “j= ”<< j << “n”;
3. Stream InsertionStream Insertion
operatoroperator
Complex c1;Complex c1;
cout << c1;cout << c1;
cout << c1 << 2;cout << c1 << 2;
// Compiler error: binary '<<' : no// Compiler error: binary '<<' : no
operator // defined which takes a right-operator // defined which takes a right-
hand // operand of typehand // operand of type ‘‘classclass
8. Stream InsertionStream Insertion
operatoroperator
class Complex{class Complex{
......
friend ostream & operator <<friend ostream & operator <<
(ostream & os, const Complex(ostream & os, const Complex
& c);& c);
};}; Note: this object
is NOT const
Note: return type
is NOT const
9. Stream InsertionStream Insertion
operatoroperator
// we want the output as:// we want the output as: (real, img)(real, img)
ostream & operator << (ostream &ostream & operator << (ostream &
os, const Complex & c){os, const Complex & c){
os << ‘(‘ << c.realos << ‘(‘ << c.real
<< ‘,‘<< ‘,‘
<< c.img << ‘)’;<< c.img << ‘)’;
return os;return os;
}}
15. Stream ExtractionStream Extraction
OperatorOperator
►Main Program:Main Program:
Complex c1(1.01, 20.1);Complex c1(1.01, 20.1);
cin >> c1;cin >> c1;
// suppose we// suppose we
entered // 1.0025 forentered // 1.0025 for
c1.real and // 0.0241c1.real and // 0.0241
for c1.imgfor c1.img
cout << c1;cout << c1;
17. Other BinaryOther Binary
operatorsoperators►Overloading comparison operators:Overloading comparison operators:
class Complex{class Complex{
public:public:
bool operator == (const Complex & c);bool operator == (const Complex & c);
//friend bool operator == (const//friend bool operator == (const
//Complex & c1, const Complex & c2);//Complex & c1, const Complex & c2);
bool operator != (const Complex & c);bool operator != (const Complex & c);
//friend bool operator != (const//friend bool operator != (const
//Complex & c1, const Complex & c2);//Complex & c1, const Complex & c2);
……
};};
18. Other BinaryOther Binary
operatorsoperatorsbool Complex::operator ==(constbool Complex::operator ==(const
Complex & c){Complex & c){
ifif(((real == c.real) &&(real == c.real) &&
(img == c.img)(img == c.img))){{
return true;return true;
}}
elseelse
return false;return false;
}}
19. Other BinaryOther Binary
operatorsoperatorsbool operator ==(constbool operator ==(const
Complex& lhs, const Complex& rhs){Complex& lhs, const Complex& rhs){
ifif(((lhs.real == rhs.real) &&(lhs.real == rhs.real) &&
(lhs.img == rhs.img)(lhs.img == rhs.img))){{
return true;return true;
}}
elseelse
return false;return false;
}}
20. Other BinaryOther Binary
operatorsoperatorsbool Complex::operator !=(constbool Complex::operator !=(const
Complex & c){Complex & c){
ifif(((real != c.real) ||(real != c.real) ||
(img != c.img)(img != c.img))){{
return true;return true;
}}
elseelse
return false;return false;
}}