c++设计一个类层次,基类为Date,从Date分别派生出ShortE、MediumDate和LongDate.他们各自有成员函数print(),输出当前日期。 Date输出日期格式为:04-14-2018 ShortE输出日期格式为:14-04-2018 MediumDate输出日期格式为:Apr.14,2018 LongDate输出日期格式为:April 14, 2018
时间: 2023-06-04 17:03:46 浏览: 69
好的,你的问题是关于设计一个类层次的。基类是 `Date`,从 `Date` 分别派生出 `ShortE`、`MediumDate` 和 `LongDate`,它们各自有成员函数 `print()`,用于输出当前日期。其中,`Date` 输出日期格式为 `04-14-2018`,`ShortE` 输出日期格式为 `14-04-2018`,`MediumDate` 输出日期格式为 `Apr.14,2018`,`LongDate` 输出日期格式为 `April 14, 2018`。下面是具体实现的代码:
```c++
#include <iostream>
#include <string>
class Date {
protected:
int m_month;
int m_day;
int m_year;
public:
Date(int month, int day, int year) : m_month(month), m_day(day), m_year(year) {}
virtual void print() {
std::cout << m_month << "-" << m_day << "-" << m_year;
}
};
class ShortE : public Date {
public:
ShortE(int month, int day, int year) : Date(month, day, year) {}
virtual void print() {
std::cout << m_day << "-" << m_month << "-" << m_year;
}
};
class MediumDate : public Date {
public:
MediumDate(int month, int day, int year) : Date(month, day, year) {}
virtual void print() {
std::string month_str;
switch (m_month) {
case 1:
month_str = "Jan.";
break;
case 2:
month_str = "Feb.";
break;
case 3:
month_str = "Mar.";
break;
case 4:
month_str = "Apr.";
break;
case 5:
month_str = "May";
break;
case 6:
month_str = "Jun.";
break;
case 7:
month_str = "Jul.";
break;
case 8:
month_str = "Aug.";
break;
case 9:
month_str = "Sep.";
break;
case 10:
month_str = "Oct.";
break;
case 11:
month_str = "Nov.";
break;
case 12:
month_str = "Dec.";
break;
default:
month_str = "Invalid month";
}
std::cout << month_str << m_day << "," << m_year;
}
};
class LongDate : public Date {
public:
LongDate(int month, int day, int year) : Date(month, day, year) {}
virtual void print() {
std::string month_str;
switch (m_month) {
case 1:
month_str = "January";
break;
case 2:
month_str = "February";
break;
case 3:
month_str = "March";
break;
case 4:
month_str = "April";
break;
case 5:
month_str = "May";
break;
case 6:
month_str = "June";
break;
case 7:
month_str = "July";
break;
case 8:
month_str = "August";
break;
case 9:
month_str = "September";
break;
case 10:
month_str = "October";
break;
case 11:
month_str = "November";
break;
case 12:
month_str = "December";
break;
default:
month_str = "Invalid month";
}
std::cout << month_str << " " << m_day << "," << m_year;
}
};
int main() {
Date d(4, 14, 2018);
d.print();
std::cout << std::endl;
ShortE se(4, 14, 2018);
se.print();
std::cout << std::endl;
MediumDate md(4, 14, 2018);
md.print();
std::cout << std::endl;
LongDate ld(4, 14, 2018);
ld.print();
std::cout << std::endl;
return 0;
}
```
注意,如果您计划使用此代码,请使用自己的日期类进行适当修改和归因。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)