Operator Overloading C++
Pada kelas Currency di pembahasan sebelumnya, method Add dapat dipandang sebagai operator +,
sedangkan method Increment dapat dipandang sebagai operator +=. Namun kita
dapat menggunakan baik operator + maupun += dengan cara meng-overload-nya.
Operator yang demikian dinamakan operator overloading (operator berbeban lebih).
Untuk itu kelas Currency di atas kita ubah menjadi versi ketiga berikut ini.
Program 2.12. Versi 3 : Kelas Currency dengan menggunakan Operator overloading |
enum sign {plus, minus}; class Currency { public: Currency(sign s = plus, unsigned long d = 0, unsigned int c = 0); ~Currency() {} bool Set(sign s, unsigned long d, unsigned int c); bool Set(float a); sign Sign() const {if (amount < 0) return minus; else return plus;} unsigned long Dollars() const {if (amount < 0) return (-amount) / 100; else return amount / 100;} unsigned int Cents() const {if (amount < 0) return -amount - Dollars() * 100; else return amount - Dollars() * 100;} |
Currency operator+(const Currency& x) const; Currency& operator+=(const Currency& x) {amount += x.amount; return *this;} void Output(ostream& out) const; private: long amount; }; |
Operator overloading dari + terlihat pada (15), sedangkan += terlihat pada
(16)
Program 2.13. Implementasi method kelas Currency versi 3 menggunakan operator overloading |
Currency::Currency(sign s, unsigned long d, unsigned int c) { if (c > 99) { cerr << "Cents should be < 100" << endl; exit(1);} amount = d * 100 + c; if (s == minus) amount = -amount; } bool Currency::Set(sign s, unsigned long d, unsigned int c) { if (c > 99) return false; amount = d * 100 + c; if (s == minus) amount = -amount; return true; } bool Currency::Set(float a) { sign sgn; if (a < 0) {sgn = minus; a = -a;} else sgn = plus; amount = (a + 0.001) * 100; if (sgn == minus) amount = -amount; return true; } |
Currency Currency::operator+(const Currency& x) const { Currency y; y.amount = amount + x.amount; return y; } void Currency::Output(ostream& out) const { long a = amount; if (a < 0) { out << '-'; a = -a;} long d = a / 100; // dollars out << '$' << d << '.'; int c = a - d * 100; // cents if (c < 10) out << "0"; out << c; } // overload operator << ostream& operator<<(ostream& out, const Currency& x) {x.Output(out); return out;} // main function untuk kelas Currency versi 3 #include <iostream.h> #include "curr3.h" void main(void) { Currency g, h(plus, 3, 50), i, j; g.Set(minus, 2, 25); i.Set(-6.45); j = h + g; cout << j << endl; i += h; cout << i << endl; j = i + g + h; cout << j << endl; j = (i+=g) + h; cout << j << endl; cout << i << endl; } |
Penanganan PerkecualianKelas Currency di bawah ini telah dilengkapi dengan fasilitas penanganan
perkecualian
Program 2.14. Kelas Currency dengan penanganan kesalahan |
#include <iostream.h> #include <stdlib.h> #include "xcept.h" #include "bool.h" enum sign {plus, minus}; class Currency { public: Currency(sign s = plus, unsigned long d = 0, unsigned int c = 0); ~Currency() {} void Set(sign s, unsigned long d, unsigned int c); void Set(float a); sign Sign() const {if (amount < 0) return minus; else return plus;} unsigned long Dollars() const {if (amount < 0) return (-amount) / 100; else return amount / 100;} unsigned int Cents() const {if (amount < 0) return -amount - Dollars() * 100; else return amount - Dollars() * 100;} Currency operator+(const Currency& x) const; Currency& operator+=(const Currency& x) {amount += x.amount; return *this;} void Output(ostream& out) const; private: long amount; }; |
Program 2.15. Implementasi Kelas Currency dengan penanganan kesalahan |
Currency::Currency(sign s, unsigned long d, unsigned int c) { |
amount = d * 100 + c;
if (s == minus) amount = -amount;
}
void Currency::Set(sign s, unsigned long d, unsigned int c)
{
if (c > 99) throw BadInitializers();
amount = d * 100 + c;
if (s == minus) amount = -amount;
}
void Currency::Set(float a)
{ sign sgn;
if (a < 0) {sgn = minus; a = -a;}
else sgn = plus;
amount = (a + 0.001) * 100;
if (sgn == minus) amount = -amount;
}
Currency Currency::operator+(const Currency& x) const
{
Currency y;
y.amount = amount + x.amount;
return y;
}
void Currency::Output(ostream& out) const
{
long a = amount;
if (a < 0) {out << '-';
a = -a;}
long d = a / 100;
out << '$' << d << '.';
int c = a - d * 100;
if (c < 10) out << "0";
out << c;
}
// overload <<
ostream& operator<<(ostream& out, const Currency& x)
{x.Output(out); return out;}
#include <iostream.h>
#include "curr4.h" void main(void) { Currency g, h(plus, 3, 50), i, j; g.Set(minus, 2, 25); i.Set(-6.45); j = h + g; cout << j << endl; i += h; cout << i << endl; j = i + g + h; cout << j << endl; j = (i+=g) + h; cout << j << endl; cout << i << endl; } |
EmoticonEmoticon