帮我用C++实现一个图书管理系统,运用C++语言描述书籍类、图书类型类、图书类,每一个类应包含数据成员和成员函数。设计基类和派生类,并运用多态性和虚函数的知识。
时间: 2024-03-04 16:49:44 浏览: 120
图书管理系统,一个用C++写的小程序
好的,下面是一个简单的图书管理系统的实现,包含书籍类、图书类型类、图书类以及基类和派生类的设计。
首先,我们需要设计三个类:书籍类、图书类型类和图书类。
1. 书籍类
书籍类包含以下数据成员:
- 书籍编号
- 书名
- 作者
- 出版社
- 出版日期
书籍类包含以下成员函数:
- 构造函数:用于初始化数据成员
- 获取书籍编号、书名、作者、出版社、出版日期的函数
- 设置书籍编号、书名、作者、出版社、出版日期的函数
```c++
class Book{
private:
string bookID;
string bookName;
string author;
string publisher;
string publishDate;
public:
Book(string id, string name, string author, string publisher, string date);
string getBookID();
string getBookName();
string getAuthor();
string getPublisher();
string getPublishDate();
void setBookID(string id);
void setBookName(string name);
void setAuthor(string author);
void setPublisher(string publisher);
void setPublishDate(string date);
};
Book::Book(string id, string name, string author, string publisher, string date){
bookID = id;
bookName = name;
this->author = author;
this->publisher = publisher;
publishDate = date;
}
string Book::getBookID(){
return bookID;
}
string Book::getBookName(){
return bookName;
}
string Book::getAuthor(){
return author;
}
string Book::getPublisher(){
return publisher;
}
string Book::getPublishDate(){
return publishDate;
}
void Book::setBookID(string id){
bookID = id;
}
void Book::setBookName(string name){
bookName = name;
}
void Book::setAuthor(string author){
this->author = author;
}
void Book::setPublisher(string publisher){
this->publisher = publisher;
}
void Book::setPublishDate(string date){
publishDate = date;
}
```
2. 图书类型类
图书类型类包含以下数据成员:
- 图书类型编号
- 图书类型名称
图书类型类包含以下成员函数:
- 构造函数:用于初始化数据成员
- 获取图书类型编号、图书类型名称的函数
- 设置图书类型编号、图书类型名称的函数
```c++
class BookType{
private:
string typeID;
string typeName;
public:
BookType(string id, string name);
string getTypeID();
string getTypeName();
void setTypeID(string id);
void setTypeName(string name);
};
BookType::BookType(string id, string name){
typeID = id;
typeName = name;
}
string BookType::getTypeID(){
return typeID;
}
string BookType::getTypeName(){
return typeName;
}
void BookType::setTypeID(string id){
typeID = id;
}
void BookType::setTypeName(string name){
typeName = name;
}
```
3. 图书类
图书类包含以下数据成员:
- 书籍类对象
- 图书类型类对象
- 借出日期
- 借出状态
图书类包含以下成员函数:
- 构造函数:用于初始化数据成员
- 获取书籍类对象、图书类型类对象、借出日期、借出状态的函数
- 设置借出日期、借出状态的函数
```c++
class BookItem{
private:
Book book;
BookType bookType;
string borrowedDate;
string borrowedStatus;
public:
BookItem(Book book, BookType bookType);
Book getBook();
BookType getBookType();
string getBorrowedDate();
string getBorrowedStatus();
void setBorrowedDate(string date);
void setBorrowedStatus(string status);
};
BookItem::BookItem(Book book, BookType bookType){
this->book = book;
this->bookType = bookType;
}
Book BookItem::getBook(){
return book;
}
BookType BookItem::getBookType(){
return bookType;
}
string BookItem::getBorrowedDate(){
return borrowedDate;
}
string BookItem::getBorrowedStatus(){
return borrowedStatus;
}
void BookItem::setBorrowedDate(string date){
borrowedDate = date;
}
void BookItem::setBorrowedStatus(string status){
borrowedStatus = status;
}
```
接下来,我们需要设计基类和派生类,并运用多态性和虚函数的知识。
基类是一个抽象类,包含以下纯虚函数:
- 添加函数:用于添加书籍、图书类型、图书信息
- 修改函数:用于修改书籍、图书类型、图书信息
- 删除函数:用于删除书籍、图书类型、图书信息
- 查找函数:用于查找书籍、图书类型、图书信息
- 输出函数:用于输出书籍、图书类型、图书信息
- 排序函数:用于对图书信息按编号、书名排序
- 保存函数:用于将图书信息保存为文件
```c++
class LibrarySystem {
public:
virtual void addBook(Book book) = 0;
virtual void addBookType(BookType bookType) = 0;
virtual void addBookItem(BookItem bookItem) = 0;
virtual void modifyBook(Book book) = 0;
virtual void modifyBookType(BookType bookType) = 0;
virtual void modifyBookItem(BookItem bookItem) = 0;
virtual void deleteBook(string bookID) = 0;
virtual void deleteBookType(string typeID) = 0;
virtual void deleteBookItem(string bookID) = 0;
virtual void searchBook(string keyword) = 0;
virtual void searchBookType(string keyword) = 0;
virtual void searchBookItem(string keyword) = 0;
virtual void sortBookItemByID() = 0;
virtual void sortBookItemByName() = 0;
virtual void saveToFile() = 0;
};
```
派生类是实际的类,继承基类并实现基类的纯虚函数。
例如,派生类可以是图书管理类,包含以下成员函数:
- 添加函数:用于添加书籍、图书类型、图书信息
- 修改函数:用于修改书籍、图书类型、图书信息
- 删除函数:用于删除书籍、图书类型、图书信息
- 查找函数:用于查找书籍、图书类型、图书信息
- 输出函数:用于输出书籍、图书类型、图书信息
- 排序函数:用于对图书信息按编号、书名排序
- 保存函数:用于将图书信息保存为文件
```c++
class BookManagement : public LibrarySystem {
private:
vector<Book> books;
vector<BookType> bookTypes;
vector<BookItem> bookItems;
public:
void addBook(Book book);
void addBookType(BookType bookType);
void addBookItem(BookItem bookItem);
void modifyBook(Book book);
void modifyBookType(BookType bookType);
void modifyBookItem(BookItem bookItem);
void deleteBook(string bookID);
void deleteBookType(string typeID);
void deleteBookItem(string bookID);
void searchBook(string keyword);
void searchBookType(string keyword);
void searchBookItem(string keyword);
void sortBookItemByID();
void sortBookItemByName();
void saveToFile();
};
void BookManagement::addBook(Book book){
books.push_back(book);
}
void BookManagement::addBookType(BookType bookType){
bookTypes.push_back(bookType);
}
void BookManagement::addBookItem(BookItem bookItem){
bookItems.push_back(bookItem);
}
void BookManagement::modifyBook(Book book){
for(int i=0; i<books.size(); i++){
if(books[i].getBookID() == book.getBookID()){
books[i] = book;
break;
}
}
}
void BookManagement::modifyBookType(BookType bookType){
for(int i=0; i<bookTypes.size(); i++){
if(bookTypes[i].getTypeID() == bookType.getTypeID()){
bookTypes[i] = bookType;
break;
}
}
}
void BookManagement::modifyBookItem(BookItem bookItem){
for(int i=0; i<bookItems.size(); i++){
if(bookItems[i].getBook().getBookID() == bookItem.getBook().getBookID()){
bookItems[i] = bookItem;
break;
}
}
}
void BookManagement::deleteBook(string bookID){
for(int i=0; i<books.size(); i++){
if(books[i].getBookID() == bookID){
books.erase(books.begin() + i);
break;
}
}
}
void BookManagement::deleteBookType(string typeID){
for(int i=0; i<bookTypes.size(); i++){
if(bookTypes[i].getTypeID() == typeID){
bookTypes.erase(bookTypes.begin() + i);
break;
}
}
}
void BookManagement::deleteBookItem(string bookID){
for(int i=0; i<bookItems.size(); i++){
if(bookItems[i].getBook().getBookID() == bookID){
bookItems.erase(bookItems.begin() + i);
break;
}
}
}
void BookManagement::searchBook(string keyword){
cout << "Search result for books:" << endl;
for(int i=0; i<books.size(); i++){
if(books[i].getBookID().find(keyword) != string::npos ||
books[i].getBookName().find(keyword) != string::npos ||
books[i].getAuthor().find(keyword) != string::npos ||
books[i].getPublisher().find(keyword) != string::npos ||
books[i].getPublishDate().find(keyword) != string::npos){
cout << books[i].getBookID() << " " << books[i].getBookName() << " " << books[i].getAuthor() << " " << books[i].getPublisher() << " " << books[i].getPublishDate() << endl;
}
}
}
void BookManagement::searchBookType(string keyword){
cout << "Search result for book types:" << endl;
for(int i=0; i<bookTypes.size(); i++){
if(bookTypes[i].getTypeID().find(keyword) != string::npos ||
bookTypes[i].getTypeName().find(keyword) != string::npos){
cout << bookTypes[i].getTypeID() << " " << bookTypes[i].getTypeName() << endl;
}
}
}
void BookManagement::searchBookItem(string keyword){
cout << "Search result for book items:" << endl;
for(int i=0; i<bookItems.size(); i++){
if(bookItems[i].getBook().getBookID().find(keyword) != string::npos ||
bookItems[i].getBook().getBookName().find(keyword) != string::npos ||
bookItems[i].getBook().getAuthor().find(keyword) != string::npos ||
bookItems[i].getBook().getPublisher().find(keyword) != string::npos ||
bookItems[i].getBook().getPublishDate().find(keyword) != string::npos ||
bookItems[i].getBookType().getTypeID().find(keyword) != string::npos ||
bookItems[i].getBookType().getTypeName().find(keyword) != string::npos ||
bookItems[i].getBorrowedDate().find(keyword) != string::npos ||
bookItems[i].getBorrowedStatus().find(keyword) != string::npos){
cout << bookItems[i].getBook().getBookID() << " " << bookItems[i].getBook().getBookName() << " " << bookItems[i].getBook().getAuthor() << " " << bookItems[i].getBook().getPublisher() << " " << bookItems[i].getBook().getPublishDate() << " " << bookItems[i].getBookType().getTypeID() << " " << bookItems[i].getBookType().getTypeName() << " " << bookItems[i].getBorrowedDate() << " " << bookItems[i].getBorrowedStatus() << endl;
}
}
}
void BookManagement::sortBookItemByID(){
sort(bookItems.begin(), bookItems.end(), [](BookItem a, BookItem b){
return a.getBook().getBookID() < b.getBook().getBookID();
});
}
void BookManagement::sortBookItemByName(){
sort(bookItems.begin(), bookItems.end(), [](BookItem a, BookItem b){
return
阅读全文