C++ Class Template for vector operations.

//Class Template for vector operations.


#include
#include

template
class vector
{
T *arr;
int size;

public:

vector() {
arr=NULL;
size=0;
}

vector(int m);
vector(T *a,int n);
void modify(T value,int index);
void multiply(int scalarvalue);
void display();
};

template
vector :: vector(int m)
{
size=m;
arr = new T[size];
for(int i=0;i arr[i]=0;
}

template
vector :: vector(T *a,int n)
{
size=n;
arr = new T[size];
for(int i=0;i arr[i]=a[i];
}


template
void vector :: modify(T value,int index)
{
arr[index]=value;
}


template
void vector :: multiply(int scalarvalue)
{
for(int i=0;i arr[i] = arr[i] * scalarvalue;
}

template
void vector :: display()
{
cout<<"(";
for(int i=0;i {
cout< if(i!=size-1)
cout<<", ";
}
cout<<")";
}


void main()
{
clrscr();


//Creating Integer Vector.
int iarr[]={1,2,3,4,5};
vector v1(iarr,5); //Integer array with 5 elements.
cout<<"\nInteger Vector : ";
v1.display();
cout<<"\n\nModify index 3 with value 15\n";
v1.modify(15,3); //modifying index 3 with value 15.
cout<<"After Modification : ";
v1.display();
cout<<"\n\nMultiply with scalar value : 10\n";
v1.multiply(10); //Multiply with scalar value 10.
cout<<"After Multiplying : ";
v1.display();
cout<<"\n\n";


//Creating double Vector.
double darr[]={1.1,2.2,3.3,4.4,5.5};
vector v2(darr,5); //Double array with 5 elements.
cout<<"\nDouble Vector : ";
v2.display();
cout<<"\n\nModify index 0 with value 9.9 \n";
v2.modify(9.9,0); //modifying index 0 with value 9.9.
cout<<"After Modification : ";
v2.display();
cout<<"\n\nMultiply with scalar value : 10\n";
v2.multiply(10); //Multiply with scalar value 10.
cout<<"After Multiplying : ";
v2.display();
cout<<"\n\n";

getch();



}