Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Programming Lecture 34. In Today’s Lecture Arrays of objects Arrays of objects Interaction of Arrays with Free Store Interaction of Arrays.

Similar presentations


Presentation on theme: "Introduction to Programming Lecture 34. In Today’s Lecture Arrays of objects Arrays of objects Interaction of Arrays with Free Store Interaction of Arrays."— Presentation transcript:

1 Introduction to Programming Lecture 34

2 In Today’s Lecture Arrays of objects Arrays of objects Interaction of Arrays with Free Store Interaction of Arrays with Free Store new and delete operators new and delete operators Overloading of Arrays Operator Overloading of Arrays Operator

3 Arrays of Object

4 Date mydate [ 10 ] ;

5 int intArray [ 10 ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } ;

6 Date mydate [ 10 ] = { Date ( 21, 01, 1979 ), Date ( 21, 02, 1979 ), Date ( 21, 03, 1979 ), Date ( 21, 04, 1979 ), Date ( 21, 05, 1979 ), Date ( 02, 06, 1979 ), Date ( 02, 07, 1979 ), Date ( 02, 08, 1979 ), Date ( 02, 09, 1979 ), Date ( 02, 10, 1979 ) };

7 Example Fill the array for ( int i = 0 ; i < arraySize ; i ++ ) { cin >> c [ i ] ; }

8 Example Print in Reverse order for ( int i = arraySize ; i >= 0 ; i -- ) { cout << c [ i ] ; } Incorrect code

9 Example Print in Reverse order for ( int i = ( arraySize – 1 ) ; i >= 0 ; i -- ) { cout << c [ i ] ; }

10 Example void Date :: display ( ) { char * monthName [ ] = { "zero", "January", "February", "March", "April", "May", "zero", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "June", "July", "August", "September", "October", "November", "December" "November", "December" } ; } ; cout << monthName [ month ] << ' ' << day << ", " << year << endl ; cout << monthName [ month ] << ' ' << day << ", " << year << endl ;}

11 Date mydate [ 10 ] = { Date ( 21, 01, 1979 ), Date ( “01-Jan-2002” ), Date ( 21, 03, 1979 ), Date ( “01-Feb-2002” ), Date ( 21, 05, 1979 ), Date ( 02, 06, 1979 ), Date ( 02, 07, 1979 ), Date ( 02, 08, 1979 ), Date ( 02, 09, 1979 ), Date ( 02, 10, 1979 ) };

12 String myString [ 5 ] = { "First line of message\n", "Second line of message\n", "First line of message\n", "Second line of message\n", String ( "Third line of message\n" ), String ( "Third line of message\n" ), String ( '-', 25 ), String ( '-', 25 ), String ( ) String ( ) } ; } ;

13 String * text ; text = new String [ 5 ] ;

14 String myString [ 5 ] = { "First line of message\n", "Second line of message\n", String ( "Third line of message\n" ), String( '-', 25 ), String ( ) };

15 Default Constructor

16 String * text ; text = new String [ 5 ] ; delete text ; // Incorrect syntax for // deleting array

17 delete [ ] text ;

18 int * iPtr ; iPtr = new int [ 10 ] ; delete iPtr ;// bad usage Example

19 delete [ ] iPtr ; delete [ ] text ;

20 Overloading new Operator void * operator new ( size_t size ) { void * rtn = calloc ( 1, size ) ; void * rtn = calloc ( 1, size ) ; return rtn ; return rtn ;}

21 Overloading delete Operator void operator delete ( void * ptr ) { free ( ptr ) ; free ( ptr ) ;}

22 void * Date :: operator new ( size_t size ) ; Overloading new Operator for Date class

23 int iArray [ 10 ] ; int i ; for ( i = 0 ; i < 100 ; i ++ ) { iArray [ i ] = i ; } Example

24 int iArray [ 10 ] ; int i ; for ( i = 0; i < upperLimit ; i ++) { iArray [ i ] = i ; } Example

25 [ ]

26 int iArray [ 5 ] ;

27 int & operator [ ] ( int index ) ;

28 #define MAXNUM 10 int iArray [ MAXNUM ] ;

29 Overloaded Array Operator [ ] int & IntArray :: operator [ ] ( int index ) { int dummy = 0 ; if ( (index > ( MAXNUM – 1 ) ) if ( (index > ( MAXNUM – 1 ) ){ cout << "Error: invalid index call.\n“ ; cout << "Error: invalid index call.\n“ ; return dummy ; return dummy ;}else return array [ index ] ; }

30 Example class IntArray { private : int length ; int length ; int * iPtr ; int * iPtr ; public : IntArray ( int i ) ; } ;

31 main ( ) { IntArray i ( 10 ) ; IntArray i ( 10 ) ; int j ; int j ; for ( j = 0 ; j < 10 ; j ++ ) for ( j = 0 ; j < 10 ; j ++ ) i [ j ] = j ; i [ j ] = j ;} Example

32 Example int & IntArray ::operator [ ] ( int index ) { int dummy = 0 ; if ( ( index = length ) ) if ( ( index = length ) ){ cout << "Error : index out of range.\n" ; cout << "Error : index out of range.\n" ; return dummy ; return dummy ;}else return array [ index ] ; return array [ index ] ;}

33 IntArray :: IntArray ( int i ) { int * iPtr ; iPtr = new int ( i ) ; length = i ; } Example

34 Today’s lecture Arrays of objects Arrays of objects Dynamically allocating arrays using new operator Dynamically allocating arrays using new operator Usages and overloading new and delete Operator Usages and overloading new and delete Operator Array subscripting [ ] Operator Array subscripting [ ] Operator


Download ppt "Introduction to Programming Lecture 34. In Today’s Lecture Arrays of objects Arrays of objects Interaction of Arrays with Free Store Interaction of Arrays."

Similar presentations


Ads by Google