好的,我来为你编写一个例子。请注意,这仅是一个示例,你可能需要根据你自己的需求进行更改。
``` #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 &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. 输出所有图书