2. The library requires a class to store information about a book. Set up a class to hold data including title, author, year of publication, and a boolean variable to indicate if the book is in the library or if the book has been taken out. Create a method to load the data for one book. Create another method to display the data for a book. Create another method to check if a book is in the library or not. Write a main program to test your methods. Use the book class in (2) above to set up an array of book objects. Use the methods you designed in (2) above to read and display the data for several books. 用c++
时间: 2023-12-30 08:06:27 浏览: 64
以下是C++的实现代码:
```c++
#include <iostream>
#include <string>
using namespace std;
class Book {
private:
string title; // 书名
string author; // 作者
int year; // 出版年份
bool inLibrary; // 是否在图书馆
public:
void loadBook(string t, string a, int y, bool l) {
title = t;
author = a;
year = y;
inLibrary = l;
}
void displayBook() {
cout << "Title: " << title << endl;
cout << "Author: " << author << endl;
cout << "Year of Publication: " << year << endl;
if (inLibrary)
cout << "Status: In Library" << endl;
else
cout << "Status: Taken Out" << endl;
}
bool checkLibrary() {
return inLibrary;
}
};
int main() {
Book books[3];
// 添加图书信息
books[0].loadBook("The Great Gatsby", "F. Scott Fitzgerald", 1925, true);
books[1].loadBook("To Kill a Mockingbird", "Harper Lee", 1960, false);
books[2].loadBook("1984", "George Orwell", 1949, true);
// 显示图书信息
for (int i = 0; i < 3; i++) {
cout << "Book " << i + 1 << endl;
books[i].displayBook();
cout << endl;
}
// 检查图书馆中是否有特定书籍
if (books[0].checkLibrary())
cout << "The Great Gatsby is in the library." << endl;
else
cout << "The Great Gatsby has been taken out." << endl;
return 0;
}
```
输出结果如下:
```
Book 1
Title: The Great Gatsby
Author: F. Scott Fitzgerald
Year of Publication: 1925
Status: In Library
Book 2
Title: To Kill a Mockingbird
Author: Harper Lee
Year of Publication: 1960
Status: Taken Out
Book 3
Title: 1984
Author: George Orwell
Year of Publication: 1949
Status: In Library
The Great Gatsby is in the library.
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)