Minggu, 31 Oktober 2010

Praktikum ke 3

#include<cstdlib>
#include<iostream>
#include<conio.h>
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.b) cout << x.a <<" ::x lebih besar dari " <<y.a<<"::y";
else cout <<x.a<<"::x.a 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<< "\nMasukkan 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;

};



class Bil_float : public Bilangan {
friend ostream& operator<< (ostream&, const Bil_float&);
public:
Bil_float(int a0=0, float b0=0.0, float cf=0.0):Bilangan(a0,b0), c(cf) {}
void banding_float(const Bil_float&, const Bil_float&);
private:
float c;

};//turunan bil float

void Bil_float::banding_float(const Bil_float& x, const Bil_float& y ){

if (x.a>y.b) cout << x.a <<" ::x lebih besar dari " <<y.a<<"::y";

else cout <<x.a<<"::x.a lebih kecil dari " <<y.a<<"::y";

}



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;

}

ostream& operator<< (ostream& out, const Bil_float& x)

{

out << "Bagian integer : " <<x.a<< endl;

out << "Bagian float : " <<x.b<< endl;

out << "Bagian float turunan : " <<x.c<< endl;

return out;

}



int main(){
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 s di-assign t\n";
cout<<"nilai s\n"<<s;
cout << " Masukkan nilai-nilai objek d";
cin>> d ;
cout << "\nSetelah d + t => \n" <<d+t;
cout << "nilai d dinegatifkan \n"<< -d;
Bil_char ss;
cout << " Nilai awal ss \n"<< ss;
Bil_float ff;
cout<<"nilai awal ff\n"<<ff;
system("PAUSE");
return EXIT_SUCCESS;
}


0 komentar:

Posting Komentar