340 likes | 360 Views
Introduction to Programming . Lecture 34. In Today’s Lecture. Arrays of objects Interaction of Arrays with Free Store new and delete operators Overloading of Arrays Operator. Arrays of Object. Date mydate [ 10 ] ;.
E N D
Introduction to Programming Lecture 34
In Today’s Lecture • Arrays of objects • Interaction of Arrays with Free Store • new and delete operators • Overloading of Arrays Operator
int intArray [ 10 ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } ;
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 ) };
Example Fill the array for ( int i = 0 ; i < arraySize ; i ++ ) { cin >> c [ i ] ; }
Example Print in Reverse order for ( int i = arraySize ; i >= 0 ; i -- ) { cout << c [ i ] ; } Incorrect code
Example Print in Reverse order for ( int i = ( arraySize – 1 ) ; i >= 0 ; i -- ) { cout << c [ i ] ; }
Example void Date :: display ( ) { char * monthName [ ] = { "zero", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November" , "December" } ; cout << monthName [ month ] << ' ' << day << ", " << year << endl ; }
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 ) };
String myString [ 5 ] = { "First line of message\n" , "Second line of message\n", String ( "Third line of message\n" ) , String ( '-' , 25 ) , String ( ) } ;
String * text ; text = new String [ 5 ] ;
String myString [ 5 ] = { "First line of message\n", "Second line of message\n", String ( "Third line of message\n" ) , String( '-', 25 ), String ( ) };
String * text ; text = new String [ 5 ] ;delete text ; // Incorrect syntax for // deleting array
Example int * iPtr ; iPtr = new int [ 10 ] ; delete iPtr ;// bad usage
Overloading new Operator void * operator new ( size_t size ) { void * rtn = calloc ( 1 , size ) ; return rtn ; }
Overloading delete Operator void operator delete ( void * ptr ) { free ( ptr ) ; }
void * Date :: operator new ( size_t size ) ; Overloading new Operator for Date class
Example int iArray [ 10 ] ; int i ; for ( i = 0 ; i < 100 ; i ++ ) { iArray [ i ] = i ; }
Example int iArray [ 10 ] ; int i ; for ( i = 0; i < upperLimit ; i ++) { iArray [ i ] = i ; }
Overloaded Array Operator [ ] int & IntArray :: operator [ ] ( int index ) { int dummy = 0 ; if ( (index > ( MAXNUM – 1 ) ) { cout << "Error: invalid index call.\n“ ; return dummy ; } else return array [ index ] ; }
Example class IntArray { private : int length ; int * iPtr ; public : IntArray ( int i ) ; ... } ;
Example main ( ) { IntArray i ( 10 ) ; int j ; for ( j = 0 ; j < 10 ; j ++ ) i [ j ] = j ; }
Example int & IntArray ::operator [ ] ( int index ) { int dummy = 0 ; if ( ( index < 0 ) || ( index >= length ) ) { cout << "Error : index out of range.\n" ; return dummy ; } else return array [ index ] ; }
Example IntArray :: IntArray ( int i ) { int * iPtr ; iPtr = new int ( i ) ; length = i ; }
Today’s lecture • Arrays of objects • Dynamically allocating arrays using new operator • Usages and overloading new and delete Operator • Array subscripting [ ] Operator