Introduction to Programming - PowerPoint PPT Presentation

About This Presentation
Title:

Introduction to Programming

Description:

Title: Introduction tp Programming Author: Saba Khalil Last modified by: umer.farooq Created Date: 11/21/2002 6:34:05 AM Document presentation format – PowerPoint PPT presentation

Number of Views:62
Avg rating:3.0/5.0
Slides: 24
Provided by: Saba164
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Programming


1
Introduction to Programming
  • Lecture 37

2
Operator Overloading
3
Stream Insertion / Extraction
Operator Overloading
4
  • int i
  • cin gtgt i

5
  • int a , b , c
  • a b c

6
  • cin gtgt i gtgt j

7
Friend Function
8
Member Operators
9
Object.dataObject.function ( )
10
Stream Insertion Operator
11
  • int i 5 , j 10 , k 15
  • cout ltlt i ltlt j ltlt k

12
ostream operator ltlt ( ostream output ,
vehicle A )
Return type Reference to the output stream
Operator


Reference to the output stream
Object of the class
13
Definition
  • ostream operator ltlt ( ostream output ,
    vehicle d )
  • outputltlt d.seats
  • outputltltd.tires
  • -------
  • return output

14
  • cout ltlt d

15
cout ltlt The description of the vehicle is \n ltlt
d
16
Example
  • class Matrix
  • private
  •           int rows , cols           int
    elements 3 3
  • public
  • Matrix ( int rows 3 , int cols 3 )
  •   friend ostream operator ltlt ( ostream
    output , Matrix m )

17
Example
  • ostream operator ltlt ( ostream output , Matrix
    m )
  • for ( int i 0 i lt m.rows i )   
            for ( int j 0 j lt m.cols j
    )                  output ltlt m.elements i
    j                 return output

18
  • int i
  • cin gtgt i

19
  • Matrix x
  • cin gtgt x

20
Example
  • class Matrix
  • private
  • int rows, cols
  • int elements 3 3
  • public
  • Matrix ( int rows 3 , int cols 3 )
  • friend ostream operator ltlt ( ostream output
    , Matrix m )
  •   friend istream operator gtgt ( istream
    input , Matrix m )

21
Example
  • istream operator gtgt ( istream input , Matrix
    m ) coutltlt Please enter the values of
    the matrix
  •     for ( int i 0 i lt m.rows i )   
            for ( int j 0 j lt m.cols j
    )                  cout ltlt Please enter
    the values of elements ltlt i ltlt , ltlt j
  • input gtgt m.elements i j
                    return input

22
Example
  • Matrix m
  • m.getMatrix ( )
  • m.displayMatrix ( )

23
Example
  • Matrix m
  • cin gtgt m
  • cout ltlt m
Write a Comment
User Comments (0)
About PowerShow.com