谁能帮我把这个C++程序注释下

2024-11-25 18:28:28
推荐回答(1个)
回答1:

Time.h
#include
class Time
{
public:
Time(int NewH=0, int NewM=0, int NewS=0); // Time(……)构造函数,使用指定的参数初始化时间。
//前扒咐 getHour(……)、getMinute(……)、getSecond(……)获取当前的时、分、秒。
int getHour();
int getMinute();
int getSecond();
Time operator ++(); //3、为时钟类重载++运算符,计算当前时刻的下一秒是多少。
friend istream & operator>>(istream &, Time &);//1、为时钟类重载>>运算符,实现时刻的输入
friend ostream & operator<<(ostream &, Time &);//2、为时钟类重载<<运算符,实现时刻的输出
private:
int hour, minute, second; //数据成员:hour,minute,second均为整型,描述时、分、秒。
};
Time.cpp
#include"Time.h"

Time::Time(intNewH, int NewM, int NewS)
{
if(0<=NewH&& NewH
<24&& 0<=NewM&& NewM <60&&
0<=NewS&& NewS <60)
{
hour=NewH;
minute=NewM;
second=NewS;
}
else
cout << "Time error!"<< endl;
}

intTime::getHour()
{
return hour;
}

intTime::getMinute()
{
return minute;
}

intTime::getSecond()
{
return second;
}

TimeTime::operator ++()
{
second++;
if(second>=60)
{
second=second-60;
minute++;
if(minute>=60)
{
minute=minute-60;
hour++;
hour=hour%24;
}
}
return *this;
}

ostream &operator <<(ostream &out, Time & mytime)
{
out< return out;
}

istream &operator >>(istream &in, Time & mytime)
{
int NewH;
int NewM;
int NewS;
cout<慧纯<"EnterTime:"< cout<<"Hour:";
in>>NewH;
cout<<"Minute:";
in>>NewM;
cout<<"Second:";
in>>NewS;
while(!(0<=NewH&& NewH
<此埋24&& 0<=NewM&& NewM <60&&
0<=NewS&& NewS <60))
{
cout<<"Time Error!Enteragain:"< cout<<"Hour:";
in>>NewH;
cout<<"Minute";
in>>NewM;
cout<<"Second:";
in>>NewS;
}
mytime.hour = NewH;
mytime.minute = NewM;
mytime.second = NewS;
return in;
}

Mytime:
#include"Time.h"

void main()
{
Time time1;
//
1、从键盘输入9:25;35,计算它的下一时刻并输出。
2、从键盘输入13;59:59,计算它的下一时刻并输出。
3、从键盘输入23:59:59,计算它的下一时刻并输日。
cin>>time1;
cout<<"time1:"< ++time1;
cout<<"++time1:"<}