Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Programming

Similar presentations


Presentation on theme: "Introduction to Programming"— Presentation transcript:

1 Introduction to Programming
Lecture 33

2 In Today’s Lecture Operator overloading Assignment operator
A “this” pointer Operator over loading using this pointer Conversion function

3 Assignment Operator

4 a = b ;

5 Member Wise Assignment

6 Member Wise Copy

7 Example class String { char buf [ 100 ] ; public: String ( ) ;
... } ;

8 Example main ( ) { String s1 ( “This is a test” ) ; String s2 ;
s2 = s1 ; }

9 s2 = s1 ;

10 Example void String :: operator = ( String & s1 ) { delete buf ;
buf = new char [ s1.buf + 1 ] ; strcpy ( buf , s1.buf ) ; }

11 Assignment Operator int i , j , k ; i = 5 ; j = 10 ; k = 15 ; i = j ;

12 i = j = k ; k = i = j ;

13 k = i = ++ j ;

14 this pointer

15 this pointer buf ; this -> buf ; ( * this ).buf ;

16 int i ; i = i ; // nothing happens

17 String s [ 1000 ] = { “This is a Test” } ;
s = s ;

18 Self Assignment

19 Example main ( ) { String s , * sPtr ; sPtr = & s ; . s = * sPtr ; }

20 Example void String :: operator= ( String & other ) {
if ( this == & other ) return ; }

21 Example String & String :: operator = ( String & other ) {
if ( this == & other ) return * this ; delete buf ; buf = new char [ other.length + 1 ] ; strcpy ( buf , other.buf ) ; }

22 s3 = s2 = s1 ;

23 cout << a << b << c ;

24 Example Date d1 , d2 ; d2 = d1 ++ ; d2 = d1 + 1 ;

25 Example Date date1 , date2 ; date2 = date1 + 1 ; date2 = date1 ++ ;

26 Example main ( ) { int i ; float x ; x = i ; i = x ; }

27 Example main ( ) { Date d ; int i ; d = i ; } ?

28 Example main ( ) { int i ; float x ; x = ( float ) i ; }

29 Conversion Function

30 Date ( ) { // body of function }

31 double x = 1 / 3 ; Output: x * 3 ; Output:

32 Common Business Oriented Language


Download ppt "Introduction to Programming"

Similar presentations


Ads by Google