CS201-34 - Introduction to Programming Lecture 34 In...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 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 ] ; 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 ) }; Fill the array for ( int i = 0 ; i < arraySize ; i ++ ) { cin >> c [ i ] ; } Example Print in Reverse order Example for ( int i = arraySize ; i >= 0 ; i ­­ ) { cout << c [ i ] ; } Incorrect code Print in Reverse order Example 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 ; 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 ( ) }; Default Constructor String * text ; text = new String [ 5 ] ; delete text ; // Incorrect syntax for // deleting array delete [ ] text ; Example int * iPtr ; iPtr = new int [ 10 ] ; delete iPtr ;// bad usage delete [ ] iPtr ; delete [ ] text ; 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 ) ; } Overloading new Operator for Date class void * Date :: operator new ( size_t size ) ; 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 ; } Example [ ] int iArray [ 5 ] ; int & operator [ ] ( int index ) ; #define MAXNUM 10 int iArray [ MAXNUM ] ; 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 ) ; ... } ; main ( ) { IntArray i ( 10 ) ; int j ; Example for ( j = 0 ; j < 10 ; j ++ ) i [ j ] = j ; } 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 ; } Example Arrays of objects Today’s lecture Dynamically allocating arrays using new operator Usages and overloading new and delete Operator Array subscripting [ ] Operator ...
View Full Document

This note was uploaded on 01/12/2010 for the course CS CS 201 taught by Professor Dr.naveedmalik during the Spring '09 term at Virtual University of Pakistan.

Ask a homework question - tutors are online