//DESTA ARIANTI
//09018291
#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 ;
private:
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"<<y.a<<"::y";
}
Bilangan& Bilangan ::operator =(const Bilangan& x)
{
a=x.a;
b=x.b;
return *this;
}
istream& operator>>(istream& in ,Bilangan& x)
{
cout<<"\nMasukan bagian integer :";
in>>x.a;
cout<<"masukan 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;
}
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<<"masukan nilai-nilai objek d";
cin>>d;
cout<<"setelah d +t =>\n"<<d+t;
cout<<"nilai d dinegatifkan\n"<<-d;
system ("PAUSE");
return EXIT_SUCCESS;
}
NAMA : DESTA ARIANTI
NIM : 09018291
3. A. KELAS
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 ;
private:
int a;
float b;
};
B. KONSTRUKTOR
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"<<y.a<<"::y";
}
Bilangan& Bilangan ::operator =(const Bilangan& x)
{
a=x.a;
b=x.b;
return *this;
C. METHOD
int a;
float b;
};
D. DATA MEMBER
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 ;
E. OPERATOR OVERLOADING
friend ostream& operator<<(ostream&, const Bilangan&) ;
friend istream& operator>>(istream&, Bilangan&);