Postes Praktikum Struktur Data Pertemuan 1
#include <cstdlib>Outputnya :
#include <iostream>
using namespace std;
template <class T>
class aritmatik{
public:
void judul()
{cout<<"\t\t\tClassdanObjekserta 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\nOperasiAritmatikadengan 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\nOperasiAritmatikadengan method\n";
cout<<"\n\ndataA(";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;
}
0 Response to " SData - Praktikum 1 "Konstruksi Program C++" "
Post a Comment