Object-Oriented ProgrammingObject-Oriented Programming
(OOP)(OOP)
Lecture No. 19Lecture No. 19
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”;
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
Stream InsertionStream Insertion
operatoroperator
class Complex{class Complex{
……
public:public:
……
void operator << (constvoid operator << (const
Complex & rhs);Complex & rhs);
};};
Stream InsertionStream Insertion
operatoroperator
int main(){int main(){
Complex c1;Complex c1;
cout << c1;cout << c1; // Error// Error
c1 << cout;c1 << cout;
c1 << cout << 2; // Errorc1 << cout << 2; // Error
return 0;return 0;
};};
Stream InsertionStream Insertion
operatoroperator
class Complex{class Complex{
……
public:public:
……
void operator << (ostream &);void operator << (ostream &);
};};
Stream InsertionStream Insertion
operatoroperator
void Complex::operator <<void Complex::operator <<
(ostream & os){(ostream & os){
osos << ‘(‘ << real<< ‘(‘ << real
<< ‘,’ << img << ‘)’;<< ‘,’ << img << ‘)’;
}}
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
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;
}}
Stream InsertionStream Insertion
operatoroperator
Complex c1(1.01, 20.1),Complex c1(1.01, 20.1),
c2(0.01, 12.0);c2(0.01, 12.0);
cout << c1 << endl << c2;cout << c1 << endl << c2;
Stream InsertionStream Insertion
operatoroperator
Output:Output:
( 1.01 , 20.1 )( 1.01 , 20.1 )
( 0.01 , 12.0 )( 0.01 , 12.0 )
Stream InsertionStream Insertion
operatoroperator
cout << c1 << c2;cout << c1 << c2;
is equivalent tois equivalent to
operator<<(operator<<(
operator<<(cout,c1)operator<<(cout,c1),c2);,c2);
Stream ExtractionStream Extraction
OperatorOperator
►OverloadingOverloading ““>>>>”” operator:operator:
class Complex{class Complex{
......
friend istream & operatorfriend istream & operator
>> (istream & i, Complex &>> (istream & i, Complex &
c);c);
};};
Note: this object
is NOT const
Stream ExtractionStream Extraction
OperatorOperator
istream & operator << (istreamistream & operator << (istream
& in, Complex & c){& in, Complex & c){
in >> c.real;in >> c.real;
in >> c.img;in >> c.img;
return in;return in;
}}
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;
Stream ExtractionStream Extraction
OperatorOperator
Output:Output:
( 1.0025 , 0.0241 )( 1.0025 , 0.0241 )
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);
……
};};
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;
}}
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;
}}
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;
}}

Operator Overloading