#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)
编写一个程序,实行两个时间相加
最新推荐文章于 2022-07-12 13:33:26 发布
本文展示了一个C++程序,用于实现两个时间对象的相加操作。通过定义一个`Time`类,包括构造函数、时间相加的运算符重载友元函数以及显示时间的成员函数,实现了对小时、分钟和秒的累加,并处理了进位问题。在主函数中,创建了三个`Time`对象并展示了相加结果。
摘要由CSDN通过智能技术生成