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 :

Array Statis

#include
#include
#define maks 5
using namespace std;
class Array1D{
friend ostream& operator<(ostream&, const Array1D&); friend istream& operator>(istream&, Array1D&);
public :
Array1D();
void cetak();
void geser_kiri();
void geser_kanan();
private :
char A[maks];
};

Array1D::Array1D()
{
for(int i=0;i<<<" "; } ostream& operator<<(ostream& out, const Array1D& x) { for(int i=0;i<<<" "; cout<>(istream& in, Array1D& x)
{
int posisi;
cout<<"Mengisi pada posisi ke : "; in>>posisi;
if(posisi>0 &&posisi<=maks){ cout<<"masukkan elemen array-nya :"; in>>x.A[posisi-1];
}
else
cout<<"anda memasukan posisi diluar range ..."; return in; } void Array1D::geser_kanan() { int n = maks; int temp = A[n-1]; for(int i=n-1; i>=0;i--)
A[i+1]=A[i];
A[0]=temp;
}


void Array1D::geser_kiri()
{
int n = maks;
int temp = A[0];
for(int i=0;i<<"Array masih kosong : "<>x;
cout<<"isi array saat ini:"<
x.geser_kiri();
cout<<"isi array setelah digeser ke kiri :"<
x.geser_kanan();
cout<<"isi array setelah digeser ke kanan :"<

system("PAUSE");
return EXIT_SUCCESS;
}

Hasil output :