| #include <iostream> |
| #ifndef __xcept |
| #define __xcept |
| // exception classes for various error types |
| // bad initializers |
| class BadInitializers { |
| public: |
| BadInitializers() {} |
| }; |
| // insufficient memory |
| class NoMem { |
| public: |
| NoMem() {} |
| }; |
| // change new to throw NoMem instead of xalloc |
| void my_new_handler() |
| { |
| throw NoMem(); |
| }; |
| //new_handler Old_Handler_ = set_new_handler(my_new_handler); |
| // improper array, find, insert, or delete index |
| // or deletion from empty structure |
| class OutOfBounds { |
| public: |
| OutOfBounds() {} |
| }; |
| // use when operands should have matching size |
| class SizeMismatch { |
| public: |
| SizeMismatch() {} |
| }; |
| // use when zero was expected |
| class MustBeZero { |
| public: |
| MustBeZero() {} |
| }; |
| // use when zero was expected |
| class BadInput { |
| public: |
| BadInput() {} |
| }; |
| #endif |
|
| //CLASS ARRAY1D: |
| using namespace std; |
| class Array1D{ |
| friend ostream& 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); |
| void geser_kiri(); |
| void geser_kanan(); |
| private: |
| int size; |
| int* element; |
| }; |
| Array1D::Array1D(int sz){ |
| if(sz<0)throw BadInitializers(); |
| size=sz; |
| element=new int[sz]; |
| } |
| Array1D::Array1D(const Array1D& v){ |
| size=v.size; |
| element=new int[size]; |
| for(int i=0; i<size; i++) |
| element[i]=element[i]; |
| } |
| int& Array1D::operator[](int i)const{ |
| if(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<size;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<size; 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<size; i++) |
| w.element[i]=element[i]-v.element[i]; |
| return w; |
| } |
| Array1D Array1D::operator-()const{ |
| Array1D w(size); |
| for(int i=0; i<size; i++) |
| w.element[i]=-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<size; i++) |
| w.element[i]=element[i]=v.element[i]; |
| return w; |
| } |
| Array1D& Array1D::operator+=(const int& x){ |
| for(int i=0; i<size;i++) |
| element[i]+=x; |
| return* this; |
| } |
| ostream& operator<<(ostream& out, const Array1D& x){ |
| for(int i=0; i<x.size; i++) |
| out << x.element[i] << " "; |
| return out; |
| } |
| Array1D& Array1D::Resize(int sz){ |
| if(sz<0) throw BadInitializers(); |
| delete[]element; |
| size=sz; |
| element=new int[size]; |
| return* this; |
| } |
|
| //FUNGSI MAINNYA: |
| int main(int argc, char *argv[]) |
| { |
| try{ |
| Array1D X(10), Y, Z; |
| for(int i=0; i<10; i++) |
| X[i]=i; |
| cout << "X[3] = " << X[3] << endl; |
| cout << "\nX is \n" << X << endl; |
| Y=X; |
| cout << "\nY is \n" << Y << endl; |
| X += 2; |
| cout << "\nX increment by 2 is \n" << X << endl; |
| Z=(Y+X)*Y; |
| cout << "\n(Y+X)*Y is \n" << Z << endl; |
| cout << "\n-(Y+X)*Y is \n" << -Z << endl; |
| } |
| catch(...){ |
| cout << "An Exception has occured" << endl;} |
| |
| system("PAUSE"); |
| return EXIT_SUCCESS; |
| } |