运用C++语言描述书籍类、图书类型类、图书类,每一个类应包含数据成员和成员函数。设计基类和派生类,并运用多态性和虚函数的知识。设计要实现的主要功能如下: 1)建立书籍类、图书类型类、图书类 2)书籍、图书类型、图书信息的初始化 3)书籍、图书类型、图书信息的添加、修改、删除、查找 4)图书信息的输出 5)对图书信息按编号、书名排序 6)将图书信息保存为文件
时间: 2023-07-10 09:30:19 浏览: 92
好的,我理解了你的问题。下面是该问题的代码实现,包括三个类:Book、BookType、Library,其中Library类继承自Book和BookType类,实现了多态性和虚函数的知识。
Book类的数据成员包括书名、作者、出版社、出版日期、价格、图书编号等,其成员函数包括构造函数、析构函数、获取和设置图书信息等。BookType类的数据成员包括图书类型编号、图书类型名称等,其成员函数也包括构造函数、析构函数、获取和设置图书类型信息等。Library类继承自Book和BookType类,其数据成员包括Book和BookType的对象,其成员函数包括初始化、添加、修改、删除、查找、排序、保存为文件等。
以下是代码实现:
```c++
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <fstream>
using namespace std;
// 书籍类
class Book {
private:
string book_name; // 书名
string author; // 作者
string publisher; // 出版社
string publish_date; // 出版日期
double price; // 价格
int book_id; // 图书编号
public:
Book() {}
Book(string name, string aut, string pub, string date, double pri, int id) : book_name(name), author(aut), publisher(pub), publish_date(date), price(pri), book_id(id) {}
~Book() {}
// 获取和设置图书信息
string get_name() { return book_name; }
string get_author() { return author; }
string get_publisher() { return publisher; }
string get_publish_date() { return publish_date; }
double get_price() { return price; }
int get_id() { return book_id; }
void set_name(string name) { book_name = name; }
void set_author(string aut) { author = aut; }
void set_publisher(string pub) { publisher = pub; }
void set_publish_date(string date) { publish_date = date; }
void set_price(double pri) { price = pri; }
void set_id(int id) { book_id = id; }
};
// 图书类型类
class BookType {
private:
int type_id; // 图书类型编号
string type_name; // 图书类型名称
public:
BookType() {}
BookType(int id, string name) : type_id(id), type_name(name) {}
~BookType() {}
// 获取和设置图书类型信息
int get_id() { return type_id; }
string get_name() { return type_name; }
void set_id(int id) { type_id = id; }
void set_name(string name) { type_name = name; }
};
// 图书馆类
class Library : public Book, public BookType {
private:
vector<Book> books; // 书籍列表
vector<BookType> book_types; // 图书类型列表
public:
Library() {}
~Library() {}
// 初始化,从文件中读取图书信息和图书类型信息
void init() {
// 读取图书信息
ifstream in_book("book.txt");
string name, aut, pub, date;
double pri;
int id;
while (in_book >> name >> aut >> pub >> date >> pri >> id) {
Book book(name, aut, pub, date, pri, id);
books.push_back(book);
}
in_book.close();
// 读取图书类型信息
ifstream in_type("type.txt");
int type_id;
string type_name;
while (in_type >> type_id >> type_name) {
BookType type(type_id, type_name);
book_types.push_back(type);
}
in_type.close();
}
// 添加图书信息
void add_book() {
string name, aut, pub, date;
double pri;
int id;
cout << "请输入书名、作者、出版社、出版日期、价格、编号:" << endl;
cin >> name >> aut >> pub >> date >> pri >> id;
Book book(name, aut, pub, date, pri, id);
books.push_back(book);
}
// 修改图书信息
void modify_book() {
int id;
cout << "请输入要修改的图书编号:" << endl;
cin >> id;
for (int i = 0; i < books.size(); i++) {
if (books[i].get_id() == id) {
string name, aut, pub, date;
double pri;
cout << "请输入新的书名、作者、出版社、出版日期、价格:" << endl;
cin >> name >> aut >> pub >> date >> pri;
books[i].set_name(name);
books[i].set_author(aut);
books[i].set_publisher(pub);
books[i].set_publish_date(date);
books[i].set_price(pri);
cout << "修改成功!" << endl;
return;
}
}
cout << "找不到该图书信息!" << endl;
}
// 删除图书信息
void delete_book() {
int id;
cout << "请输入要删除的图书编号:" << endl;
cin >> id;
for (int i = 0; i < books.size(); i++) {
if (books[i].get_id() == id) {
books.erase(books.begin() + i);
cout << "删除成功!" << endl;
return;
}
}
cout << "找不到该图书信息!" << endl;
}
// 查找图书信息
void search_book() {
int id;
cout << "请输入要查找的图书编号:" << endl;
cin >> id;
for (int i = 0; i < books.size(); i++) {
if (books[i].get_id() == id) {
cout << "书名:" << books[i].get_name() << endl;
cout << "作者:" << books[i].get_author() << endl;
cout << "出版社:" << books[i].get_publisher() << endl;
cout << "出版日期:" << books[i].get_publish_date() << endl;
cout << "价格:" << books[i].get_price() << endl;
cout << "编号:" << books[i].get_id() << endl;
return;
}
}
cout << "找不到该图书信息!" << endl;
}
// 按编号排序
void sort_by_id() {
sort(books.begin(), books.end(), [](Book a, Book b) { return a.get_id() < b.get_id(); });
}
// 按书名排序
void sort_by_name() {
sort(books.begin(), books.end(), [](Book a, Book b) { return a.get_name() < b.get_name(); });
}
// 输出图书信息
void print_books() {
for (int i = 0; i < books.size(); i++) {
cout << "书名:" << books[i].get_name() << endl;
cout << "作者:" << books[i].get_author() << endl;
cout << "出版社:" << books[i].get_publisher() << endl;
cout << "出版日期:" << books[i].get_publish_date() << endl;
cout << "价格:" << books[i].get_price() << endl;
cout << "编号:" << books[i].get_id() << endl;
cout << endl;
}
}
// 保存为文件
void save() {
// 保存图书信息
ofstream out_book("book.txt");
for (int i = 0; i < books.size(); i++) {
out_book << books[i].get_name() << " " << books[i].get_author() << " " << books[i].get_publisher() << " " << books[i].get_publish_date() << " " << books[i].get_price() << " " << books[i].get_id() << endl;
}
out_book.close();
// 保存图书类型信息
ofstream out_type("type.txt");
for (int i = 0; i < book_types.size(); i++) {
out_type << book_types[i].get_id() << " " << book_types[i].get_name() << endl;
}
out_type.close();
}
};
int main() {
Library library;
library.init();
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:
library.add_book();
break;
case 2:
library.modify_book();
break;
case 3:
library.delete_book();
break;
case 4:
library.search_book();
break;
case 5:
library.sort_by_id();
break;
case 6:
library.sort_by_name();
break;
case 7:
library.print_books();
break;
case 8:
library.save();
break;
case 9:
return 0;
default:
cout << "输入错误,请重新输入!" << endl;
}
}
return 0;
}
```
阅读全文