问题遇到的现象和发生背景
用代码块功能插入代码,请勿粘贴截图
我想要达到的结果
```c++
#include<iostream>
using namespace std;
class DateType
{
public:
int y;
int m;
int d;
void PrintDate()
{
cout << y << "-" << m << "-" << d << endl;
}
};
class TimeType
{
public:
int hr;
int mi;
int se;
};
class DateTimeType {
private://自定义的日期时间类 DateTimeType
DateType date; //类 DateType 的类对象 date 作为其数据成员
TimeType time; //类 TimeType 的类对象 time 作为其另一个数据成员
public:
DateTimeType(int y0 , int m0, int d0 , int hr0 , int mi0 , int se0 );
//构造函数,设定 DateTimeType 类对象的日期时间,并为各参数设置了默认值
DateType& GetDate() { return date; } //返回本类的私有数据对象 data
TimeType& GetTime() { return time; } //返回本类的私有数据对象 time
void IncrementSecond(int s); //增加若干秒,注意“进位”问题
void PrintDateTime(); //屏幕输出日期时间对象的有关数据
};
DateTimeType::DateTimeType(int y0 = 1, int m0 = 1, int d0 = 1, int hr0 = 0, int mi0 = 0, int se0 = 0)
{
date.y = y0;
date.m = m0;
date.d = d0;
time.hr = hr0;
time.mi = mi0;
time.se = se0;
}
void DateTimeType:: IncrementSecond(int s)
{
int temp = 0;
int m1[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
time.se += s;
if (time.se >=60)
{
temp = time.se / 60;
time.se -= temp * 60;
time.mi += temp;
if (time.mi >= 60)
{
temp = time.mi / 60;
time.mi -= temp * 60;
time.hr += temp;
if (time.hr >= 24)
{
temp = time.hr / 24;
time.hr -= temp * 24;
date.d += temp;
if (date.y % 4 == 0 && date.y % 100 == 0 || date.y % 400 == 0) { m1[2] = 29; }
while (date.d > m1[date.m])
{
date.d= date.d - m1[date.m];
date.m++;
}
if (date.m > 12)
{
temp = date.m / 12;
date.m -= temp * 12;
date.y += temp;
}
}
}
}
}
void DateTimeType:: PrintDateTime()
{
cout << date.y << "-" << date.m << "-" << date.d << ” “<<time.hr << ":" << time.mi << ":" << time.se << endl;
}
void main() {
DateTimeType dttm1(1999, 12, 31, 23, 59, 59), dttm2;
(dttm1.GetDate()).PrintDate(); //调用对象成员所属类的公有成员函数
cout << endl;
dttm1.PrintDateTime(); //调用本派生类的成员函数 PrintDateTime
dttm2.PrintDateTime();
dttm1.IncrementSecond(30); //调用本派生类成员函数
dttm1.PrintDateTime();
}