Computer Notes - Stream Insertion operator - PowerPoint PPT Presentation

About This Presentation
Title:

Computer Notes - Stream Insertion operator

Description:

- Computer Notes - Stream Insertion operator in Object oriented Programming what is Stream Insertion operator Explain about it in detail .explain it with example – PowerPoint PPT presentation

Number of Views:265

less

Transcript and Presenter's Notes

Title: Computer Notes - Stream Insertion operator


1
Stream Insertion operator
http//ecomputernotes.com
2
Stream 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
3
Stream 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

4
Stream Insertion operator
  • class Complex
  • public
  • void operator ltlt (const
  • Complex rhs)

http//ecomputernotes.com
5
Stream Insertion operator
  • int main()
  • Complex c1
  • cout ltlt c1 // Error
  • c1 ltlt cout
  • c1 ltlt cout ltlt 2 // Error
  • return 0

http//ecomputernotes.com
6
Stream Insertion operator
  • class Complex
  • public
  • void operator ltlt (ostream )

http//ecomputernotes.com
7
Stream Insertion operator
  • void Complexoperator ltlt (ostream os)
  • os ltlt ( ltlt real
  • ltlt , ltlt img ltlt )

http//ecomputernotes.com
8
Stream 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
9
Stream 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
10
Stream Insertion operator
  • Complex c1(1.01, 20.1), c2(0.01, 12.0)
  • cout ltlt c1 ltlt endl ltlt c2

http//ecomputernotes.com
11
Stream Insertion operator
  • Output
  • ( 1.01 , 20.1 )
  • ( 0.01 , 12.0 )

http//ecomputernotes.com
12
Stream Insertion operator
  • cout ltlt c1 ltlt c2
  • is equivalent to
  • operatorltlt(
  • operatorltlt(cout,c1),c2)

http//ecomputernotes.com
13
Stream Extraction Operator
  • Overloading gtgt operator
  • class Complex
  • ...
  • friend istream operator gtgt (istream i,
    Complex c)

Note this object is NOT const
14
Stream Extraction Operator
  • istream operator ltlt (istream in, Complex
    c)
  • in gtgt c.real
  • in gtgt c.img
  • return in

http//ecomputernotes.com
15
Stream 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
16
Stream Extraction Operator
  • Output
  • ( 1.0025 , 0.0241 )

http//ecomputernotes.com
17
Other 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)

18
Other Binary operators
  • bool Complexoperator (const Complex c)
  • if((real c.real)
  • (img c.img))
  • return true
  • else
  • return false

http//ecomputernotes.com
19
Other 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
20
Other Binary operators
  • bool Complexoperator !(const Complex c)
  • if((real ! c.real)
  • (img ! c.img))
  • return true
  • else
  • return false

http//ecomputernotes.com
Write a Comment
User Comments (0)
About PowerShow.com