关于操作符的重载,老是出错。。
真的不习惯C++语法。。程序代码:
#include <iostream> using namespace std; class money { public: money(int _dollars, int _cents); void showmoney(); int getdollars() return dollars; int getcents() return cents; private: int dollars; int cents; }; money::money(int _dollars, int _cents){ dollars = _dollars; cents =_cents; } void money::showmoney(){ cout << "Money:" << dollars << " . " << cents << endl; } const money operator +(const money &a, const money &b){ int _dollars = a.getdollars() + b.getdollars(); int _cents = a.getcents() + b.getcents(); return money(_dollars, _cents); } int main(){ money my(100, 25); money your(200, 50); money our = my + your; our.showmoney(); return 0; }