相较前面的代码,这次增加了操作符重载,以及函数重载,并使用一个createObject方法来调用构造函数生成对象。不过没有对减法提供很好的实现。
#include
using namespace std;
///define class Clock
class Clock
{
public:
Clock();
Clock(int, int);
~Clock();
///test valid
bool test();
bool test(int h, int m);
///show the time
void showTime();
///give a prompt
void prompt();
///create object use ctor
Clock createObject();
///overload operator
friend Clock operator+(const Clock& c1, const Clock& c2);
friend Clock operator-(const Clock& c1, const Clock& c2);
friend ostream &operator<<(ostream &os, Clock &c);
private:
int hour;
int minutes;
};
///ctor
Clock::Clock()
{
}
Clock::Clock(int h, int m)
{
hour = h;
minutes = m;
}
Clock::~Clock()
{
}
///test valid
bool Clock::test()
{
if (hour >