Академический Документы
Профессиональный Документы
Культура Документы
Lecture 34
In Today’s Lecture
Arrays of objects
Interaction of Arrays with Free Store
}
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