praktikum ke-2 sdata

konstruktor 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;

}

This entry was posted in praktikum sdata. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s