代码:
#include <iostream>
class Time {
...
public:
Time Sum(const Time& s)const;
//或
Time operator+(const Time & s)const;
//重载+运算符,以便对两个对象的私有成员进行相加操作。
};
int main()
{
Time coding;
Time fixing;
Time total;
total = coding.Sum(fixing);
//Sum函数返回值为类对象,因此会创建临时对象,并赋给total。
//或
total = coding.operator + (fixing);
//可以像调用Sum()那样调用oprator+()。
//或
total = coding + fixing;
//左侧为调用对象,右侧为参数对象。
}