定义一个时间类Time
来源:百度知道 编辑:UC知道 时间:2024/05/22 10:42:58
定义一个时间类Time,它能表示分,秒,时,并提供以下操作:
1.Time(int h,int m,int s);//构造函数
2.set(int h,int m,int s);//调整时间
3.increment();//时间增加1秒
4.display();//显示时间
5.equal(Time &other_time);//比较是否与某个时间相等
6.less_than(Time &other_time);比较是否早于某个时间
我自己看的c++类这一块知识..不是很清楚我写的对不对,希望大家帮忙看看:
class Time
{int hour,minute,second;
public:
Time(int h,int m,int s)//构造函数
{
if(h<0||h>=60||m<0||m>=60||s<0||s>=60)
{cerr<<"Time isn't right!\n"; exit(-1);}
hour=h;minute=m;second=s;
}
set(int h,int m,int s)//调整时间
{
if(h<0||h>=60||m<0||m>=60||s<0||s>=60)
{cerr<<"Time isn't right!\n"; exit(-1);}
hour=h;minute=m;second=s;
}
increment()//时间增加1秒
{second++:
1.Time(int h,int m,int s);//构造函数
2.set(int h,int m,int s);//调整时间
3.increment();//时间增加1秒
4.display();//显示时间
5.equal(Time &other_time);//比较是否与某个时间相等
6.less_than(Time &other_time);比较是否早于某个时间
我自己看的c++类这一块知识..不是很清楚我写的对不对,希望大家帮忙看看:
class Time
{int hour,minute,second;
public:
Time(int h,int m,int s)//构造函数
{
if(h<0||h>=60||m<0||m>=60||s<0||s>=60)
{cerr<<"Time isn't right!\n"; exit(-1);}
hour=h;minute=m;second=s;
}
set(int h,int m,int s)//调整时间
{
if(h<0||h>=60||m<0||m>=60||s<0||s>=60)
{cerr<<"Time isn't right!\n"; exit(-1);}
hour=h;minute=m;second=s;
}
increment()//时间增加1秒
{second++:
有点小问题就是有几个函数缺少返回类型.
需要这样写:
void set(int h,int m,int s)
void increment()//时间增加1秒
void display()//显示时间
只是看了一下其他的没运行不知道