34
Introduction to Programmi Introduction to Programmi Lecture 34 Lecture 34

CS201- Introduction to Programming- Lecture 34

Embed Size (px)

DESCRIPTION

Virtual University Course CS201- Introduction to Programming Lecture No 34 Instructor's Name: Dr. Naveed A. Malik Course Email: [email protected]

Citation preview

Page 1: CS201- Introduction to Programming- Lecture 34

Introduction to ProgrammingIntroduction to Programming

Lecture 34Lecture 34

Page 2: CS201- Introduction to Programming- Lecture 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

Page 3: CS201- Introduction to Programming- Lecture 34

Arrays of Arrays of ObjectObject

Page 4: CS201- Introduction to Programming- Lecture 34

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

Page 5: CS201- Introduction to Programming- Lecture 34

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

} ; } ;

Page 6: CS201- Introduction to Programming- Lecture 34

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 )

};};

Page 7: CS201- Introduction to Programming- Lecture 34

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 ] ;

}}

Page 8: CS201- Introduction to Programming- Lecture 34

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

Page 9: CS201- Introduction to Programming- Lecture 34

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 ] ;}}

Page 10: CS201- Introduction to Programming- Lecture 34

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 ;}}

Page 11: CS201- Introduction to Programming- Lecture 34

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 ) )

};};

Page 12: CS201- Introduction to Programming- Lecture 34

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 ( )

} ;} ;

Page 13: CS201- Introduction to Programming- Lecture 34

String * text ;String * text ;

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

Page 14: CS201- Introduction to Programming- Lecture 34

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 ( )

};};

Page 15: CS201- Introduction to Programming- Lecture 34

Default Default ConstructoConstructo

rr

Page 16: CS201- Introduction to Programming- Lecture 34

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

// Incorrect syntax for// deleting array

Page 17: CS201- Introduction to Programming- Lecture 34

delete [ ] delete [ ] text ;text ;

Page 18: CS201- Introduction to Programming- Lecture 34

int * iPtr ;int * iPtr ;

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

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

ExampleExample

Page 19: CS201- Introduction to Programming- Lecture 34

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

Page 20: CS201- Introduction to Programming- Lecture 34

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 ;

}}

Page 21: CS201- Introduction to Programming- Lecture 34

Overloading delete Overloading delete OperatorOperator

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

{{

free ( ptr ) ;free ( ptr ) ;

}}

Page 22: CS201- Introduction to Programming- Lecture 34

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

Overloading new Overloading new

Operator for Operator for DateDate classclass

Page 23: CS201- Introduction to Programming- Lecture 34

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

Page 24: CS201- Introduction to Programming- Lecture 34

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

Page 25: CS201- Introduction to Programming- Lecture 34

[ ][ ]

Page 26: CS201- Introduction to Programming- Lecture 34

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

Page 27: CS201- Introduction to Programming- Lecture 34

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

Page 28: CS201- Introduction to Programming- Lecture 34

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

Page 29: CS201- Introduction to Programming- Lecture 34

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 ] ; }}

Page 30: CS201- Introduction to Programming- Lecture 34

Example Example class IntArrayclass IntArray

{{

private :private :

int length ;int length ;

int * iPtr ;int * iPtr ;

public :public :

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

......

} ;} ;

Page 31: CS201- Introduction to Programming- Lecture 34

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

Page 32: CS201- Introduction to Programming- Lecture 34

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 ] ; }}

Page 33: CS201- Introduction to Programming- Lecture 34

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

Page 34: CS201- Introduction to Programming- Lecture 34

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