Skip to content

praktikum 3

Oktober 19, 2010

#include <cstdlib>
#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_int(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_char:public bilangan{
friend ostream& operator<<(ostream&, const bil_char&);
public:
bil_char(int a0=0, int b0=0,char ch=’x’):bilangan(a0,b0),c(ch){}
private:
char c;
};

ostream& operator<<(ostream& out,const bil_char& x){
out<<“Bagian integer: “<<x.a<<endl;
out<<“Bagian float: “<<x.b<<endl;
out<<“Bagian char: “<<x.c<<endl;
return out;
}

class bil_float:public bilangan{
friend ostream& operator<<(ostream&, const bil_float&);
friend istream& operator>>(istream&, bilangan&);
public:
bil_float(int a0=0, int b0=0, float ch=’x’):bilangan(a0,b0),c(ch){}
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 char: “<<x.c<<endl;
return out;
}

int main(int argc, char *argv[])
{
bilangan s,t(-2,3.14),d;
cout<<“Nilai awal s\n”<<s;
cout<<“Nilai awal t dari deklarasi\n”<<t;
s=t;
cout<<“Setelah diassign t\n”;
cout<<“Nilai s\n”<<s;
cout<<“Masukkan nilai-nilai objek d”;
cin>>d;
cout<<“Setelah d+t=>\n”<<d+t;
cout<<“Nilai d dinegatifkan\n”<<-d;
bil_char ss;
cout<<“Nilai awal ss\n”<<ss;

system(“PAUSE”);
return EXIT_SUCCESS;
}

From → Praktikum

Tinggalkan sebuah Komentar

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

%d blogger menyukai ini: