此题和接下来的几道题要求你设计并实现一个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 13:00:27 浏览: 33
好的,以下是一个多文件程序的示例代码,其中`Book.h`文件中定义了`Book`类和`Genre`枚举类型,`Book.cpp`文件中实现了`Book`类的成员函数,`main.cpp`文件中演示了如何使用`Book`类。
`Book.h`文件:
```cpp
#ifndef BOOK_H
#define BOOK_H
#include <string>
#include <regex>
#include <iostream>
using namespace std;
enum class Genre {
fiction,
nonfiction,
periodical,
biography,
children,
};
class Book {
private:
string isbn;
string title;
string author;
string copyright;
bool borrowed;
Genre genre;
public:
Book(string isbn, string title, string author, string copyright, Genre genre);
string getIsbn() const;
string getTitle() const;
string getAuthor() const;
string getCopyright() const;
bool isBorrowed() const;
Genre getGenre() const;
void borrow();
void returnBook();
bool operator==(const Book& other) const;
bool operator!=(const Book& other) const;
friend ostream& operator<<(ostream& os, const Book& book);
};
#endif
```
`Book.cpp`文件:
```cpp
#include "Book.h"
Book::Book(string isbn, string title, string author, string copyright, Genre genre) {
// ISBN号的有效性检查
regex pattern("\\d+-\\d+-\\d+-[\\dX]");
if (!regex_match(isbn, pattern)) {
throw invalid_argument("Invalid ISBN format!");
}
this->isbn = isbn;
this->title = title;
this->author = author;
this->copyright = copyright;
this->borrowed = false;
this->genre = genre;
}
string Book::getIsbn() const {
return isbn;
}
string Book::getTitle() const {
return title;
}
string Book::getAuthor() const {
return author;
}
string Book::getCopyright() const {
return copyright;
}
bool Book::isBorrowed() const {
return borrowed;
}
Genre Book::getGenre() const {
return genre;
}
void Book::borrow() {
if (borrowed) {
throw runtime_error("Book is already borrowed!");
}
borrowed = true;
}
void Book::returnBook() {
if (!borrowed) {
throw runtime_error("Book is not borrowed!");
}
borrowed = false;
}
bool Book::operator==(const Book& other) const {
return (this->isbn == other.isbn);
}
bool Book::operator!=(const Book& other) const {
return (this->isbn != other.isbn);
}
ostream& operator<<(ostream& os, const Book& book) {
os << book.title << " by " << book.author << ", ISBN: " << book.isbn << endl;
return os;
}
```
`main.cpp`文件:
```cpp
#include "Book.h"
#include <vector>
int main() {
vector<Book> books;
books.push_back(Book("1-1-1-1", "The Great Gatsby", "F. Scott Fitzgerald", "1925", Genre::fiction));
books.push_back(Book("2-2-2-2", "To Kill a Mockingbird", "Harper Lee", "1960", Genre::fiction));
books.push_back(Book("3-3-3-3", "The Catcher in the Rye", "J.D. Salinger", "1951", Genre::fiction));
books.push_back(Book("4-4-4-4", "1984", "George Orwell", "1949", Genre::fiction));
books.push_back(Book("5-5-5-5", "Pride and Prejudice", "Jane Austen", "1813", Genre::fiction));
for (const auto& book : books) {
cout << book;
}
return 0;
}
```
在这个示例代码中,我们定义了`Book`类和`Genre`枚举类型,并在`Book.h`文件中进行了声明。在`Book.cpp`文件中,我们实现了`Book`类的成员函数和运算符重载。在`main.cpp`文件中,我们演示了如何创建`Book`对象并输出其信息。
你可以根据需要修改和扩展这个示例代码,以满足你的需求。