CS201- Introduction to Programming- Lecture 34

Preview:

DESCRIPTION

Virtual University Course CS201- Introduction to Programming Lecture No 34 Instructor's Name: Dr. Naveed A. Malik Course Email: cs201@vu.edu.pk

Citation preview

Introduction to ProgrammingIntroduction to Programming

Lecture 34Lecture 34

In Today’s In Today’s LectureLecture

Arrays of objectsArrays of objects Interaction of Arrays with Free Interaction of Arrays with Free

StoreStore new and delete operatorsnew and delete operators Overloading of Arrays OperatorOverloading of Arrays Operator

Arrays of Arrays of ObjectObject

Date mydate [ 10 ] ;Date mydate [ 10 ] ;

int intArray [ 10 ] = { 1 , 2 , 3 , 4 , int intArray [ 10 ] = { 1 , 2 , 3 , 4 , 5 , 5 ,

6 , 7 , 8 , 9 , 6 , 7 , 8 , 9 , 10 10

} ; } ;

Date mydate [ 10 ]Date mydate [ 10 ] = = {{ Date ( 21 , 01 , 1979 ) Date ( 21 , 01 , 1979 )

,, Date ( 21 , 02 , 1979 ) Date ( 21 , 02 , 1979 )

,, Date ( 21 , 03 , 1979 ) Date ( 21 , 03 , 1979 )

,, Date ( 21 , 04 , 1979 ) Date ( 21 , 04 , 1979 )

,, Date ( 21 , 05 , 1979 ) Date ( 21 , 05 , 1979 )

,, Date ( 02 , 06 , 1979 ) Date ( 02 , 06 , 1979 )

,, Date ( 02 , 07 , 1979 Date ( 02 , 07 , 1979

) ,) , Date ( 02 , 08 , 1979 ) Date ( 02 , 08 , 1979 )

,, Date ( 02 , 09 , 1979 ) Date ( 02 , 09 , 1979 )

,, Date ( 02 , 10 , 1979 ) Date ( 02 , 10 , 1979 )

};};

Example Example Fill the arrayFill the array

for ( int i = 0 ; i < arraySize ; i +for ( int i = 0 ; i < arraySize ; i ++ )+ )

{{

cin >> c [ i ] ;cin >> c [ i ] ;

}}

ExampleExamplePrint in Reverse order Print in Reverse order

for ( int i = arraySize ; i >= 0 ; i for ( int i = arraySize ; i >= 0 ; i -- )-- )

{{

cout << c [ i ] ;cout << c [ i ] ;

}} Incorrect code

ExampleExamplePrint in Reverse order Print in Reverse order

for ( int i = ( arraySize – 1 ) ; i >= 0 ; i -- )for ( int i = ( arraySize – 1 ) ; i >= 0 ; i -- ){{

cout << c [ i ] ;cout << c [ i ] ;}}

Example Example void Date :: display ( )void Date :: display ( ){{

char * monthName [ ] =char * monthName [ ] = {{ "zero", "January", "February", "March", "April", "zero", "January", "February", "March", "April",

"May", "May", "June", "July", "August", "September", "October", "June", "July", "August", "September", "October", "November" , "December""November" , "December" } ;} ; cout << monthName [ month ] << ' ' << day << ", " << cout << monthName [ month ] << ' ' << day << ", " <<

year << endl ;year << endl ;}}

Date mydate [ 10 ]Date mydate [ 10 ] = ={{ Date ( 21 , 01 , Date ( 21 , 01 , 1979 ) , 1979 ) ,

Date ( “01-Jan- Date ( “01-Jan-2002” ) ,2002” ) ,

Date ( 21 , 03 , 1979 Date ( 21 , 03 , 1979 ) ,) ,

Date ( “01-Feb- Date ( “01-Feb-2002” ),2002” ),

Date ( 21 , 05 , 1979 ) Date ( 21 , 05 , 1979 ) ,,

Date ( 02 , 06 , 1979 Date ( 02 , 06 , 1979 ) ,) ,

Date ( 02 , 07 , 1979 ) Date ( 02 , 07 , 1979 ) ,,

Date ( 02 , 08 , 1979 ) Date ( 02 , 08 , 1979 ) ,,

Date ( 02 , 09 , 1979 ) Date ( 02 , 09 , 1979 ) ,,

Date ( 02 , 10 , 1979 Date ( 02 , 10 , 1979 ) )

};};

String myString [ 5 ] = { String myString [ 5 ] = {

"First line of message\n" , "First line of message\n" , "Second line of message\ "Second line of message\

n",n",

String ( "Third line of String ( "Third line of message\n" ) , message\n" ) ,

String ( '-' , 25 ) ,String ( '-' , 25 ) ,

String ( ) String ( )

} ;} ;

String * text ;String * text ;

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

String myString [ 5 ] = {String myString [ 5 ] = { "First line of message\n", "First line of message\n",

"Second line of message\n", "Second line of message\n", String ( "Third line of String ( "Third line of

message\ message\n" ) ,n" ) , String( '-', 25 ), String( '-', 25 ), String ( ) String ( )

};};

Default Default ConstructoConstructo

rr

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

// Incorrect syntax for// deleting array

delete [ ] delete [ ] text ;text ;

int * iPtr ;int * iPtr ;

iPtr = new int [ 10 ] ;iPtr = new int [ 10 ] ;

delete iPtr ;delete iPtr ;// bad usage// bad usage

ExampleExample

delete [ ] iPtr ;delete [ ] iPtr ;delete [ ] delete [ ] text ;text ;

Overloading new Overloading new OperatorOperator

void * operator new ( size_t size )void * operator new ( size_t size )

{{

void * rtn = calloc ( 1 , size ) ;void * rtn = calloc ( 1 , size ) ;

return rtn ;return rtn ;

}}

Overloading delete Overloading delete OperatorOperator

void operator delete ( void * ptr void operator delete ( void * ptr ))

{{

free ( ptr ) ;free ( ptr ) ;

}}

void * Date :: operator new ( size_t size ) ;void * Date :: operator new ( size_t size ) ;

Overloading new Overloading new

Operator for Operator for DateDate classclass

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

iArray [ i ] = i ;iArray [ i ] = i ;}}

ExampleExample

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

iArray [ i ] = i ;iArray [ i ] = i ;}}

ExampleExample

[ ][ ]

int iArray [ 5 ] int iArray [ 5 ] ;;

int & operator [ ] ( int int & operator [ ] ( int index ) ;index ) ;

#define MAXNUM 10#define MAXNUM 10int iArray int iArray [ MAXNUM ] ;[ MAXNUM ] ;

Overloaded Array Operator Overloaded Array Operator [ ] [ ] int & IntArray :: operator [ ] ( int index )int & IntArray :: operator [ ] ( int index )

{{int dummy = 0 ;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 else

return array [ index ] ;return array [ index ] ; }}

Example Example class IntArrayclass IntArray

{{

private :private :

int length ;int length ;

int * iPtr ;int * iPtr ;

public :public :

IntArray ( int i ) ;IntArray ( int i ) ;

......

} ;} ;

main ( )main ( ){{ IntArray i ( 10 ) ;IntArray i ( 10 ) ; int j ;int j ;

for ( j = 0 ; j < 10 ; j ++ )for ( j = 0 ; j < 10 ; j ++ ) i i [ j ] = j ; [ j ] = j ;

}}

Example Example

Example Example int & IntArray ::operator [ ] ( int index )int & IntArray ::operator [ ] ( int index ){{

int dummy = 0 ;int dummy = 0 ; if ( ( index < 0 ) || ( index >= length ) )if ( ( index < 0 ) || ( index >= length ) )

{{ cout << "Error : index out of range.\n" ;cout << "Error : index out of range.\n" ;

return dummy ;return dummy ;}}elseelse

return array [ index ] ; return array [ index ] ; }}

IntArray :: IntArray ( int i )IntArray :: IntArray ( int i )

{{

int * iPtr ;int * iPtr ;

iPtr = new int ( i ) ;iPtr = new int ( i ) ;

length = i ;length = i ;

}}

Example Example

Today’s lectureToday’s lecture Arrays of objectsArrays of objects Dynamically allocating arrays using Dynamically allocating arrays using

new operatornew operator Usages and overloading new and Usages and overloading new and

delete Operatordelete Operator Array subscripting [ ] OperatorArray subscripting [ ] Operator