完成CTime时间类和CDate日期类的设计,使得主程序能够正确运行。 时间类CTime包含私有成员数据时(hour)分(minute)秒(second),均为int类型。 CDate日期类由CTime时间类公有派生而来,包含私有成员数据年(year)月(month)日(day),均为int类型。 main函数已给定,提交时只需要提交main函数外的代码部分。 #include<iostream> using namespace std; //你提交的代码在这里 int main() { int dy,dm,dd,th,tm,ts; cin>>dy>>dm>>dd>>th>>tm>>ts; CTime t1; cout<<"[T1]"; t1.Show(); CDate d1; cout<<"[D1]"; d1.Show(); CDate d2(dy,dm,dd); cout<<"[D2]"; d2.Show(); CDate d3(dy,dm,dd,th,tm,ts); cout<<"[D3]"; d3.Show(); CDate d4(dy,dm,dd,t1); cout<<"[D4]"; d4.Show(); CDate d5(t1); cout<<"[D5]"; d5.Show(); return 0; } Input 1行,包含6个整数,分别代表年,月,日,时,分和秒。 Output 输出包含4行,具体输出格式参照样例。 Sample Input 1 1949 10 1 16 28 37 Sample Output 1 Function #1 is called! [T1]9:10:11 Function #3 is called! Function #1 is called! Function #4 is called! [D1]2023-4-5 9:10:11 Function #3 is called! Function #9 is called! Function #1 is called! Function #5 is called! [D2]1949-10-1 9:10:11 Function #3 is called! Function #9 is called! Function #2 is called! Function #6 is called! [D3]1949-10-1 16:28:37 Function #3 is called! Function #9 is called! Function #0 is called! Function #7 is called! [D4]1949-10-1 9:10:11 Function #3 is called! Function #9 is called! Function #0 is called! Function #8 is called! [D5]2000-12-31 9:10:11 Function #3 is called! Function #9 is called!
时间: 2024-03-19 07:43:30 浏览: 124
```
#include<iostream>
using namespace std;
class CTime {
private:
int hour, minute, second;
public:
CTime() {
hour = 9;
minute = 10;
second = 11;
cout << "Function #1 is called! ";
}
CTime(int h, int m, int s) {
hour = h;
minute = m;
second = s;
cout << "Function #0 is called! ";
}
void Show() {
if (hour < 10) cout << 0;
cout << hour << ":";
if (minute < 10) cout << 0;
cout << minute << ":";
if (second < 10) cout << 0;
cout << second << endl;
}
};
class CDate : public CTime {
private:
int year, month, day;
public:
CDate() : CTime() {
year = 2023;
month = 4;
day = 5;
cout << "Function #2 is called! ";
}
CDate(int y, int m, int d) : CTime() {
year = y;
month = m;
day = d;
cout << "Function #6 is called! ";
}
CDate(int y, int m, int d, int h, int mi, int s) : CTime(h, mi, s) {
year = y;
month = m;
day = d;
cout << "Function #7 is called! ";
}
CDate(int y, int m, int d, CTime t) : CTime(t) {
year = y;
month = m;
day = d;
cout << "Function #8 is called! ";
}
CDate(CTime t) : CTime(t) {
year = 2000;
month = 12;
day = 31;
cout << "Function #5 is called! ";
}
void Show() {
cout << year << "-";
if (month < 10) cout << 0;
cout << month << "-";
if (day < 10) cout << 0;
cout << day << " ";
CTime::Show();
cout << "Function #9 is called! ";
}
};
int main() {
int dy, dm, dd, th, tm, ts;
cin >> dy >> dm >> dd >> th >> tm >> ts;
CTime t1;
cout << "[T1]";
t1.Show();
CDate d1;
cout << "[D1]";
d1.Show();
CDate d2(dy, dm, dd);
cout << "[D2]";
d2.Show();
CDate d3(dy, dm, dd, th, tm, ts);
cout << "[D3]";
d3.Show();
CDate d4(dy, dm, dd, t1);
cout << "[D4]";
d4.Show();
CDate d5(t1);
cout << "[D5]";
d5.Show();
return 0;
}
```
阅读全文