http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h
Chapter 9 // Exercise 6
Add operators for Book class. Have the == operator check whether the ISBN numbers are the same for two books. Have the != also compare the ISBN numbers. Have << print out the title, author, and ISBN on separate lines.
I changed the get functions to const on this version to prevent errors when using const versions of Book in the operators. Also, instead of using an if statement to print out specific statements in relation to a book being printed out, it just prints out 0 or 1 now. Other than that, not much was changed in this version.
#include "stdafx.h" #include "std_lib_facilities.h" class Book { public: Book(); ~Book(); string getISBN() const { return m_isbn; } string getTitle() const { return m_title; } string getAuthor() const { return m_author; } int getDate() const { return m_date; } bool getStatus() const { return m_checkedOut; } void setISBN(string num1, string num2, string num3, string char1); void setTitle(string title) { m_title = title; } void setAuthor(string author) { m_author = author; } void setDate(string year); void checkBookOut() { m_checkedOut = true; }; void checkBookIn() { m_checkedOut = false; }; private: //m_ prefix to denote member variable string m_isbn, m_title, m_author; int m_date; bool m_checkedOut; }; //--OVERLOAD OPERATORS--// //compare ISBN numbers bool operator==(const Book& a, const Book& b) { return a.getISBN() == b.getISBN(); } bool operator!=(const Book& a, const Book& b) { return a.getISBN() != b.getISBN(); } //print out book ostream& operator<<(ostream& os, const Book& book) { return os << "Title: " << book.getTitle() << endl << "Author: " << book.getAuthor() << endl << "Copyright: " << book.getDate() << endl << "ISBN: " << book.getISBN() << endl << "Checked out:" << book.getStatus() << endl << endl; } //constructor Book::Book() { m_isbn = m_title = m_author = ""; m_date = 0; m_checkedOut = false; } //deconstructor Book::~Book() {} //set the ISBN void Book::setISBN(string num1, string num2, string num3, string char1) { size_t check; //check first number check = num1.find_first_not_of("0123456789"); while (check != string::npos) { cout << "Not a valid ISBN number, try again: "; cin >> num1; check = num1.find_first_not_of("0123456789"); } //check second number check = num2.find_first_not_of("0123456789"); while (check != string::npos) { cout << "Not a valid ISBN number, try again: "; cin >> num2; check = num2.find_first_not_of("0123456789"); } //check third number check = num3.find_first_not_of("0123456789"); while (check != string::npos) { cout << "Not a valid ISBN number, try again: "; cin >> num3; check = num3.find_first_not_of("0123456789"); } //check last character check = char1.find_first_not_of("0123456789ABCDEFGHIJKLMNOPQRSTUVWXY"); while (check != string::npos) { cout << "Not a valid ISBN digit or letter, try again: "; cin >> char1; check = char1.find_first_not_of("0123456789ABCDEFGHIJKLMNOPQRSTUVWXY"); } //set isbn m_isbn = ""; m_isbn.append(num1); m_isbn.append("-"); m_isbn.append(num2); m_isbn.append("-"); m_isbn.append(num3); m_isbn.append("-"); m_isbn.append(char1); } //set and check the year of copyright void Book::setDate(string year) { size_t check; check = year.find_first_not_of("0123456789"); while (check != string::npos || year.size() != 4) { cout << "Not a valid year try again: "; cin >> year; check = year.find_first_not_of("0123456789"); } m_date = stoi(year); } int main() { Book lotr; lotr.setTitle("Lord Of The Rings"); lotr.setAuthor("J.R.R Tolkien"); lotr.setDate("1954"); lotr.setISBN("1111", "1111", "1111", "J"); cout << lotr; lotr.setISBN("123", "0", "4444", "6"); lotr.checkBookOut(); cout << lotr; keep_window_open(); return 0; }
I changed the get functions to const on this version to prevent errors when using const versions of Book in the operators. Also, instead of using an if statement to print out specific statements in relation to a book being printed out, it just prints out 0 or 1 now. Other than that, not much was changed in this version.
No comments:
Post a Comment