Pelajaran Hidup

Inspirasi dari sebuah pengalaman dan pelajaran hidup..Semua butuh sebuah pembelajaran

Array Dinamis

#include
#include
#include "xcept.h"

class Array1D{
friend istream& operator<<(ostream&,const Array1D&); public: Array1D(int size=0); Array1D(const Array1D& v); ~Array1D(){delete [] element;} int& operator[](int i)const; int Size(){return size;} Array1D& operator=(const Array1D& v); Array1D operator+()const; Array1D operator+(const Array1D& v)const; Array1D operator-()const; Array1D operator-(const Array1D& v)const; Array1D operator*(const Array1D& v)const; Array1D& operator+=(const int& x); Array1D& Resize(int sz); private: int size; int* element; }; Array1D::Array1D(int sz){ if(sz<0)throw size="sz;" element="new" size="v.size;" element="new" i="0;" int="" i=""><0||i>=size)throw OutOfBounds();
return element[i];
}

Array1D& Array1D::operator=(const Array1D& v){
if(this !=&v){
size=v.size;
delete[]element;
element=new int[size];
for(int i=0;i
element[i]=element[i];
}
return* this;
}

Array1D Array1D::operator+(const Array1D& v)const{
if (size != v.size) throw SizeMismatch();
Array1D w(size);
for(int i=0; i
w.element[i]=element[i]+v.element[i];
return w;
}

Array1D Array1D::operator-(const Array1D& v)const{
if(size != v.size) throw SizeMismatch();
Array1D w(size);
for(int i=0; i
w.element[i]=element[i]-v.element[i];
return w;
}

Array1D Array1D::operator-()const{
Array1D w(size);
for(int i=0; i
w.element[i]=-element[i];
return w;
}

Hasil Output :

0 komentar:

Posting Komentar