Oleh: adjist | 31 Oktober 2010

POSTTEST KE-5

#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; // unary +
Array1D<T> operator+(const Array1D<T>& v) const;
Array1D<T> operator-() const; // unary minus
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);
private:
int size;
T *element;
};

template<class T>
Array1D<T>::Array1D(int sz)
{
if (sz < 0) throw BadInitializers();
size = sz;
element = new T[sz];
}

template<class T>
Array1D<T>::Array1D(const Array1D<T>& v)
{
size = v.size;
element = new T[size];
for (int i = 0; i < size; i++)
element[i] = v.element[i];
}

template<class T>
T& Array1D<T>::operator[](int i) const
{
if (i < 0 || i >= size) throw OutOfBounds();
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
{
if (size != v.size) throw SizeMismatch();

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
{/
if (size != v.size) throw SizeMismatch();

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
{
if (size != v.size) throw SizeMismatch();

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)
{

if (sz < 0) throw BadInitializers();
delete [] element;
size = sz;
element = new T [size];
return *this;
}


class BadInitializers {
public:
BadInitializers() {}
};


class NoMem {
public:
NoMem() {}
};


void my_new_handler()
{
throw NoMem();
};


class OutOfBounds {
public:
OutOfBounds() {}
};


class SizeMismatch {
public:
SizeMismatch() {}
};


class MustBeZero {
public:
MustBeZero() {}
};


class BadInput {
public:
BadInput() {}
};

int main(int argc, char *argv[])
{
try {
Array1D<int> X(10), Y, Z;
for (int i=0; i < 10; i++)
X[i] = i;
cout << “X[3] = ” << X[3] << endl;
cout << “X is ” << X << endl;
Y = X;
cout << “Y is ” << Y << endl;
X += 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;
}
catch (…) {
cerr << “An exception has occurred” << endl;}

return 0;
}
(mari belajar bersama….hheheheheheh)

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

Kategori

%d blogger menyukai ini: