1 Star 0 Fork 115

thornphoenix / CPlusPlusThings

forked from Yj / CPlusPlusThings 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
时钟++运算符重载.cpp 1.81 KB
一键复制 编辑 原始数据 按行查看 历史
#include<iostream>
#include<cmath>
using namespace std;
/*
* 时钟类
*/
class Clock
{
private:
int Hour, Minute, Second;
public:
Clock(int h=0, int m=0, int s=0);
void ShowTime();
Clock& operator ++();
Clock operator ++(int);
};
/*
* 时钟类构造函数
*/
Clock::Clock(int h,int m, int s)
{
if(h>=0 && h<24 && m>=0 && m<60 && s>=0 && s<60)
{
Hour = h;
Minute =m;
Second= s;
}
else
cout<<"输入的时间格式错误!"<<endl;
}
/*
* 显示时间
*/
void Clock::ShowTime()
{
cout<<Hour<<":"<<Minute<<":"<<Second<<endl;
}
/*
* 时间递增一秒(重载前缀++运算符)
*/
Clock& Clock::operator ++()
{
Second++;
if (Second >= 60)
{
Second = Second - 60;
Minute++;
if (Minute >= 60)
{
Minute = Minute - 60;
Hour++;
Hour = Hour % 24;
}
}
return *this;
}
/*
* 时间递增一秒(重载后缀++运算符)
*/
Clock Clock::operator ++(int)
{
Clock old = *this;
++(*this);
return old;
}
/*
* 主函数
*/
int main()
{
Clock myClock(23,59,59);
cout<<"初始化显示时间为:\t\t";
myClock.ShowTime();
cout<<"执行myClock++后的时间为:\t";
//先执行ShowTime(),输出myClock=23:59:59,
//再执行myClock++,此时myClock=00:00:00
(myClock++).ShowTime();
cout<<"执行++myClock后的时间为:\t";
//先执行++myClock,此时myClock=00:00:01
//再执行ShowTime(),输出myClock=00:00:01
(++myClock).ShowTime();
system("pause");
}
C++
1
https://gitee.com/thornphoenix_admin/CPlusPlusThings.git
git@gitee.com:thornphoenix_admin/CPlusPlusThings.git
thornphoenix_admin
CPlusPlusThings
CPlusPlusThings
master

搜索帮助