I have a programming project due for my object orientated programming class, where I have to create a class the handles dynamically adding and deleting items from a template array. The class has a private data member that is a pointer to an array of type template.
I understand ok how this works with integers.
int * intarray;
intarray = new int [5];
but can you do the same thing this way?
T * rubberarray;
rubberarray = new T [5];
??
If I need to do a member by member copy from one array to another, is it the same syntax as with any other type?
In general, I am really unfamiliar with templates at all, as they weren't covered in any depth in my previous programming classes. Below is the instructions for my project. If anyone could help me with the syntax of templates that would help a lot.
For this project you will create a templateclass RubberArray. It will be a fully functional type. It will contain the following:
// Constructors and Destructor
RubberArray ( int i = 0 );
// i is the virtual index of the first element in your RubberArray
RubberArray( const T*, unsigned s, int i = 0 );
// s is the number of items in the T* bult-in array
// i is the virtual index of the first element in your RubberArray
RubberArray( const RubberArray& );
~RubberArray( );
// Assignment
RubberArray& operator= ( const RubberArray& );
// Insert the array or a pointer to an array into an out stream
friend ostream& operator<< ( ostream&, const RubberArray& );
friend ostream& operator<< ( ostream&, const RubberArray* );
// access the item at index 0<=i<length (assuming standard indexing)
T& operator[] ( int i );
const T& operator[] ( int i ) const;
// Returns a sub array that begins at index first and goes through to less than index last
RubberArray operator( ) ( int first, int last )const;
// Add either a new item or another RubberArray beginning at index length
void add ( const T& );
void add ( const RubberArray& );
// return the number of items in the array
unsigned length ( ) const
// Delete the item at index 0<=i<length (assuming standard indexing)
void dele( int i );
// Delete the items from index first and goes through to less than index last
void dele ( int first, int last );
// Insert at index index 0<=i<length (assuming standard indexing)
void insert ( const T&, int i );
void insert ( int i, const T& );
// Insert at index 0 (assuming standard indexing)
void insert ( const T& );
You may find it easier to have all of the code forthisclass in one file instead of two files as is usual. Templates work a little differently than regular classes.
This array must grow and shrink as items are added and deleted from it. Start with no allocated space. When you need space, add a CHUNK of space to what you have. When you have one CHUNK of unused space, dealloacte one CHUNK of the space. Store CHUNK as a global const in your file. For testing, make it a small enough number that you are sure to be testing the expansion and contraction fo your array
The standard indexing for your array will be zero but this can be set in the constructor.
Save your code in one file named RubberArray.h
Submit your code to the D2L dropbox before it closes
In general, yes it's all the same. (the prototypes may be a little more cumbersome)
However what you do with your templatized objets will set restrictions to the classes that can be used with that template.
By instance:T * rubberarray = new T [5]; is asumming that your class has a default constructor.