Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

See image for program description here: http://i.imgur.com/w0V0u9I.jpg Design a

ID: 3546116 • Letter: S

Question


See image for program description here: http://i.imgur.com/w0V0u9I.jpg


Design a generalized template class named Simple Vector - an array that can hold any specified data type. Use a dynamic array for the implementation of SimpleVector. This class should have the following: private data members - a pointer to the specific template data type, and the array size a default no-arg constructor that sets the pointer to null and array size to 0 a single argument constructor that takes the array size as argument and creates a dynamic array using the specified size. your own copy constructor that performs a deep copy i.e., create a dynamic array and copy elements from the other array that was passed as an argument to the copy constructor a destructor that deallocates the memory allocated to the dynamic array accessor method to return the array size a public method called get Element At that takes position as argument and returns the element from the array at that specified position Overload the operator. The argument is a subscript. This function returns a reference to the element in the array indexed by the subscript. The main() should test all of these functions. It should perform the following steps in a loop: ask the user what type of data do they want to enter (this would be the data type you would use to create an object of the Simple Vector class). You can ask the user to enter 1 for integer, 2 for double and 3 for strings. ask the user how many data inputs do they have (this would be the size of your dynamic array). ask the user to enter the data (this is the data that you would enter in the dynamic array). ask the user to enter an index to retrieve the data at that index (use 'getElementAt' member function for this). Display the retrieved data with the index in a nicely formatted way. ask the user again to enter an index to retrieve the data at that index (use the overloaded ' ' operator for this). Display the retrieved data with the index in a nicely formatted way. create a new Simple Vector object using the copy constructor by passing the old object to the constructor. Display to the user the data in this new object. ask the user if they want to enter the data again (y/n). if 'n', then the program ends, otherwise it should clear the SimpleVector object (i.e., deallocate the dynamic array set it to NULL and set the size to 0) and repeat the loop (ask the user to enter new data...). Filename: simpleVector.cpp

Explanation / Answer

#include<iostream>
using namespace std;
template<class T>
class SimpleVector
{
private:
int array_size;
T* array;
public:
SimpleVector()
{
array = NULL;
array_size = 0;
}
SimpleVector(int size)
{
if(size>0)
{
array_size = size;
array = new T[size];
}
}
int getArray_size() { return array_size; }
SimpleVector(const SimpleVector<T>& other): // copy constructor
array_size(other.array_size)
{
array = new T[array_size];
for(int i=0; i<array_size; i++)
array[i] = other.array[i];
}
~SimpleVector()
{
delete[] array;
array_size=0;
}
T getElementAt(int position)
{
if(position>=0 && position < array_size)
return array[position];
return 0;
}
T& operator[](int position)
{
if(position>=0 && position < array_size)
return array[position];
}
void clear()
{
if(array!=NULL)
{
delete[] array;
array_size=0;
}
}
void setElementAt(int i, T val)
{
array[i] = val;
}
};

int main()
{
int choice;
char ch;
do
{
cout << "What type of data you want to create " << endl;
cout <<"Enter 1 for Integers " << endl;
cout <<"Enter 2 for Double " << endl;
cout <<"Enter 3 for Strings " << endl;
cin >> choice;
cout << endl;
int size;
cout << "Enter how many data inputs do u have : ";
cin >> size;
cout << endl;
if(choice==1)
{
SimpleVector<int> s_int(size);
int value;
for(int i=0; i<size; i++)
{
cout << "Enter element " << (i+1) << " : ";
cin >> value;
s_int.setElementAt(i,value);
cout << endl;
}
int pos;
cout <<"Enter index of position for which we need data :";
cin >> pos;
cout << endl;
cout << "Value at Position " << pos << " in array is " << s_int.getElementAt(pos) << endl;
cout <<"Enter index of position for which we need data :";
cin >> pos;
cout << endl;
cout << "Value at Position " << pos << " in array is " << s_int[pos] << endl;
SimpleVector<int> s_copier(s_int);
cout <<"Contents of copier are " << endl;
for(int i=0; i<s_copier.getArray_size(); i++)
cout << s_copier[i] << " " << endl;
cout << endl;
}
else if(choice==2)
{
SimpleVector<double> s_double(size);
double value;
for(int i=0; i<size; i++)
{
cout << "Enter element " << (i+1) << " : ";
cin >> value;
s_double.setElementAt(i,value);
cout << endl;
}
int pos;
cout <<"Enter index of position for which we need data :";
cin >> pos;
cout << endl;
cout << "Value at Position " << pos << " in array is " << s_double.getElementAt(pos) << endl;
cout <<"Enter index of position for which we need data :";
cin >> pos;
cout << endl;
cout << "Value at Position " << pos << " in array is " << s_double[pos] << endl;
SimpleVector<double> s_copier(s_double);
cout <<"Contents of copier are " << endl;
for(int i=0; i<s_copier.getArray_size(); i++)
cout << s_copier[i] << " " << endl;
cout << endl;
}
else if(choice==3)
{
SimpleVector<string> s_string(size);
string name;
for(int i=0; i<size; i++)
{
cout << "Enter element " << (i+1) << " : ";
cin >> name;
s_string.setElementAt(i,name);
cout << endl;
}
int pos;
cout <<"Enter index of position for which we need data :";
cin >> pos;
cout << endl;
cout << "String at Position " << pos << " in array is " << s_string.getElementAt(pos) << endl;
cout <<"Enter index of position for which we need data :";
cin >> pos;
cout << endl;
cout << "String at Position " << pos << " in array is " << s_string[pos] << endl;
SimpleVector<string> s_copier(s_string);
cout <<"Contents of copier are " << endl;
for(int i=0; i<s_copier.getArray_size(); i++)
cout << s_copier[i] << " " << endl;
cout << endl;
}
else
{
cout << "Invalid type entereed" << endl;
}
cout << "Do you want to create one more :" << endl;
cin >> ch;
}while(ch=='y' || ch =='Y');
return 0;
}