Вы находитесь на странице: 1из 34

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


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 ;
}
[]
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 ( )
Example
{
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

Вам также может понравиться