Contoh Program Template

#include <cstdlib>
#include <iostream>

using namespace std;
template <class T>
class aritmatik{
      public:
             void judul(){cout<<"\t\t\tClass dan Objek serta Template\n\n\n\n";}
aritmatik(T p=0, T q=0):a(p),b(q){}
             void cetak();
aritmatik operator -();
aritmatik operator -(const aritmatik&);
aritmatik operator +(const aritmatik&);
aritmatik<T>bagi(const aritmatik<T>&, const aritmatik<T>&);
aritmatik<T> kali(const aritmatik<T>&, const aritmatik<T>&);
      private:
              T a;
              T b;
      };

      template <class T>
      void aritmatik<T>::cetak(){
cout<<a<<" , "<<b;
           }

      template <class T>
aritmatik<T>aritmatik<T>::operator-(){
aritmatik x;
x.a=a;
x.b=-b;
                   return x;
                   }

      template <class T>
aritmatik<T>aritmatik<T>::operator-(const aritmatik& y){
aritmatik x;
x.a=a-y.a;
x.b=b-y.b;
                   return x;
                   }

      template <class T>
aritmatik<T>aritmatik<T>::operator+(const aritmatik& y){
aritmatik x;
x.a=a+y.a;
x.b=b+y.b;
                   return x;
                   }

      template <class T>
aritmatik<T>aritmatik<T>::bagi(const aritmatik<T>& x, const aritmatik<T>& y){
aritmatik z;
z.a=x.a/y.a;
z.b=x.b/y.b;
                   return z;
                   }

      template <class T>
aritmatik<T>aritmatik<T>::kali(const aritmatik<T>& x, const aritmatik<T>& y){
aritmatik z;
z.a=x.a*y.a;
z.b=x.b*y.b;
                   return z;
                   }

int main(int argc, char *argv[])
{
aritmatik<double>dataA(5.5,2), dataB(0,2), dataC;
dataC.judul();
cout<<"Isi dataA \n ";
dataA.cetak();
cout<<"\n\nIsidataB \n ";
dataB.cetak();
cout<<"\n\n OperasiAritmatikadengan operator\n";
cout<<"\n\ndataA(";dataA.cetak();cout<<")";cout<<" + dataB(";dataB.cetak();cout<<")\n";
dataC = dataA + dataB;
dataC.cetak();
cout<<"\n\ndataA(";dataA.cetak();cout<<")";cout<<" - dataB(";dataB.cetak();cout<<")\n";
dataC = dataA - dataB;
dataC.cetak();
cout<<"\n\n- dataA(";dataA.cetak();cout<<")\n";
dataC = - dataA;
dataC.cetak();
cout<<"\n\n- dataB(";dataB.cetak();cout<<")\n";
dataC = - dataB;
dataC.cetak();
cout<<"\n\n OperasiAritmatikadengan method\n";
cout<<"\n\n dataA(";dataA.cetak();cout<<")";cout<<" / dataB(";dataB.cetak();cout<<")\n";
dataC=dataC.bagi(dataA,dataB);
dataC.cetak();
cout<<"\n\ndataA(";dataA.cetak();cout<<")";cout<<" * dataB(";dataB.cetak();cout<<")\n";
dataC=dataC.kali(dataA,dataB);
dataC.cetak();

cout<<endl<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}
Iklan

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s