#include <iostream.h>
template <class T>
class Array1D{
friend ostream& operator<<(ostream&,const Array1D<T>&);
public:
Array1D(int size=0);
Array1D(const Array1D<T>& v);
Array1D(){delete[]element;}
T& operator[](int i)const;
int Size(){return size;}
Array1D<T>& operator =(const Array1D<T>& v);
Array1D<T> operator+()const;
Array1D<T> operator+(const Array1D<T>& v)const;
Array1D<T> operator-()const;
Array1D<T> operator-(const Array1D<T>& v)const;
Array1D<T> operator*(const Array1D<T>& v)const;
Array1D<T>&operator+=(const T& x);
Array1D<T>& ReSize(int sz);
Array1D<T>& geser_kanan();
Array1D<T>& geser_kiri();
private:
int size;
T*element;
};
template <class T>
Array1D<T>::Array1D(int sz)
{
size =sz;
element=new T[sz];
}
template <class T>
Array1D<T>::Array1D(const Array1D<T>& v)
{
size = v.size;
element=new T[size]; // get space
for (int i=0;i<size;i++)// copy elements
element[i]=v.element[i];
}
template <class T>
T& Array1D<T>::operator[](int i)const
{
return element[i];
}
template <class T>
Array1D<T>&Array1D<T>::operator =(const Array1D<T>& v)
{
if (this !=&v){
size=v.size;
delete[]element;
element=new T[size];
for(int i=0;i<size;i++)
element[i]=v.element[i];
}
return *this;
}
template <class T>
Array1D<T>Array1D<T>::operator+(const Array1D<T>& v)const
{
Array1D<T>w(size);
for(int i=0; i<size;i++)
w.element[i]=element[i]- v.element[i];
return w;
}
template <class T>
Array1D<T>Array1D<T>::operator-(const Array1D<T>& v)const
{
Array1D<T>w(size);
for(int i=0; i<size;i++)
w.element[i]=element[i]+ v.element[i];
return w;
}
template <class T>
Array1D<T>Array1D<T>::operator-()const
{
Array1D<T>w(size);
for(int i=0; i<size;i++)
w.element[i]=-element[i];
return w;
}
template <class T>
Array1D<T>Array1D<T>::operator*(const Array1D<T>& v)const
{
Array1D<T>w(size);
for(int i=0; i<size;i++)
w.element[i]=element[i]* v.element[i];
return w;
}
template <class T>
Array1D<T>&Array1D<T>::operator+=(const T& x)
{
for(int i=0; i<size;i++)
element[i]+=x;
return *this;
}
template <class T>
ostream& operator<<(ostream& out,const Array1D<T>& x)
{
for(int i=0; i<x.size;i++)
out<<x.element[i]<<" ";
return out;
}
template <class T>
Array1D<T>&Array1D<T>::ReSize(int sz)
{
delete[]element;
size=sz;
element=new T[size];
return *this;
}
template <class T>
Array1D<T>&Array1D<T>::geser_kanan()
{
int n = size;
int temp = element[n-1];
for (int i=n-1; i>=0; i--)
element[i+1]=element[i];
element[0]=temp;
}
template <class T>
Array1D<T>&Array1D<T>::geser_kiri()
{
int n=size;
int temp = element[0];
for(int i=0; i<n; i++)
element[i]=element[i+1];
element[n-1]=temp;
}
int main()
{
int X(10),Y,Z;
for (int i=0; i<10; i++)
X=i;
cout<<" X = "<< X <<endl;
cout<<" X is "<< X <<endl;
Y=X;
cout<<" Y is "<< Y <<endl;
Y += 2;
cout << " X incremented by 2 is " <<X<<endl;
Z=(Y+X)*Y;
cout<<"(Y+X)*y is " << Z <<endl;
cout<< "-(Y+X)*Y is " << -Z <<endl;
cout<<"setelah digeser kekanan :"<<(Y-X)*Y<<endl;
cout<<"setelah digeser kekiri :"<<(Y+X)<<endl;
system("PAUSE");
}
Tidak ada komentar:
Posting Komentar