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