#include <time.h>
#include <stdlib.h>
#include <stdio.h>
class Time
{
public:
Time(int h , int s , int m)
{
this->h = h;
this->s = s;
this->m = m;
}
protected:
int h,s,m;
public:
void Print()
{
printf("%d小时 %d分钟 %d秒\n" , h , s , m);
}
Time operator+(Time &time1)
{
Time Tim(0,0,0);
int h = time1.h + this->h;
int s = time1.s + this->s;
int m = time1.m + this->m;
if (m > 59)
{
s += 1;
m -= 60;
}
if (s > 59)
{
h +=1;
s -= 60;
}
if (h > 23)
{
h -= 24;
}
Tim.h = h;
Tim.s = s;
Tim.m = m;
return Tim;
}
};
void main()
{
Time tim1(10,20,35) , tim2(11,50,48),tim3(0,0,0);
tim1.Print();
tim2.Print();
tim3 = tim1 + tim2;
printf("\n");
printf("相加结果为:\n");
tim3.Print();
printf("\n");
printf("\n");
}
#include <stdlib.h>
#include <stdio.h>
class Time
{
public:
Time(int h , int s , int m)
{
this->h = h;
this->s = s;
this->m = m;
}
protected:
int h,s,m;
public:
void Print()
{
printf("%d小时 %d分钟 %d秒\n" , h , s , m);
}
Time operator+(Time &time1)
{
Time Tim(0,0,0);
int h = time1.h + this->h;
int s = time1.s + this->s;
int m = time1.m + this->m;
if (m > 59)
{
s += 1;
m -= 60;
}
if (s > 59)
{
h +=1;
s -= 60;
}
if (h > 23)
{
h -= 24;
}
Tim.h = h;
Tim.s = s;
Tim.m = m;
return Tim;
}
};
void main()
{
Time tim1(10,20,35) , tim2(11,50,48),tim3(0,0,0);
tim1.Print();
tim2.Print();
tim3 = tim1 + tim2;
printf("\n");
printf("相加结果为:\n");
tim3.Print();
printf("\n");
printf("\n");
}