#include<iostream>
#include<string>
using namespace std;
class Time{
public:
Time(int h=0,int m=0,int s=0);
//定义构造函数
Time operator +(Time&);
//运算符“+”重载为友元函数
void disptime(string);
//定义普通输出成员函数
private:
int hour;
int minutes;
int seconds;
};
Time::Time(int h,int m,int s)
//构造函数
{
hour=h;
minutes=m;
seconds=s;
}
Time Time::operator +(Time& t)
//运算符“+”重载为友元函数
{
Time temp;
temp.seconds=seconds+t.seconds;
temp.minutes=minutes+t.minutes;
temp.hour=hour+t.hour;
if(temp.seconds >=60)
{
temp.minutes=temp.minutes+1;
temp.seconds=temp.seconds-60;
}
if(temp.minutes >=60)
{
temp.hour=temp.hour+1;
temp.minutes=temp.minutes-60;
}
return temp;
}
void Time::disptime(string str)
//输出成员函数
{
cout<<str;
cout<<hour<<"时"<<minutes<<"分"<<seconds<<"秒"<<endl;
}
int main()
{
Time t1(8,2,32),t2(9,4,42),t3;
//定义对象t1,t2,t3
t3=t1+t2;
t1.disptime("时间t1为:
");
t2.disptime("时间t2为:
");
t3.disptime("t1和t2时间之和为:
");
return 0;
}
最后
以上就是满意草丛最近收集整理的关于编写一个程序,实行两个时间相加的全部内容,更多相关编写一个程序内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复