This code is from my text book it shows how to implement code that is embedded I have modified it somewhat but I was wondering how I could get the user to implement the size of the array and enter the integers with the size of array that was implemented. Thanks
#include<iostream>
#include<cstdlib>
#include<iomanip>
usingnamespace std;
template<class T>
class SimpleVector
{
private:
T *aptr;//a pointer to a specific template data type
int arraySize;//array size
void subError();//subscript out of range
public:
SimpleVector(int);
SimpleVector(const SimpleVector &);//copy constructor
~SimpleVector();
int size()
{
return arraySize;
}
T &operator[](int);//overloaded [] operator
void print();//outputs the array elements
void getElementAt();
};
//*********************************************
//constructor for simple vector class. set the size
//of the array and allocates memory for it
//*********************************************
template<class T>
SimpleVector<T>::SimpleVector(int s)
{
arraySize = s;
aptr = new T [s];
for(int count = 0; count < arraySize; count++)
aptr[count] = T();
}
//***********************************************
//copy constructor for SimpleVector class
//***********************************************
template<class T>
SimpleVector<T>::SimpleVector(const SimpleVector &obj)
{
arraySize = obj.arraySize;
aptr = new T[arraySize];
for(int count = 0; count < arraySize; count++)
aptr[count] = obj[count];
}
//***********************************************
//Destructor for SimpleVector class
//***********************************************
template<class T>
SimpleVector<T>::~SimpleVector()
{
if(arraySize > 0)
delete []aptr;
}
//***********************************************
//sub error function. Display an error messages and
//terminates the program when subscript is out of
//range
//***********************************************
template<class T>
void SimpleVector<T>::subError()
{
cout<< " Error: Subscript out of range.\n";
exit(0);
}
//************************************************
//overloaded [] operator. The argumentis the subscript
//This function returns a reference to the element
//in the array indexed by the subscript
//************************************************
template<class T>
T &SimpleVector<T>::operator[](int sub)
{
if (sub < 0 || sub >= arraySize)
subError();
return aptr[sub];
}
//**************************************************
//prints all the entries in the array
//**************************************************
template<class T>
void SimpleVector<T>::print()
{
for(int k = 0; k < arraySize; k++)
cout<<aptr[k] << " ";
cout<<endl;
}
int main()
{
constint SIZE = 0;
char keepGoing;//yes to continue no to exit
int choice;//menu choice
do{
cout<< " Simple Vector \n"<<endl;
cout<< " Enter number 1 for integer, 2 for double, or 3 for strings"<<endl;
cout<< "1. integer\n";
cout<< "2. double\n";
cout<< "3. string\n";
cout<< " Enter your choice: ";
cin>> choice;
//set the numbic output formatting
cout<< fixed << showpoint << setprecision(2);
//set of actions
if(choice == 1)
{
//size of dynamic array
cout<< "\n How many data inputs do you have: ";
//data for size of array
/*******************************************
this is the data you enter in dynamic array
********************************************/
cout<< " Enter the data:\n"<<endl;
}
elseif(choice == 2)
{
//size of dynamic array
cout<< "\n How many data inputs do you have: "<<endl;
//data for size of array
//
/*******************************************
this is the data you enter in dynamic array
********************************************/
}
elseif(choice == 3)
{
//size of dynamic array
cout<< "\n How many data inputs do you have: "<<endl;
cout<< " Enter an index to retrieve the data at that index\n"<<endl;
}
cout<<" Play again?";
cin>>keepGoing;
}while(toupper(keepGoing) =='Y');
return 0;
}
I think you need to create a method called something like [set_size].
This method will have to cater for condition when [aptr] is pointing to some valid array and consequently needs to be transferred to the new array that [aptr] will point to.
This method will also have to cater for the case when you make the size smaller than its previous value - this will require some elements in array to be truncated.