写一个图书管理系统c++
时间: 2023-07-30 15:10:56 浏览: 116
好的,这里是一个简单的图书管理系统的C++代码示例:
```c++
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Book {
public:
string title;
string author;
int year;
Book(string title, string author, int year) {
this->title = title;
this->author = author;
this->year = year;
}
};
class Library {
public:
vector<Book> books;
void addBook(Book book) {
books.push_back(book);
}
void removeBook(string title) {
for (int i = 0; i < books.size(); i++) {
if (books[i].title == title) {
books.erase(books.begin() + i);
break;
}
}
}
void displayBooks() {
for (int i = 0; i < books.size(); i++) {
cout << "Title: " << books[i].title << endl;
cout << "Author: " << books[i].author << endl;
cout << "Year: " << books[i].year << endl;
cout << endl;
}
}
};
int main() {
Library library;
Book book1("The Catcher in the Rye", "J.D. Salinger", 1951);
Book book2("To Kill a Mockingbird", "Harper Lee", 1960);
Book book3("1984", "George Orwell", 1949);
library.addBook(book1);
library.addBook(book2);
library.addBook(book3);
cout << "Displaying all books in library:" << endl;
library.displayBooks();
cout << "Removing 'To Kill a Mockingbird' from library." << endl;
library.removeBook("To Kill a Mockingbird");
cout << "Displaying all books in library:" << endl;
library.displayBooks();
return 0;
}
```
这个图书管理系统使用了两个类,`Book`和`Library`。`Book`类表示一本书,包含书名、作者和出版年份等属性。`Library`类表示整个图书馆,包含一个`vector`存储所有书籍,还实现了添加、删除和展示书籍的功能。
在`main`函数中,我们创建了三本书,并将它们添加到图书馆中。然后展示了所有书籍,再删除了其中一本,最后再次展示所有书籍。
这只是一个简单的示例,实际的图书管理系统还需要考虑更多的功能和细节。
阅读全文