完成CTime时间类和CDate日期类的设计,使得主程序能够正确运行。 时间类CTime包含私有成员数据时(hour)分(minute)秒(second),均为int类型。 CDate日期类由CTime时间类公有派生而来,包含私有成员数据年(year)月(month)日(day),均为int类型。
时间: 2024-01-24 07:02:48 浏览: 116
好的,我理解了您的要求。以下是CTime时间类和CDate日期类的设计:
```C++
class CTime {
private:
int hour;
int minute;
int second;
public:
CTime(int h, int m, int s) {
hour = h;
minute = m;
second = s;
}
int getHour() const {
return hour;
}
int getMinute() const {
return minute;
}
int getSecond() const {
return second;
}
void setHour(int h) {
hour = h;
}
void setMinute(int m) {
minute = m;
}
void setSecond(int s) {
second = s;
}
};
class CDate : public CTime {
private:
int year;
int month;
int day;
public:
CDate(int y, int m, int d, int h, int mi, int s) : CTime(h, mi, s) {
year = y;
month = m;
day = d;
}
int getYear() const {
return year;
}
int getMonth() const {
return month;
}
int getDay() const {
return day;
}
void setYear(int y) {
year = y;
}
void setMonth(int m) {
month = m;
}
void setDay(int d) {
day = d;
}
};
```
主程序可以使用以下方式创建并使用这些类:
```C++
int main() {
CDate date(2021, 7, 28, 10, 30, 0);
cout << "Date: " << date.getYear() << "-" << date.getMonth() << "-" << date.getDay() << endl;
cout << "Time: " << date.getHour() << ":" << date.getMinute() << ":" << date.getSecond() << endl;
date.setYear(2022);
date.setMonth(8);
date.setDay(29);
date.setHour(11);
date.setMinute(31);
date.setSecond(1);
cout << "Date: " << date.getYear() << "-" << date.getMonth() << "-" << date.getDay() << endl;
cout << "Time: " << date.getHour() << ":" << date.getMinute() << ":" << date.getSecond() << endl;
return 0;
}
```
输出结果如下:
```
Date: 2021-7-28
Time: 10:30:0
Date: 2022-8-29
Time: 11:31:1
```
以上是CTime时间类和CDate日期类的设计,请您仔细查看并测试运行。
阅读全文