首先,创建一个基类 Book,包含书的基本信息,如书名、作者、出版社等。这个类可以包含构造函数、析构函数、复制构造函数等成员函数。为了实现多态输出,可以在这个类中编写一个虚函数 print(),用于输...

好的,我来为你编写一个例子。请注意,这仅是一个示例,你可能需要根据你自己的需求进行更改。

``` #include #include

using namespace std;

// 基类 Book class Book { public: // 构造函数 Book(string name, string author, string publisher) : m_name(name), m_author(author), m_publisher(publisher) { }

// 析构函数
virtual ~Book()
{
}

// 复制构造函数
Book(const Book &other)
    : m_name(other.m_name), m_author(other.m_author), m_publisher(other.m_publisher)
{
}

// 虚函数 print()
virtual void print()
{
    cout << "书名: " << m_name << endl;
    cout << "作者: " << m_author << endl;
    cout << "出版社: " << m_publisher << endl;
}

private: string m_name; // 书名 string m_author; // 作者 string m_publisher; // 出版社 };

// 派生类 EBook,继承自基类 Book class EBook : public Book { public: // 构造函数 EBook(string name, string author, string publisher, string format) : Book(name, author, publisher), m_format(format) { }

// 析构函数
virtual ~EBook()
{
}

// 复制构造函数
EBook(const EBook &amp;other)
    : Book(other), m_format(other.m_format)
{
}

// 虚函数 print()
void print()
{
    Book::print();   // 调用基类的 print() 函数
    cout << "文件格式: " << m_format << endl;
}

private: string m_format; // 电子书的文件格式 };

// 静态函数 menu() static void menu() { cout << "1. 添加图书" << endl; cout << "2. 删除图书" << endl; cout << "3. 输出所有图书

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值