运用C++语言描述书籍类、图书类型类、图书类,每一个类应包含数据成员和成员函数。设计基类和派生类,并运用多态性和虚函数的知识,帮我写一份图书管理系统,能在控制台进行操作,要具有增删改查,保存和读入功能。
时间: 2024-03-04 22:49:27 浏览: 78
好的,我来为您解答。
首先,我们需要设计三个类:书籍类、图书类型类、图书类。每一个类都应该包含数据成员和成员函数,如下所示:
```c++
class BookType {
public:
int typeId; // 图书类型ID
string typeName; // 图书类型名称
};
class Book {
public:
int bookId; // 图书ID
string bookName; // 图书名称
int typeId; // 图书类型ID
double price; // 图书价格
};
class BookManager {
public:
vector<BookType> bookTypes; // 图书类型列表
vector<Book> books; // 图书列表
void addBookType(BookType bookType); // 添加图书类型
void updateBookType(BookType bookType); // 更新图书类型
void deleteBookType(int typeId); // 删除图书类型
void addBook(Book book); // 添加图书
void updateBook(Book book); // 更新图书
void deleteBook(int bookId); // 删除图书
void saveToFile(); // 保存数据到文件
void loadFromFile(); // 从文件读取数据
};
```
接着,我们需要设计基类和派生类,并运用多态性和虚函数的知识来实现图书管理系统。我们可以将图书类型类和图书类作为基类,派生出具体的图书类型和图书,如下所示:
```c++
class BookBase {
public:
virtual void input() = 0; // 输入
virtual void output() = 0; // 输出
};
class BookTypeBase : public BookBase {
public:
int typeId; // 图书类型ID
string typeName; // 图书类型名称
virtual void input(); // 输入图书类型信息
virtual void output(); // 输出图书类型信息
};
class BookBase : public BookBase {
public:
int bookId; // 图书ID
string bookName; // 图书名称
int typeId; // 图书类型ID
double price; // 图书价格
virtual void input(); // 输入图书信息
virtual void output(); // 输出图书信息
};
class ComputerBook : public Book {
public:
string programmingLanguage; // 编程语言
virtual void input(); // 输入计算机类图书信息
virtual void output(); // 输出计算机类图书信息
};
class Novel : public Book {
public:
string author; // 作者
string publisher; // 出版社
virtual void input(); // 输入小说类图书信息
virtual void output(); // 输出小说类图书信息
};
```
最后,我们可以在控制台实现图书管理系统的增删改查、保存和读入功能。具体实现可以参考下面的代码:
```c++
void BookTypeBase::input() {
cout << "请输入图书类型ID:";
cin >> typeId;
cout << "请输入图书类型名称:";
cin >> typeName;
}
void BookTypeBase::output() {
cout << "图书类型ID:" << typeId << endl;
cout << "图书类型名称:" << typeName << endl;
}
void BookBase::input() {
cout << "请输入图书ID:";
cin >> bookId;
cout << "请输入图书名称:";
cin >> bookName;
cout << "请输入图书类型ID:";
cin >> typeId;
cout << "请输入图书价格:";
cin >> price;
}
void BookBase::output() {
cout << "图书ID:" << bookId << endl;
cout << "图书名称:" << bookName << endl;
cout << "图书类型ID:" << typeId << endl;
cout << "图书价格:" << price << endl;
}
void ComputerBook::input() {
Book::input();
cout << "请输入编程语言:";
cin >> programmingLanguage;
}
void ComputerBook::output() {
Book::output();
cout << "编程语言:" << programmingLanguage << endl;
}
void Novel::input() {
Book::input();
cout << "请输入作者:";
cin >> author;
cout << "请输入出版社:";
cin >> publisher;
}
void Novel::output() {
Book::output();
cout << "作者:" << author << endl;
cout << "出版社:" << publisher << endl;
}
void BookManager::addBookType(BookType bookType) {
bookTypes.push_back(bookType);
}
void BookManager::updateBookType(BookType bookType) {
for (int i = 0; i < bookTypes.size(); i++) {
if (bookTypes[i].typeId == bookType.typeId) {
bookTypes[i] = bookType;
break;
}
}
}
void BookManager::deleteBookType(int typeId) {
for (int i = 0; i < bookTypes.size(); i++) {
if (bookTypes[i].typeId == typeId) {
bookTypes.erase(bookTypes.begin() + i);
break;
}
}
}
void BookManager::addBook(Book book) {
books.push_back(book);
}
void BookManager::updateBook(Book book) {
for (int i = 0; i < books.size(); i++) {
if (books[i].bookId == book.bookId) {
books[i] = book;
break;
}
}
}
void BookManager::deleteBook(int bookId) {
for (int i = 0; i < books.size(); i++) {
if (books[i].bookId == bookId) {
books.erase(books.begin() + i);
break;
}
}
}
void BookManager::saveToFile() {
ofstream ofs("book.dat", ios::out);
if (!ofs) {
cout << "保存数据失败!" << endl;
return;
}
ofs << bookTypes.size() << endl;
for (int i = 0; i < bookTypes.size(); i++) {
ofs << bookTypes[i].typeId << " " << bookTypes[i].typeName << endl;
}
ofs << books.size() << endl;
for (int i = 0; i < books.size(); i++) {
ofs << books[i].bookId << " " << books[i].bookName << " " << books[i].typeId << " " << books[i].price << endl;
}
ofs.close();
}
void BookManager::loadFromFile() {
ifstream ifs("book.dat", ios::in);
if (!ifs) {
cout << "读取数据失败!" << endl;
return;
}
int typeCount;
ifs >> typeCount;
for (int i = 0; i < typeCount; i++) {
BookType bookType;
ifs >> bookType.typeId >> bookType.typeName;
bookTypes.push_back(bookType);
}
int bookCount;
ifs >> bookCount;
for (int i = 0; i < bookCount; i++) {
Book book;
ifs >> book.bookId >> book.bookName >> book.typeId >> book.price;
books.push_back(book);
}
ifs.close();
}
int main() {
BookManager bookManager;
// 从文件读取数据
bookManager.loadFromFile();
while (true) {
// 显示菜单
cout << "请选择操作:" << endl;
cout << "1. 添加图书类型" << endl;
cout << "2. 更新图书类型" << endl;
cout << "3. 删除图书类型" << endl;
cout << "4. 添加图书" << endl;
cout << "5. 更新图书" << endl;
cout << "6. 删除图书" << endl;
cout << "7. 查询图书" << endl;
cout << "8. 保存数据到文件" << endl;
cout << "9. 退出" << endl;
// 获取用户输入
int choice;
cin >> choice;
switch (choice) {
case 1: // 添加图书类型
BookType bookType;
bookType.input();
bookManager.addBookType(bookType);
break;
case 2: // 更新图书类型
cout << "请输入要更新的图书类型ID:";
int typeId;
cin >> typeId;
for (int i = 0; i < bookManager.bookTypes.size(); i++) {
if (bookManager.bookTypes[i].typeId == typeId) {
BookType bookType;
bookType.input();
bookType.typeId = typeId;
bookManager.updateBookType(bookType);
break;
}
}
break;
case 3: // 删除图书类型
cout << "请输入要删除的图书类型ID:";
cin >> typeId;
bookManager.deleteBookType(typeId);
break;
case 4: // 添加图书
cout << "请选择图书类型:" << endl;
for (int i = 0; i < bookManager.bookTypes.size(); i++) {
cout << bookManager.bookTypes[i].typeId << " " << bookManager.bookTypes[i].typeName << endl;
}
cin >> typeId;
Book book;
book.input();
if (typeId == 1) { // 计算机类图书
ComputerBook computerBook;
computerBook.input();
computerBook.bookId = book.bookId;
computerBook.bookName = book.bookName;
computerBook.typeId = book.typeId;
computerBook.price = book.price;
bookManager.addBook(computerBook);
} else if (typeId == 2) { // 小说类图书
Novel novel;
novel.input();
novel.bookId = book.bookId;
novel.bookName = book.bookName;
novel.typeId = book.typeId;
novel.price = book.price;
bookManager.addBook(novel);
} else {
bookManager.addBook(book);
}
break;
case 5: // 更新图书
cout << "请输入要更新的图书ID:";
int bookId;
cin >> bookId;
for (int i = 0; i < bookManager.books.size(); i++) {
if (bookManager.books[i].bookId == bookId) {
Book book;
book.input();
book.bookId = bookId;
bookManager.updateBook(book);
break;
}
}
break;
case 6: // 删除图书
cout << "请输入要删除的图书ID:";
cin >> bookId;
bookManager.deleteBook(bookId);
break;
case 7: // 查询图书
cout << "请输入要查询的图书名称:";
string bookName;
cin >> bookName;
for (int i = 0; i < bookManager.books.size(); i++) {
if (bookManager.books[i].bookName == bookName) {
BookBase* book = nullptr;
if (bookManager.books[i].typeId == 1) { // 计算机类图书
ComputerBook* computerBook = new ComputerBook;
*computerBook = static_cast<ComputerBook>(bookManager.books[i]);
book = computerBook;
} else if (bookManager.books[i].typeId == 2) { // 小说类图书
Novel* novel = new Novel;
*novel = static_cast<Novel>(bookManager.books[i]);
book = novel;
} else {
book = new Book;
*book = bookManager.books[i];
}
book->output();
}
}
break;
case 8: // 保存数据到文件
bookManager.saveToFile();
break;
case 9: // 退出
return 0;
default: // 输入错误
cout << "输入错误,请重新输入!" << endl;
break;
}
}
return 0;
}
```
以上就是一个简单的图书管理系统的实现,希望能对您有所帮助。
阅读全文