09018238
POSTEST
Header File
#include <iostream> |
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&)const; |
Bilangan operator-()const; |
protected: |
int a; |
float b; |
}; |
ostream& operator<<(ostream& out, const Bilangan& x){ |
out<<"Bagian integer:"<<x.a<<endl; |
out<<"Bagian float:"<<x.b<<endl; |
return out; |
} |
void Bilangan::banding_float(const Bilangan& x, const Bilangan& y){ |
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){ |
cout<<"\nMasukkan 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; |
} |
class Bil_float:public Bilangan{ |
friend ostream& operator<<(ostream&, const Bil_float&); |
public: |
Bil_float(int a0=0, int b0=0, float ft='3,14'):Bilangan(a0,b0), c(ft){} |
private: |
float c; |
}; |
ostream& operator<<(ostream& out, const Bil_float& x){ |
out<<"Bagian integer:"<<x.a<<endl; |
out<<"Bagian float:"<<x.b<<endl; |
out<<"Bagian float:"<<x.c<<endl; |
return out; |
} |
Main function
#include <iostream> |
using namespace std; |
int main(){ |
Bilangan s,t(-2,3.14),d; |
cout<<"Nilai awal s sebelum deklarasi\n"<<s; |
cout<<endl; //operator biner << output |
cout<<"Nilai awal t setelah deklarasi\n"<<t; |
s=t; //operator biner = |
cout<<endl; |
cout<<"Setelah s di-assaign t\n"; |
cout<<"Nilai s\n"<<t; |
cout<<endl; |
cout<<"Masukkan nilai-nilai objek d"; |
cin>>d; //operator >> input |
cout<<endl; |
cout<<"Setelah d + t => \n"<<d+t; //operator biner + |
cout<<endl; |
cout<<"Nilai d dinegatifkan\n"<<-d; //operator uner - |
Bil_float ss; |
cout<<endl; |
cout<<"Nilai awal ss\n"<<ss; //operator biner << output |
system ("PAUSE"); |
return 0; |
} |
PRETEST
1. class Bil_float:public Bilangan{
friend ostream& operator<<(ostream&, const Bil_float&);
public:
Bil_float(int a0=0, int b0=0, float ft='3,14'):Bilangan(a0,b0), c(ft){}
private:
float c;
};
2. Bil_float(int a0=0, int b0=0, float ft='3,14'):Bilangan(a0,b0), c(ft){}
3. ostream& operator<<(ostream& out, const Bil_float& x){
out<<"Bagian integer:"<<x.a<<endl;
out<<"Bagian float:"<<x.b<<endl;
out<<"Bagian float:"<<x.c<<endl;
return out;
istream& operator>>(istream& in, Bilangan& x){
cout<<"\nMasukkan bagian integer:";
in>>x.a;
cout<<"Masukkan bagian float:";
in>>x.b;
return in;
}
4. void Bilangan::banding_float(const Bilangan& x, const Bilangan& y){
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";
}
Tidak ada komentar:
Posting Komentar