Pages

praktikum pertama post test : ini ada program contoh dari program class


#include
#include
#include
using namespace std;

class bilangan {
friend ostream& operator<<(ostream&, const bilangan&);
friend istream& operator>>(istream&, bilangan&);

public :
bilangan(int a0=0, float b0=0.0) : a(a0), b(b0){}
void banding_int(const bilangan&, const bilangan&);
bilangan& operator=(const bilangan&);
bilangan operator+(const bilangan&);
bilangan operator-()const;

private:
int a;
float b;

};

ostream& operator<<(ostream& out, const bilangan& x)
{out<< "bagian integer : " <<
out<< "bagian float : " <<
return out;
}

void bilangan::banding_int(const bilangan&x, const bilangan& y)
{
if (x.a > y.a) cout <<<"::x lebih besar dari "<<<"::y";
else cout <<<"::x lebih kecil dari" <<<"::y";

}

bilangan& bilangan::operator=(const bilangan& x)
{ a= x.a;
b= x.b;
return *this;
}
istream& operator >>(istream& in, bilangan& x)
{
cout<<"\nmasukkan bagian integer :";
in >> x.a;
cout <<"masukkan bagian float :";
in >> x.b;
return in;

}

bilangan bilangan::operator+(const bilangan& x)
{
bilangan cc;
cc.a = a + x.a;
cc.b = b + x.b;
return cc;
}

bilangan bilangan::operator-() const

{ bilangan x;
x.a = -a;
x.b = -b;

return x;

}

int main(){
bilangan s, t(-2,3.14), d;
cout << "nilai awal s\n" << s; // operator biner << output
cout << "nilai awal + dari deklarasi\n" << t;
s = t; // operator biner =
cout << "setelah s di-assign t\n";
cout << "nilai s\n" << s;
cout << "masukkan nilai-nilai objek d";
cin >> d;
cout << "setelah d + t => \n” << d+t;
cout << "nilai d dinegatifkan\n" << -d;

getch;
return 0;
}

--------------------------------------------------------------------------------------------------------
a. Contoh bentuk kelas :

class bilangan {
friend ostream& operator<<(ostream&, const bilangan&);
friend istream& operator>>(istream&, bilangan&);

public :
bilangan(int a0=0, float b0=0.0) : a(a0), b(b0){}
void banding_int(const bilangan&, const bilangan&);
bilangan& operator=(const bilangan&);
bilangan operator+(const bilangan&);
bilangan operator-()const;

private:
int a;
float b;

b.Contoh bentuk Konstruktor :

ostream& operator<<(ostream& out, const bilangan& x)
{out<< "bagian integer : " <<
out<< "bagian float : " <<
return out;
}

c.Method

void bilangan::banding_int(const bilangan&x, const bilangan& y)
{
if (x.a > y.a) cout <<<"::x lebih besar dari "<<<"::y";
else cout <<<"::x lebih kecil dari" <<<"::y";

}

bilangan& bilangan::operator=(const bilangan& x)
{ a= x.a;
b= x.b;
return *this;
}
istream& operator >>(istream& in, bilangan& x)
{
cout<<"\nmasukkan bagian integer :";
in >> x.a;
cout <<"masukkan bagian float :";
in >> x.b;
return in;

}

bilangan bilangan::operator+(const bilangan& x)
{
bilangan cc;
cc.a = a + x.a;
cc.b = b + x.b;
return cc;
}

bilangan bilangan::operator-() const

{ bilangan x;
x.a = -a;
x.b = -b;

return x;

d. Data member

public :
bilangan(int a0=0, float b0=0.0) : a(a0), b(b0){}
void banding_int(const bilangan&, const bilangan&);
bilangan& operator=(const bilangan&);
bilangan operator+(const bilangan&);
bilangan operator-()const;

private:
int a;
float b;

=========================================================================

#include <iostream>
    using namespace std;
    // bayu harnan purwaka
    // 09018099
    //template<class T>
    class Kompleks{
          friend class Operasi;
          friend ostream& operator<<(ostream&, const Kompleks&);
          friend istream& operator>>(istream&, Kompleks&);
          public:
                 Kompleks(int s=0, int t=0):a(s),b(t){}
                 void cetak();
                 Kompleks operator-();
                 Kompleks operator-(const Kompleks&);
                 Kompleks operator+(const Kompleks&);
                 Kompleks operator*(const Kompleks&);
                 private:
                         int a;
                         int b;
                         };
    //template<class T>
    void Kompleks::cetak(){
         if(b>0)cout<<"bilangan kompleks:"<<a<<"+"<<b<<"i";
         else cout<<"bilangan kompleks:"<<a<<b<<"i";
         cout<<endl;
         }
    //     template<class T>
         Kompleks Kompleks::operator-(){
                     Kompleks x;
                     x.a=a;
                     x.b=-b;
                     return x;
                     }
    //  template<classT>
         Kompleks Kompleks::operator-(const Kompleks& m){
                 Kompleks x;
                     x.a=a-m.a;
                     x.b=b-m.b;
                     return x;
                     }
    //     template<class T>
         Kompleks Kompleks::operator+(const Kompleks& m){
                     Kompleks x;
                     x.a=a+m.a;
                     x.b=b+m.b;
                     return x;
                     }
   
    Kompleks Kompleks::operator*(const Kompleks&m)
    {
             Kompleks x;
             x.a=a*m.a;
             x.b=b*m.b;
             return x;
    }
   
    //     template<class T>
          ostream& operator<<(ostream& out, const Kompleks& x){
                              if(x.b==0) out<<'['<<x.a<<']';
                              else if(x.a==0 && x.b==1)out<<'['<<"i"<<']';
                              else if(x.a==0 && x.b==-1)out<<'['<<"-i"<<']';
                              else if(x.a==0 && x.b>1)out<<'['<<x.b<<"i"<<']';
                              else if(x.a==0 && x.b<-1)out<<'['<<x.b<<"i"<<']';
                              else if(x.b==1)out<<'['<<x.a<<"+"<<"i"<<']';
                              else if(x.b>0)out<<'['<<x.a<<"+"<<x.b<<"i"<<']';
                              else if(x.b==-1)out<<'['<<x.a<<"-i"<<']';
                              else out<<'['<<x.a<<x.b<<"i"<<']';
                              return out;
                              }
   
    //template <class T>
    istream& operator>>(istream& in, Kompleks&x)
    {
             cout<<"masukkan bagian real : ";
             in>>x.a;
             cout<<"masukakn bagian imajiner : ";
         in>>x.b;
             return in;
    }
   
    //template <class T>
    class Operasi{
          public :
                 Kompleks jumlah(const Kompleks&,const Kompleks&);
                 Kompleks kali(const Kompleks&,const Kompleks&);
                 Kompleks kurang(const Kompleks&,const Kompleks&);
    };
   
    //template <class T>
    Kompleks Operasi::jumlah(const Kompleks&m, const Kompleks&n)
    {
                                        Kompleks temp;
                                        temp.a=m.a+n.a;
                                        temp.b=m.b+n.b;
                                        return temp;
    }
   
    //template <class T>
    Kompleks Operasi::kurang(const Kompleks&m, const Kompleks&n)
    {
                                        Kompleks temp;
                                        temp.a=m.a-n.a;
                                        temp.b=m.b-n.b;
                                        return temp;
    }
   
    //template <class T>
    Kompleks Operasi::kali(const Kompleks&m, const Kompleks&n)
    {
                                        Kompleks temp;
                                        temp.a=(m.a*n.a)-(m.b*n.b);
                                        temp.b=(m.a*n.b)-(m.b*n.a);
                                        return temp;
    }
   
    int main (){
         Kompleks x(2,3),y(4,-4),t;
         Operasi z;
         cout<<"\nMenggunakan cetak():\n";
         x.cetak();
         cout<<"\nMenggunakan overloading :"<<x;
         cout<<"\nKonjugat :"<<-x<<endl;
         y.cetak();
         cout<<"\nPenjumlahan menggunakan methods :";
         t=z.jumlah(x,y);
         t.cetak();
         cout<<"\nPenjumlahan menggunakan operator:";
         t=x+y;
         cout<<x<<"+"<<y<<"="<<t;
         cout<<"\nPerkalian menggunakan methods:";
         t=z.kali(x,y);
         t.cetak();
         cout<<"\nperkalian menggunakan operator:";
         t=x*y;
         cout<<x<<"*"<<y<<"="<<t;
         t=x-y;
         cout<<"\n"<<x<<"-"<<y<<"="<<t<<endl;
         cout<<endl;
         Kompleks n;
         cin>>n;
         cout<<n;
         system("PAUSE");
         return 0;
         }

=========================================================================

#include <iostream>
#include <conio.h>
//#include ‘xcept.h’

using namespace std;
//template
class Array1D {

friend ostream& operator<< (istream&, 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 ;
};

//template
Array1D::Array1D(int sz)
{
if ( sz < 0);
size = sz;
element = new int[sz];
}

//template
Array1D::Array1D(const Array1D& v)
{
size = v.size;
element =new int[size]; // get space
for (int i=0; i<size; i++)// copy element
element[i] = v.element[i];
}

//template
int& Array1D::operator[](int i)const
{
if(i=size); //throw OutOfBounds();
return element[i];
}
//template
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] = v.element[i];
}
return *this;
}

//template
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;
}

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

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

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

//tempalte
Array1D& Array1D::operator+=(const int& x)
{
for (int i=0; i < size; i++)
element [i] += x;
return *this;
}

//tempalte
ostream& operator <<( ostream& out, const Array1D& x)
{
for (int i=0; i<x.size; i++)
out << x.element[i] <=0; i–)
element[i+1]=element[i];
element[0]=temp;
}

void Array1D::geser_kiri()
{
int n= 10;
int temp= element[0];
for(int i=0;i<n;i++)
element[i]=element[i+1];
element[n-1]= temp;
}

//template
Array1D& Array1D::ReSize(int sz)
{
if (sz < 0);//throw BadInitializer();
delete [] element;
size = sz;
element = new int [size];
return *this;
}

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 <<"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;
Z.geser_kiri();
cout<<"isi Array seteleh digeser ke kiri;"<<Z<<endl;
Z.geser_kanan();
cout<<"Isi Array setelah digeser ke kanan:"<<Z<<endl;

}
catch (…) {
cerr << "An execption has occurred" << endl;}

system("PAUSE");
return EXIT_SUCCESS;
}

=========================================================================

#include <cstdlib>
#include <iostream>

using namespace std;

class Node{
friend class List;
friend ostream& operator<<(ostream&, const List&);
public:
Node(char& t, Node* p) : info(t), berikut(p){}
protected:
char info;
Node *berikut;
};

class List{
friend ostream& operator<<(ostream&, const List&);
public:
List() : kepala(0){}
~List();
void sisip(char t);
int hapus(char& t);
int kosong() {return (kepala == 0);}
void cetak();
protected:
Node* kepala;
Node* nodeBaru(char& t,Node* p)
{ Node* q = new Node(t,p); return q;}
};

ostream& operator<<(ostream& out, const List& k)
{
for (Node* p = k.kepala; p; p=p->berikut)
out << p->info << "->";
out << "*\n";
return out;
}

List::~List()
{
Node* temp;
for (Node* p = kepala; p;)
{
temp = p;
p = p->berikut;
delete temp;
}
}

void List::sisip(char t)
{
cout << "data "<< t << " masuk list :";
Node* p = nodeBaru(t,kepala);
kepala = p;
}

int List::hapus(char& t)
{
if (kosong()) return 0;
t = kepala->info;
Node* p = kepala;
kepala = kepala->berikut;
delete p;
return 1;
}

void List::cetak()
{
for (Node* p = kepala; p; p=p->berikut)
cout << p->info << "->";
cout << "*\n";
}

int main(int argc, char *argv[])
{
List x;
char data;
x.sisip('a');
cout << x;
x.sisip('b');
cout << x;
x.sisip('c');
cout << x;
x.sisip('d');
cout << x;
for (int i=0; i<4; i++){
x.hapus(data);
cout << data << " dihapus dari list :";
cout << x;
}
system("PAUSE");
return EXIT_SUCCESS;
}

=========================================================================

#include <cstdlib>
#include <iostream>
#define maks 5
using namespace std;

class Queue{
      int a,b,c,d;
  friend ostream& operator<<(ostream&, const Queue&);
public :
 Queue();
 int penuh(int);
 int kosong(int);
 void cetak();
 void enqueue(char);
 char dequeue();
private :
 char A[maks];
 int banyak;
 int depan;
 int belakang;
};

ostream& operator<<(ostream& out, const Queue& s)
{ cout <<"\nIsi Queue:";
   for (int i=0;i<s.banyak; i++)
    out << s.A[i]<<"";
}

Queue::Queue(){
 banyak =0;
 for (int i=0; i<maks; i++)
 A[i] = '0';
}

int Queue::penuh(int s)
{ return s== maks ? 1:0;}

int Queue::kosong(int s)
{ return s== maks ? 1:0;}

void Queue::cetak()
{ cout <<"\nIsi Queue:";
  for (int i=0; i< banyak; i++)
   cout << A[i] <<"";
}

void Queue::enqueue(char x)
{
  cout <<"\nElemen :"<< x << " masuk antrian";
  if (penuh(banyak)) cout <<"queue penuh";
  else if(A[0]=='0'){
   A[0]=x;
   banyak++;
  }
else {
 for (int i=banyak;i>=0; i--)
  A[0]=0;
  banyak++;
  }
}

char Queue::dequeue()
{
  char temp=A[--banyak];
  cout << "\nDequeue elemen -->" << temp;
  A[banyak]= '0';
return temp;
}

int main(){
 Queue q;
 for(char c='a';c<'d';c++){
  q.enqueue(c);
  q.cetak();
  cout << q;

}
 char p = q.dequeue();
   q.cetak();
 cout << "\n\nCetak pakai overloading : " << q;
  system("PAUSE");
    return EXIT_SUCCESS;
}

=========================================================================




 
© 2011 Zeld BlogGobloG | Except as noted, this content is licensed under Creative Commons Attribution 2.5.
For details and restrictions, see the Content License | Recode by zeld dimension | Based on zeld tampan blog