此题和接下来的几道题要求你设计并实现一个Book类,你可以设想这是图书馆软件系统的一部分。Book类应包含表示ISBN号、书名、作者和版权日期以及表示是否已经借出的成员。创建能返回这些成员的值的函数,以及借书和还书的函数。对于输人Book对象的数据进行简单的有效性检查,例如,只接受n-n-n-x形式的ISBN号,其中n是一个整数,x是一个数字或一个字母。 为Book类添加运算符。添加==运算符,用于检查两本书的 ISBN 号是否相等。定义!=运算符,比较ISBN号是否不等。定义<<,分行输出书名、作者和ISBN号。 为Book类创建一个名为Genre的枚举类型,用以区分书籍的类型:小说(fiction)、非小说类文学作品(nonfiction)、期刊(periodical)、传记(biography)、儿童读物(children)。为每本书赋予一个Genre值,适当修改Book的构造函数和成员函数。要在测试文件给出图书的录入信息
时间: 2024-02-13 12:00:37 浏览: 11
好的,这道题和之前的一样,要求设计并实现一个Book类。类中需要包含表示ISBN号、书名、作者和版权日期以及表示是否已经借出的成员,并需要创建能返回这些成员的值的函数,以及借书和还书的函数。对于输入Book对象的数据需要进行简单的有效性检查,例如,只接受n-n-n-x形式的ISBN号,其中n是一个整数,x是一个数字或一个字母。另外,还需要添加运算符,如==运算符用于检查两本书的ISBN号是否相等,!=运算符用于比较ISBN号是否不等,以及<<运算符,用于以分行输出书名、作者和ISBN号的方式输出书籍信息。此外,还需要为Book类创建一个名为Genre的枚举类型,用以区分书籍的类型。最后,需要适当修改Book的构造函数和成员函数,为每本书赋予一个Genre值。我们需要在测试文件中给出图书的录入信息。好的,我开始回答你的问题。
下面是Book类的代码:
book.h:
```c++
#ifndef BOOK_H
#define BOOK_H
#include <string>
using namespace std;
enum Genre { fiction, nonfiction, periodical, biography, children };
class Book {
public:
Book(); // 默认构造函数
Book(const string& isbn, const string& title, const string& author, const string& date, Genre genre, bool is_out); // 构造函数
string get_isbn() const; // 返回ISBN号
string get_title() const; // 返回书名
string get_author() const; // 返回作者
string get_date() const; // 返回版权日期
Genre get_genre() const; // 返回书籍类型
bool is_out() const; // 返回是否已经借出
void check_in(); // 还书
void check_out(); // 借书
private:
string isbn; // ISBN号
string title; // 书名
string author; // 作者
string date; // 版权日期
Genre genre; // 书籍类型
bool out; // 是否已经借出
};
bool operator==(const Book& a, const Book& b); // ISBN号相等运算符
bool operator!=(const Book& a, const Book& b); // ISBN号不等运算符
ostream& operator<<(ostream& os, const Book& b); // 输出运算符
#endif
```
book.cpp:
```c++
#include "book.h"
#include <stdexcept>
Book::Book() // 默认构造函数
: isbn(""), title(""), author(""), date(""), genre(fiction), out(false) { }
Book::Book(const string& isbn, const string& title, const string& author, const string& date, Genre genre, bool is_out) // 构造函数
: isbn(isbn), title(title), author(author), date(date), genre(genre), out(is_out) { }
string Book::get_isbn() const { // 返回ISBN号
return isbn;
}
string Book::get_title() const { // 返回书名
return title;
}
string Book::get_author() const { // 返回作者
return author;
}
string Book::get_date() const { // 返回版权日期
return date;
}
Genre Book::get_genre() const { // 返回书籍类型
return genre;
}
bool Book::is_out() const { // 返回是否已经借出
return out;
}
void Book::check_in() { // 还书
if (!out) throw runtime_error("The book is not checked out.");
out = false;
}
void Book::check_out() { // 借书
if (out) throw runtime_error("The book is already checked out.");
out = true;
}
bool operator==(const Book& a, const Book& b) { // ISBN号相等运算符
return a.get_isbn() == b.get_isbn();
}
bool operator!=(const Book& a, const Book& b) { // ISBN号不等运算符
return !(a == b);
}
ostream& operator<<(ostream& os, const Book& b) { // 输出运算符
os << b.get_title() << endl;
os << b.get_author() << endl;
os << b.get_isbn() << endl;
return os;
}
```
下面是测试文件的代码:
```c++
#include "book.h"
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<Book> books;
// 添加几本书
books.push_back(Book("1-1-1-a", "C++ Primer", "Lippman", "2005", nonfiction, false));
books.push_back(Book("2-2-2-b", "Effective C++", "Meyers", "2005", nonfiction, true));
books.push_back(Book("3-3-3-c", "The Lord of the Rings", "Tolkien", "1954", fiction, false));
books.push_back(Book("4-4-4-d", "The Catcher in the Rye", "Salinger", "1951", fiction, true));
// 输出所有书籍信息
for (int i = 0; i < books.size(); ++i) {
cout << books[i] << endl;
}
// 借出一本书
cout << "Check out a book:" << endl;
books[0].check_out();
cout << books[0].is_out() << endl;
// 还回一本书
cout << "Check in a book:" << endl;
books[1].check_in();
cout << books[1].is_out() << endl;
// 比较两本书的ISBN号
cout << "Compare two books:" << endl;
cout << (books[0] == books[2]) << endl;
cout << (books[1] == books[3]) << endl;
return 0;
}
```
在测试文件中,我们首先创建了一个空的vector,然后向其中添加了几本书籍信息。接着,我们使用输出运算符输出了所有书籍的信息,并尝试借出和还回一本书。最后,我们比较了两本书的ISBN号。