Title: Computer Notes - Stream Insertion operator
1Stream Insertion operator
http//ecomputernotes.com
2Stream Insertion operator
- Often we need to display the data on the screen
- Example
- int i1, j2
- cout ltlt i ltlt i ltlt \n
- Cout ltlt j ltlt j ltlt \n
http//ecomputernotes.com
3Stream Insertion operator
- Complex c1
- cout ltlt c1
- cout ltlt c1 ltlt 2
- // Compiler error binary 'ltlt' no operator //
defined which takes a right-hand //
operand of type class Complex
4Stream Insertion operator
- class Complex
-
- public
-
- void operator ltlt (const
- Complex rhs)
http//ecomputernotes.com
5Stream Insertion operator
- int main()
- Complex c1
- cout ltlt c1 // Error
- c1 ltlt cout
- c1 ltlt cout ltlt 2 // Error
- return 0
http//ecomputernotes.com
6Stream Insertion operator
- class Complex
-
- public
-
- void operator ltlt (ostream )
http//ecomputernotes.com
7Stream Insertion operator
- void Complexoperator ltlt (ostream os)
- os ltlt ( ltlt real
- ltlt , ltlt img ltlt )
http//ecomputernotes.com
8Stream Insertion operator
- class Complex
- ...
- friend ostream operator ltlt (ostream os, const
Complex c)
Note return type is NOT const
Note this object is NOT const
http//ecomputernotes.com
9Stream Insertion operator
- // we want the output as (real, img)
- ostream operator ltlt (ostream os, const
Complex c) - os ltlt ( ltlt c.real
- ltlt ,
- ltlt c.img ltlt )
- return os
http//ecomputernotes.com
10Stream Insertion operator
- Complex c1(1.01, 20.1), c2(0.01, 12.0)
- cout ltlt c1 ltlt endl ltlt c2
http//ecomputernotes.com
11Stream Insertion operator
- Output
- ( 1.01 , 20.1 )
- ( 0.01 , 12.0 )
http//ecomputernotes.com
12Stream Insertion operator
- cout ltlt c1 ltlt c2
- is equivalent to
-
- operatorltlt(
- operatorltlt(cout,c1),c2)
-
-
http//ecomputernotes.com
13Stream Extraction Operator
- Overloading gtgt operator
- class Complex
- ...
- friend istream operator gtgt (istream i,
Complex c)
Note this object is NOT const
14Stream Extraction Operator
- istream operator ltlt (istream in, Complex
c) - in gtgt c.real
- in gtgt c.img
- return in
http//ecomputernotes.com
15Stream Extraction Operator
- Main Program
- Complex c1(1.01, 20.1)
- cin gtgt c1
- // suppose we entered // 1.0025 for c1.real
and // 0.0241 for c1.img - cout ltlt c1
http//ecomputernotes.com
16Stream Extraction Operator
- Output
- ( 1.0025 , 0.0241 )
http//ecomputernotes.com
17Other Binary operators
- Overloading comparison operators
- class Complex
- public
- bool operator (const Complex c)
- //friend bool operator (const //Complex c1,
const Complex c2) - bool operator ! (const Complex c)
- //friend bool operator ! (const //Complex c1,
const Complex c2) -
18Other Binary operators
- bool Complexoperator (const Complex c)
- if((real c.real)
- (img c.img))
- return true
-
- else
- return false
http//ecomputernotes.com
19Other Binary operators
- bool operator (const
- Complex lhs, const Complex rhs)
- if((lhs.real rhs.real)
- (lhs.img rhs.img))
- return true
-
- else
- return false
http//ecomputernotes.com
20Other Binary operators
- bool Complexoperator !(const Complex c)
- if((real ! c.real)
- (img ! c.img))
- return true
-
- else
- return false
http//ecomputernotes.com