#1951. 类与对象-课堂案例2

类与对象-课堂案例2

题目描述

《时钟类》

【时钟类】【成员函数方式重载 - 运算符】 要求:使用 class 定义一个 Clock 类。 成员变量包括:小时(hour)、分钟(minute)、秒(second)。 重载减法-运算符实现两个 Clock 对象相减得到相差的秒数。 #include using namespace std; class Clock { private: int hour; int minute; int second; public: // 构造函数 Clock(int h, int m, int s){ hour = h; minute = m; second = s; } // 重载 - 运算符(成员函数)用于把两个 Clock 对象相减,功能:this-other得相差的秒数 int operator-(Clock& other) { int thisSeconds = hour6060 + minute60 + second; int otherSeconds = other.hour6060 + other.minute60 + other.second; return thisSeconds-otherSeconds; } }; int main() { Clock c1(23,58,59); Clock c2(23,58,00); cout << c2-c1 << endl; return 0; }

输入格式

无

输出格式

(见样例)