设计一个时间类Time,要求: 1、包含时(hour),分(minute),秒(second)私有数据成员; 2、包含构造函数,重
载关于一时间加上另一时间的加法运算符"+"、重载输出运算符"<<"、重载输入运算符">>"。要求,定义完Time类后,main函数中声明对象time1,time2,tim...
载关于一时间加上另一时间的加法运算符"+"、重载输出运算符"<<"、重载输入运算符">>"。
要求,定义完Time类后,main函数中声明对象time1,time2,time3,然后实现
cin>>time1>>time2;
time3=time1+time2;
cout<<time3;
输入时间一、时间二,注意:在main函数中必须用cin>>time1>>time2实现输入
输出两个时间相加的结果,注意,在main函数中必须用cout<<time3实现输出 展开
要求,定义完Time类后,main函数中声明对象time1,time2,time3,然后实现
cin>>time1>>time2;
time3=time1+time2;
cout<<time3;
输入时间一、时间二,注意:在main函数中必须用cin>>time1>>time2实现输入
输出两个时间相加的结果,注意,在main函数中必须用cout<<time3实现输出 展开
展开全部
#include <iostream>
using namespace std;
class Time
{
public:
Time();
Time(int h, int m ,int s);
Time operator +(Time &t1);
Time operator >>(Time &t1);
Time operator <<(Time &t1);
void setHour(int h )
{
hour=h;
}
void setMinute(int m)
{
minute=m;
}
void setSecond(int s)
{
second=s;
}
int getHour()
{
return hour;
}
int getMinute()
{
return minute;
}
int getSecond()
{
return second;
}
private:
int hour;
int minute;
int second;
};
Time::Time()
{
hour=0;
minute=0;
second=0;
}
Time::Time(int h, int m ,int s)
{
hour=h;
minute=m;
second=s;
}
Time Time::operator +(Time &t1)
{
return Time(hour+t1.getHour(),minute+t1.getMinute(),second+t1.getSecond());
}
Time Time::operator >>(Time &t1)
{
int a,b,c;
cout<<"inputTime1:"<<endl;
cin>>a>>b>>c;
hour=a;
minute=b;
second=c;
cout<<"inputTime2:"<<endl;
cin>>a>>b>>c;
t1.setHour(a);
t1.setMinute(b);
t1.setSecond(c);
return Time(hour+t1.getHour(),minute+t1.getMinute(),second+t1.getSecond());
}
Time Time::operator <<(Time &t1)
{
cout<<t1.getHour()<<':'<<t1.getMinute()<<':'<<t1.getSecond()<<endl;
return Time(hour+t1.getHour(),minute+t1.getMinute(),second+t1.getSecond());
}
int main()
{
Time time1(1,2,3);
Time time2(2,3,4);
Time time3;
time3 = time1+time2;
time3 = time1>>time2;
time3<<(time1+time2);
}
using namespace std;
class Time
{
public:
Time();
Time(int h, int m ,int s);
Time operator +(Time &t1);
Time operator >>(Time &t1);
Time operator <<(Time &t1);
void setHour(int h )
{
hour=h;
}
void setMinute(int m)
{
minute=m;
}
void setSecond(int s)
{
second=s;
}
int getHour()
{
return hour;
}
int getMinute()
{
return minute;
}
int getSecond()
{
return second;
}
private:
int hour;
int minute;
int second;
};
Time::Time()
{
hour=0;
minute=0;
second=0;
}
Time::Time(int h, int m ,int s)
{
hour=h;
minute=m;
second=s;
}
Time Time::operator +(Time &t1)
{
return Time(hour+t1.getHour(),minute+t1.getMinute(),second+t1.getSecond());
}
Time Time::operator >>(Time &t1)
{
int a,b,c;
cout<<"inputTime1:"<<endl;
cin>>a>>b>>c;
hour=a;
minute=b;
second=c;
cout<<"inputTime2:"<<endl;
cin>>a>>b>>c;
t1.setHour(a);
t1.setMinute(b);
t1.setSecond(c);
return Time(hour+t1.getHour(),minute+t1.getMinute(),second+t1.getSecond());
}
Time Time::operator <<(Time &t1)
{
cout<<t1.getHour()<<':'<<t1.getMinute()<<':'<<t1.getSecond()<<endl;
return Time(hour+t1.getHour(),minute+t1.getMinute(),second+t1.getSecond());
}
int main()
{
Time time1(1,2,3);
Time time2(2,3,4);
Time time3;
time3 = time1+time2;
time3 = time1>>time2;
time3<<(time1+time2);
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询