/*This
code was written by A. El-Gadi of Tripoli University for educational
purposes. It is an implementation of a simple array class template. For any
questions and discussion
use the comment section below.*/
#include <iostream>
using namespace std;
template<typename T>
class Array{private:
T* parray;
int size;
public:
Array():parray(NULL),size(0){};
Array(int sz):parray(new T[sz]),size(sz){};
Array(T* parr,int sz):parray(new T[sz]),size(sz){for(int i=0;i<size;i++){parray[i]=parr[i];}}
void printarray(){cout<<"[ "; for(int i=0;i<size;i++){cout<<parray[i]<<' ';} cout<<']'<<endl;}
T getelement(int i){return parray[i];}
void setelement(int i,T telem){parray[i]=telem;}
void copyarray(Array<T> art){delete []parray; parray=new T[art.size]; size=art.size; for(int i=0;i<size;i++){parray[i]=art.getelement(i);}}
~Array(){delete []parray;}
};
int main(){
Array<int> ia(10);
ia.printarray();
float finita[5]={1,2,3,4,5};
Array<float> fa1(finita,5);
fa1.printarray();
Array<float> fa2;
fa2.copyarray(fa1);
fa2.printarray();
}
using namespace std;
template<typename T>
class Array{private:
T* parray;
int size;
public:
Array():parray(NULL),size(0){};
Array(int sz):parray(new T[sz]),size(sz){};
Array(T* parr,int sz):parray(new T[sz]),size(sz){for(int i=0;i<size;i++){parray[i]=parr[i];}}
void printarray(){cout<<"[ "; for(int i=0;i<size;i++){cout<<parray[i]<<' ';} cout<<']'<<endl;}
T getelement(int i){return parray[i];}
void setelement(int i,T telem){parray[i]=telem;}
void copyarray(Array<T> art){delete []parray; parray=new T[art.size]; size=art.size; for(int i=0;i<size;i++){parray[i]=art.getelement(i);}}
~Array(){delete []parray;}
};
int main(){
Array<int> ia(10);
ia.printarray();
float finita[5]={1,2,3,4,5};
Array<float> fa1(finita,5);
fa1.printarray();
Array<float> fa2;
fa2.copyarray(fa1);
fa2.printarray();
}
No comments:
Post a Comment