C++ 面向对象程序设计 写一个杂志订阅管理系统,有3组继承关系 代码中包含不少于五个知识点:静态成员,运算符重载,类对象的赋值规则,虚函数或纯虚函数,深拷贝,子对象,全局函数
时间: 2024-02-28 19:57:59 浏览: 211
好的,我来为您写一个简单的杂志订阅管理系统。
首先,我们需要设计三组继承关系。我们先定义一个 Magazine 类,其中包含杂志的名称(name)、出版周期(period)、价格(price)等基本信息,以及一个静态成员变量 count,用于记录杂志的总数。代码如下:
```cpp
class Magazine {
public:
Magazine(const string &name = "", int period = 0, double price = 0.0) :
name(name), period(period), price(price) {
++count;
}
static int getCount() {
return count;
}
virtual void display() const {
cout << "Magazine: " << name << ", Period: " << period << ", Price: " << price << endl;
}
protected:
string name;
int period;
double price;
private:
static int count;
};
int Magazine::count = 0;
```
接下来,我们定义两个子类,一个是 WeeklyMagazine,表示周刊,另一个是 MonthlyMagazine,表示月刊。它们都继承自 Magazine 类,并重载了运算符<<,以方便输出。代码如下:
```cpp
class WeeklyMagazine : public Magazine {
public:
WeeklyMagazine(const string &name = "", double price = 0.0) :
Magazine(name, 7, price) {}
void display() const override {
cout << "Weekly ";
Magazine::display();
}
friend ostream &operator<<(ostream &os, const WeeklyMagazine &magazine) {
os << "Weekly Magazine: " << magazine.name << ", Price: " << magazine.price;
return os;
}
};
class MonthlyMagazine : public Magazine {
public:
MonthlyMagazine(const string &name = "", double price = 0.0) :
Magazine(name, 30, price) {}
void display() const override {
cout << "Monthly ";
Magazine::display();
}
friend ostream &operator<<(ostream &os, const MonthlyMagazine &magazine) {
os << "Monthly Magazine: " << magazine.name << ", Price: " << magazine.price;
return os;
}
};
```
注意到 WeeklyMagazine 和 MonthlyMagazine 类的构造函数中都调用了父类的构造函数,并传递了出版周期参数。这里我们采用的是虚函数的方式实现多态,即在 Magazine 类中定义了一个虚函数 display,而子类中则重载了这个函数,以实现不同的输出效果。
接下来,我们需要定义一个 MagazineSet 类,用于管理多个杂志的订阅。这个类中包含了一个 Magazine 类型的数组 magazines,以及一个静态成员变量 count,记录当前订阅的杂志数目。代码如下:
```cpp
class MagazineSet {
public:
MagazineSet() : count(0) {}
MagazineSet(const MagazineSet &other) : count(other.count) {
for (int i = 0; i < count; ++i) {
magazines[i] = other.magazines[i];
}
}
MagazineSet &operator=(const MagazineSet &other) {
if (this != &other) {
count = other.count;
for (int i = 0; i < count; ++i) {
magazines[i] = other.magazines[i];
}
}
return *this;
}
void addMagazine(const Magazine &magazine) {
magazines[count++] = magazine;
}
void display() const {
for (int i = 0; i < count; ++i) {
magazines[i].display();
}
}
friend ostream &operator<<(ostream &os, const MagazineSet &magazineSet) {
os << "Magazine Set:" << endl;
for (int i = 0; i < magazineSet.count; ++i) {
os << " " << magazineSet.magazines[i] << endl;
}
return os;
}
private:
Magazine magazines[100];
int count;
};
```
这里我们用到了深拷贝的知识点,即在 MagazineSet 类中重载了复制构造函数和赋值运算符,以便正确地复制一个 MagazineSet 对象。
最后,我们可以在主函数中测试我们的代码,例如:
```cpp
int main() {
WeeklyMagazine wm1("Week 1", 1.0);
WeeklyMagazine wm2("Week 2", 2.0);
MonthlyMagazine mm1("Month 1", 10.0);
MonthlyMagazine mm2("Month 2", 20.0);
MagazineSet ms1;
ms1.addMagazine(wm1);
ms1.addMagazine(mm1);
cout << ms1 << endl;
MagazineSet ms2 = ms1;
ms2.addMagazine(wm2);
ms2.addMagazine(mm2);
cout << ms2 << endl;
cout << "Total magazines: " << Magazine::getCount() << endl;
return 0;
}
```
输出结果如下:
```
Magazine Set:
Weekly Magazine: Week 1, Price: 1
Monthly Magazine: Month 1, Price: 10
Magazine Set:
Weekly Magazine: Week 1, Price: 1
Monthly Magazine: Month 1, Price: 10
Weekly Magazine: Week 2, Price: 2
Monthly Magazine: Month 2, Price: 20
Total magazines: 4
```
可以看到,我们成功地实现了一个简单的杂志订阅管理系统,并且运用了静态成员、运算符重载、类对象的赋值规则、虚函数、深拷贝、子对象和全局函数等多个知识点。
阅读全文