Category Archives: Praktikum

Praktikum 1

#include <cstdlib>
#include <iostream>
using namespace std;
class bilangan{       // CLASS
friend ostream& operator<<(ostream&,const bilangan&);    
friend istream& operator>>(istream&, bilangan&);

public:
bilangan(int a0=0, float b0=0.0):a(a0),b(b0){}                 //konstruktor
void banding_int(const bilangan&, const bilangan&);
bilangan& operator=(const bilangan&);
bilangan operator+(const bilangan&)const;
bilangan operator-()const;
private:                         // DATA MEMBER (SEMUA YANG ADA  DI PRIVATE)
int a;
float b;
};
ostream& operator<<(ostream& out, const bilangan& x)                   //overloding
{
out<<"bagian integer :"<<x.a<<endl;
out<<"bagian float :" <<x.b<<endl;
return out;
}

void bilangan::banding_int(const bilangan& x, const bilangan& y)      //method
{
if(x.a>y.a)cout<<x.a<<"::x lebih besar dari "<<y.a<<"::y";
else cout<<x.a<<"::x lebih kecil dari "<<y.a<<"::y";
}
bilangan& bilangan::operator=(const bilangan& x)
{
a=x.a;
b=x.b;
return*this;
}
istream& operator>>(istream& in, bilangan& x)                            //overloading
{
cout<<"\n masukkan bagian integer :";
in>>x.a;
cout<<"masukkan bagian float ;";
in>>x.b;
return in;
}
bilangan bilangan::operator+(const bilangan& x) const   
{
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<<"niilai awa, s\n"<<s;
cout<<"nilai awal t dari deklarasi \n"<<t;
s=t;
cout<<" setelah s di assign t \n";
cout<<"nialai s\n "<<s;
cout<<"masukkan nilai-nilai objek d";
cin>>d;
cout<<"detelah d+t=> \n"<< d+t;
cout<<"nilai d dinegatifkan  \n"<<-d;
}

Praktikum 5

#include <iostream.h>
template <class T>

class Array1D{
 friend ostream& operator<<(ostream&,const Array1D&);
public:
 Array1D(int size=0);
 Array1D(const Array1D& v);
 Array1D(){delete[]element;}
 T& operator[](int i)const;
 int Size(){return size;}
 Array1D<T>& operator =(const Array1D<T>& v);
 Array1D<T> operator+()const;
 Array1D<T> operator+(const Array1D<T>& v)const;
 Array1D operator-()const;
 Array1D operator-(const Array1D& v)const;
 Array1D<T> operator*(const Array1D<T>& v)const;
 Array1D<T>&operator+=(const T& x);
 Array1D<T>& ReSize(int sz);
 Array1D<T>& geser_kanan();
 Array1D<T>& geser_kiri();
private:
 int size;
 T*element;
};
template <class T>
Array1D::Array1D(int sz)
{

 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
 element[i]=v.element[i];
}
template <class T>
T& Array1D::operator[](int i)const
{

 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
 element[i]=v.element[i];
 }
 return *this;
}
template <class T>
Array1D<T>Array1D<T>::operator+(const Array1D<T>& v)const
{
 Array1D<T>w(size);
 for(int i=0; i
 w.element[i]=element[i]- v.element[i];
 return w;
}
template <class T>
Array1DArray1D::operator-(const Array1D& v)const
{
 Array1D<T>w(size);
 for(int i=0; i
 w.element[i]=element[i]+ v.element[i];
 return w;
}
template <class T>
Array1DArray1D::operator-()const
{
 Array1D<T>w(size);
 for(int i=0; i
 w.element[i]=-element[i];
 return w;
}
template <class T>
Array1D<T>Array1D<T>::operator*(const Array1D<T>& v)const
{
 Array1D<T>w(size);
 for(int i=0; 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& 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)
{
 delete[]element;
 size=sz;
 element=new T[size];
 return *this;
}
template <class T>
Array1D<T>&Array1D<T>::geser_kanan()
{
 int n = size;
 int temp = element[n-1];
 for (int i=n-1; i>=0; i--)
 element[i+1]=element[i];
 element[0]=temp;
}
template <class T>
Array1D<T>&Array1D<T>::geser_kiri()
{
 int n=size;
 int temp = element[0];
 for(int i=0; i<n; i++)
 element[i]=element[i+1];
 element[n-1]=temp;

}

int main()
{
 int X(10),Y,Z;
 for (int i=0; i<10; i++)
 X=i;
 cout<<" X = "<< X <<endl;
 cout<<" X is "<< X <<endl;
 Y=X;
 cout<<" Y is "<< Y <<endl;
 Y += 2;
 cout << " X incremented by 2 is " <<
 Z=(Y+X)*Y;
 cout<<"(Y+X)*y is " << Z <<endl;
 cout<< "-(Y+X)*Y is " << -Z <
 cout<<"setelah digeser kekanan :"<<(Y-X)*Y<
 cout<<"setelah digeser kekiri  :"<<(Y+X)<

 system("PAUSE");
}

Praktikum 4

#include <cstdlib>
#include <iostream>
#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
                  A[i]='0';
                  }
void array1d::cetak()
{
     for(int i=0; i
     cout<< A[i]<<"";
     }

ostream& operator<<(ostream& out, const array1d& x)
{
         for (int i=0; i
         cout<i]<<"";
         cout<<endl;
         return out;
         }
istream& operator>>(istream& in, array1d& x)
{
         int posisi;
cout<<"hak cipta dilindungi undang-undang jangan dicopy";
         cout<<"mengisi array pada ke :";

         in>>posisi;
         if(posisi> 0 && posisi<=maks){
                    cout<<"masukkan elemen array nya :";
                    in>>x.A[posisi-1];
                    }
                    else
                    cout<<"anda memasukkan posisi di luar 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<n; i++)
              A[i]=A[i+1];
              A[n-1]=temp;
              }
main(){
       array1d x;
       cout<<"array masih kosog ; "
       cout<<"\n";
       cin>>x;
       cout<<"isi array saat ini : "
       cout<<"\n";
       x.geser_kiri();
       cout<<"isikan array setelah digeesr ke kiri : "
       cout<<"\n";
       x.geser_kanan();
       cout<<"isikan array setelah digeser kekanan : "
    system("PAUSE");
    return EXIT_SUCCESS;
}

Prkatikum 3

#include <cstdlib>
#include <iostream>

using namespace std;

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

public:
 Bilangan(int aO=0, float bO=0.0, float dO=0.0): a(aO), b(bO){}
 void banding_int(const Bilangan&, const Bilangan&);
 Bilangan& operator=(const Bilangan&);
 Bilangan operator+(const Bilangan&) const;
 Bilangan operator-() const;

protected:
 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<<"\nMasukkan bagian float: ";
 in>>x.b;
 return in;
}

Bilangan Bilangan::operator+(const Bilangan& x) const
{
 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;
}

class Bil_char:public Bilangan{
friend ostream& operator<<(ostream&, const Bil_char&);

public:
 Bil_char(int aO=0, int bO=0, float dO=0, char ch='x')
 : Bilangan(aO, bO, dO), c(ch){}

private:
 char c;
 };

ostream& operator<<(ostream& out, const Bil_char& x)
{
 out<<"Bagian Integer : "<<
 out<<"Bagian float : "<<
 out<<"Bagian char : "<<
 return out;
}

class Bil_float:public Bilangan{

 friend istream& operator>>(istream&, Bil_float&);
 friend ostream& operator<<(ostream&, const Bil_float&);

public:
 Bil_float(int aO=0, int bO=0, float dO=0.0)
 : Bilangan(aO, bO, dO){}

private:
 float d;
 char c;
};

istream& operator>>(istream& in, Bil_float& x){
 cout<<"Masukkan bilangan integer : "; in>>x.a;
 cout<<"Masukkan bilangan float : "; in>>x.b;
 cout<<"Masukkan bilangan char : "; in>>x.c;
 cout<<"Masukkan bilangan float : "; in>>x.d;
 return in;
 }

ostream& operator<<(ostream& out, const Bil_float& x)
{
 out<<"Bagian Integer : "<<
 out<<"Bagian float : "<<
 out<<"Bagian char : "<<
 out<<"Bagian float : "<<
 return out;
}

int main(int argc, char *argv[])
{
 Bilangan s, t(-2,3.14),d;
 cout<<"Nilai awal s\n"<
 cout<<"Nilai awal t dari deklarasi\n"<
 s=t;
 cout<<"Setelah s di assign t\n";
 cout<<"Nilai s\n"<
 cout<<"Masukkan nilai-nilai objek d";
 cin>>d;
 cout<<"setelah d+t=>\n"<
 cout<<"Nilai d dinegatifkan\n"<<-d;
 Bil_char ss;
 cout<<"Nilai awal ss\n"<

 system("PAUSE");
 return EXIT_SUCCESS;
 }

praktikum 2

komstruktor dan template

#include <cstdlib>

#include <iostream>

using namespace std;

template<class T>

class Kompleks{

friend class Operasi<T>;

friend ostream& operator<<(ostream&, const kompleks<T>&;

friend istream& operator>>(Istream&, kompleks<T>&;

public:

Kompleks (T s=0, T t=0):a(s),b(t){}

void cetak();

Kom,pleks operator-();

Kompleks operator-(const Kompleks<T>&);

Kompleks operator+(const Kompleks<T>&);

private:

T a;

T b;

};

temlate<class Y>

void kompleks<T>::cetak()

{

if (b<0)cout<<”bilangan kompleks :”<<a<<”+”<<b<<”i”;

else cout<<”bilangan kompleks”<<a<<b<<”i”;

cout<<endl;

}

Kompleks <T> Kompleks<T>::operator-()

{Kompleks x;

x.a=a;

x.b=-b;

return x;

}

tmplate<classT>

kompleks<T>kompleks<T>::operator-(cont kompleks<T>&m)

{ Kompleks x;

x.a=a-m.a;

x.b=b-m.b;

return x;

}

template <class T>

kompleks<T>kompleks<T>::operator+(cont kompleks<T>&m)

{ Kompleks x;

x.a=a+m.a;

x.b=b+m.b;

return x;

}

template <class T>

ostream & o[perator<<(ostream&out,const Kompleks<T>& 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==i) 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;

}

tempate class<T>

istrien &operator>>(istream& in, Kompleks<T>& x)

{

cout<<”maukan bilangan riil :”;

in>>x.a;

cout<<”maukan bilangan imajiner :”;

in>>x.b;

retunn in;

}

//wgtrtre

tempae<classT>

Kompleks<T> Operasi<T>::jumlah(konst kompleks<T>& m,cost kompleks<T>& n)

{ Kompleks<T> temp;

temp.a=m.a+n.a;

temp.b=m.b+n.b;

retunn temp;

}

template class<T>

Kompleks<T> Operasi<T>::kurang(konst kompleks<T>& m,cost kompleks<T>& n))

{ Kompleks<T> temp;

temp.a=m.a-n.a;

temp.b=m.b-n.b;

retunn temp;

}

template class<T>

Kompleks<T> Operasi<T>::kali(konst kompleks<T>& m,cost kompleks<T>& n))

{ Kompleks<T> temp;

temp.a=(m.a*n.a)-(m.b*n.b);

temp.b=(m.a*n.b)-(m.b*n.a);

retunn temp;

}

int main(int argc, char *argv[])

{

kompleks<int> x(2,3),y(4,-4),t;

Operasi<int> z;

cout<<”menggunakan cetak():”;x.cetak();

cout<<”menggunakan overloading: “<<x;

cout <<”konjugat :”<<-x;

y.cetak();

cout<<”\nPenjumlahan menggunakan metod :”;

t=z.jumlah(x,y);

t.cetak();

cout<<”penjumlahan menggunakan operator:”;

t=x+y;

cout<<x<<”+”<<y<<”+”<<t;

cout<<”\nperkalian menggunakan metod:”;

t=z.kali(x,y);

t.cetak();

cout<<”penjumlahan menhggunakab operator:”;

t=x*y;

cout<<x<<”*”<<y<<”=”<<t;

t=x-y;

cout<<”\n”<<x<<”-”<<y<<”=”<<t<<endl;

kompleks<int> n;

cin>>n;

cout<<n;

system(“PAUSE”);

return EXIT_SUCCESS;

}