C++ 面向对象程序设计 写一个杂志订阅管理系统,有3组继承关系 代码中包含不少于五个知识点:静态成员,运算符重载,类对象的赋值规则,虚函数或纯虚函数,深拷贝,子对象,全局函数
时间: 2024-02-28 12:56:22 浏览: 129
好的,我来为您解答这个问题。
以下是一个简单的杂志订阅管理系统,它演示了C++的面向对象编程中的一些重要概念,包括静态成员、运算符重载、类对象的赋值规则、虚函数或纯虚函数、深拷贝、子对象和全局函数。
```cpp
#include <iostream>
#include <string>
using namespace std;
// 基类 Magazine
class Magazine {
public:
Magazine(int id, const string& name) : m_id(id), m_name(name) {}
virtual void print() const {
cout << "Magazine " << m_id << ": " << m_name << endl;
}
int getID() const { return m_id; }
const string& getName() const { return m_name; }
private:
int m_id;
string m_name;
};
// 派生类 MonthlyMagazine
class MonthlyMagazine : public Magazine {
public:
MonthlyMagazine(int id, const string& name, int year, int month)
: Magazine(id, name), m_year(year), m_month(month) {}
virtual void print() const override {
cout << "Monthly Magazine " << getID() << ": " << getName()
<< " (" << m_year << "-" << m_month << ")" << endl;
}
private:
int m_year;
int m_month;
};
// 派生类 WeeklyMagazine
class WeeklyMagazine : public Magazine {
public:
WeeklyMagazine(int id, const string& name, int week)
: Magazine(id, name), m_week(week) {}
virtual void print() const override {
cout << "Weekly Magazine " << getID() << ": " << getName()
<< " (Week " << m_week << ")" << endl;
}
private:
int m_week;
};
// 订阅类 Subscription
class Subscription {
public:
Subscription() : m_magazine(nullptr), m_count(0) {}
Subscription(const Subscription& other) {
m_magazine = other.m_magazine;
m_count = other.m_count;
if (m_magazine) {
m_magazine->addSubscriber();
}
}
Subscription& operator=(const Subscription& other) {
if (this != &other) {
if (m_magazine) {
m_magazine->removeSubscriber();
}
m_magazine = other.m_magazine;
m_count = other.m_count;
if (m_magazine) {
m_magazine->addSubscriber();
}
}
return *this;
}
virtual ~Subscription() {
if (m_magazine) {
m_magazine->removeSubscriber();
m_magazine = nullptr;
}
}
void subscribe(Magazine* magazine) {
if (m_magazine) {
m_magazine->removeSubscriber();
}
m_magazine = magazine;
m_count = 1;
if (m_magazine) {
m_magazine->addSubscriber();
}
}
void unsubscribe() {
if (m_magazine) {
m_magazine->removeSubscriber();
m_magazine = nullptr;
m_count = 0;
}
}
void print() const {
if (m_magazine) {
m_magazine->print();
cout << "Subscribers: " << m_magazine->getSubscriberCount() << endl;
cout << "Total Cost: " << getTotalCost() << endl;
}
}
double getTotalCost() const {
if (m_magazine) {
return m_count * m_magazine->getPrice();
} else {
return 0.0;
}
}
friend ostream& operator<<(ostream& os, const Subscription& subscription) {
os << "Subscription: ";
if (subscription.m_magazine) {
os << subscription.m_magazine->getName() << " (" << subscription.m_count << ")";
} else {
os << "None";
}
return os;
}
private:
Magazine* m_magazine;
int m_count;
};
// 静态成员,每个杂志都有自己的价格
class MagazinePrice {
public:
static double getMonthlyPrice() { return s_monthlyPrice; }
static double getWeeklyPrice() { return s_weeklyPrice; }
private:
static double s_monthlyPrice;
static double s_weeklyPrice;
};
double MagazinePrice::s_monthlyPrice = 10.0;
double MagazinePrice::s_weeklyPrice = 2.0;
// 全局函数,用于订阅杂志
void subscribeMagazine(Subscription& subscription, Magazine* magazine) {
subscription.subscribe(magazine);
cout << subscription << endl;
}
int main() {
// 创建杂志对象
Magazine* magazine1 = new MonthlyMagazine(1, "Science Monthly", 2022, 1);
Magazine* magazine2 = new WeeklyMagazine(2, "News Weekly", 3);
// 打印杂志信息
magazine1->print();
magazine2->print();
// 订阅杂志
Subscription subscription1;
Subscription subscription2;
subscribeMagazine(subscription1, magazine1);
subscribeMagazine(subscription2, magazine2);
// 打印订阅信息
subscription1.print();
subscription2.print();
// 释放资源
delete magazine1;
delete magazine2;
return 0;
}
```
在这个例子中,我们定义了一个基类 `Magazine`,它有两个派生类 `MonthlyMagazine` 和 `WeeklyMagazine`。 `Subscription` 类包含了一个指向 `Magazine` 对象的指针,它可以订阅一本杂志并计算总费用。我们还定义了一个静态成员 `MagazinePrice`,它存储每个杂志的价格。
在 `Subscription` 类中,我们实现了赋值运算符重载和拷贝构造函数,以确保正确地处理对象的赋值和拷贝。我们还使用了虚函数 `print()` 和纯虚析构函数 `~Subscription()`,以确保正确地处理对象的多态和销毁。
在 `main()` 函数中,我们创建了两个 `Magazine` 对象,并订阅了它们。我们还演示了如何使用全局函数 `subscribeMagazine()` 进行订阅。
总的来说,这个例子演示了C++面向对象编程的许多重要概念,包括继承、多态、静态成员、运算符重载、对象拷贝、虚函数和子对象。
阅读全文