book class 1.2 [genre enumerator]

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 9 Exercise 7
Using std_lib_facilities.h by Bjarne Stroustrup.
Using Chrono.h and Chrono.cpp by Bjarne Stroustrup,
including the Chrono.h header file and the Chrono.cpp source file. Chrono is an extensive date class used in this exercise.

My project includes the following files:
Chrono.h date class header file
Chrono.cpp date class source file
p340_7_book_main.cpp my main cpp file
p340_7_my_book_class.h my book class header file
p340_7_book_source.cpp my book class header file’s source file

p340_7_book_main.cpp my main cpp file

[code language=”cpp”]
// Philipp Siedler
// Bjarne Stroustrup’s PPP
// Chapter 9 Exercise 7

#include "p340_7_my_book_class.h"
using namespace Chrono;

int main()
try
{
Book myBook(ISBN(), "book1", "author1", Date(2000, Date::jul, 5), Genre(0), false);
cout << myBook;
keep_window_open();

keep_window_open();
}
catch (runtime_error e) {
cout << e.what() << endl;
keep_window_open();
}
catch (…) {
cout << "Exiting" << endl;
keep_window_open();
}
[/code]

p340_7_my_book_class.h header file

[code language=”cpp”]
#include "std_lib_facilities.h"
#include "Chrono.h"
using namespace Chrono;

//BOOK CLASS——
struct ISBN {
int n0, n1, n2;
char x;
ISBN(int n0, int n1, int n2, char x);
ISBN();
};

// exercise 7 genre enumerator
enum Genre {
fiction = 0,
nonfiction,
periodical,
biography,
children
};

class Book {
private:
ISBN isbn;
string title;
string author;
Date copyright_date;
Genre genre;
bool checked_out;

public:
Book(ISBN _isbn, string _title, string _author, Date _copyright_date, Genre _genrebook, bool _checked_out);
Book();
ISBN isbnf() { return isbn; }
string titlef() { return title; }
string authorf() { return author; }
Date copyright_datef() { return copyright_date; }
Genre genref() { return genre; }
bool checked_outf() { return checked_out; }
void book_out();
void book_in();
};

//exercise 6 operator overloading
ostream& operator << (ostream&, ISBN&);
ostream& operator << (ostream&, const Genre&);
ostream& operator << (ostream&, Book&);
bool operator ==(ISBN&, ISBN&);
bool operator !=(ISBN&, ISBN&);
//endBOOK CLASS——
[/code]

p340_7_book_source.cpp my book class header file’s source file

[code language=”cpp”]
#include "p340_7_my_book_class.h"

//BOOK CLASS_HELPER FUNCTIONS——

ISBN::ISBN(int _n0, int _n1, int _n2, char _x) {
n0 = _n0;
n1 = _n1;
n2 = _n2;
x = _x;
}

ISBN::ISBN() {
n0 = 0;
n1 = 0;
n2 = 0;
x = ‘0’;
}

Book::Book(ISBN _isbn, string _title, string _author, Date _copyright_date, Genre _genrebook, bool _checked_out) {
cout << "— Creating book —\n";
isbn = _isbn;
title = _title;
author = _author;
copyright_date = _copyright_date;
genre = _genrebook;
checked_out = _checked_out;
};

Book::Book() {
cout << "— Creating book —\n";
isbn = ISBN(0,0,0,0);
title = "";
author = "";
copyright_date = Date();
genre = Genre();
checked_out = false;
}

void Book::book_out() {
checked_out = true;
};

void Book::book_in() {
checked_out = false;
};

ostream& operator << (ostream& os, ISBN& _isbn) {
os << _isbn.n0 << _isbn.n1 << _isbn.n2 << _isbn.x;
return os;
}

ostream& operator << (ostream& os, const Genre& _genre) {
switch (_genre){
case Genre::biography:
os << "biography";
return os;
case Genre::children:
os << "children";
return os;
case Genre::fiction:
os << "fiction";
return os;
case Genre::nonfiction:
os << "nonfiction";
return os;
case Genre::periodical:
os << "periodical";
return os;
}
}

ostream& operator << (ostream& os, Book& _book) {
os << "ISBN: " << _book.isbnf() << "\n"
<< "Title: " << _book.titlef() << "\n"
<< "Author: " << _book.authorf() << "\n"
<< "Copyright Date: " << _book.copyright_datef() << "\n"
<< "Genre: " << _book.genref() << "\n"
<< "Checked out: " << boolalpha << _book.checked_outf() << "\n";
return os;
}

bool operator ==(ISBN& book1, ISBN& book2) {
if (book1 == book2) {
return true;
}
else {
return false;
}
}

bool operator !=(ISBN& book1, ISBN& book2) {
if (book1 != book2) {
return true;
}
else {
return false;
}
}

//endBOOK CLASS_HELPER FUNCTIONS——
[/code]

Output:
--- Creating book ---
ISBN: 0000
Title: book1
Author: author1
Copyright Date: (2000,7,5)
Genre: fiction
Checked out: false
Please enter a character to exit

Leave a Reply