Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The Book class is derived from the Publication class. A book is a publication with an Author name. The book class only has one attribute

The Book class is derived from the Publication class. A book is a publication with an "Author name".

The book class only has one attribute that is a pointer to a character to hold an author's name Dynamically.

Construction

A book is created empty by default, in a safe empty state.

The rule of three

Implement what is needed to comply with the rule of three so a book can safely be copied or assigned to another book.

Methods

The book class overrides the following virtual methods and type conversion operator.

  • type
  • write
  • read
  • set
  • operator bool()

Method implementations:

type method

Returns the character "B".

write method

  • First, it will invoke the write of its Base class.
  • If the incoming argument is a console IO object.
    • writes a single space
    • writes the author's name in SDDS_AUTHOR_WIDTH spaces. If the author's name is longer than the SDDS_AUTHOR_WIDTH value, it will cut it short and writes exactly SDDS_AUTHOR_WIDTH characters. Note that this should not modify the author's name.
    • writes " |"
  • If the incoming argument is not a console IO object
    • writes a tab character '\t'
    • writes the author's name
  • returns the incoming ostream.

Read

Read the author name in local variables before setting the attribute to any value. (to make it easier lets assume the author's name can not be more than 256 characters)

  • First, invoke the read of the Base class.
  • Free the memory held for the author's name
  • If the incoming argument is a console IO object
    • ignore one character (skip the ' ')
    • prompt "Author: "
    • read the author's name
  • If the incoming argument is not a console IO object
    • ignore the tab character
    • read the author's name

Then if the incoming istream object is not in a fail state, dynamically hold the author's name in the char pointer attribute of the book class.

At the end return the incoming istream object.

set

  • invoke the set of the base class to set the member id
  • reset the date to the current date.

operator bool()

return true if the author's name exists and is not empty and the base class's operator bool() has returned true.

the Tester program

// Final Project Milestone 4 // Book // File ms4_tester.cpp // Version 1.0 // Author Fardad Soleimanloo // Revision History // ----------------------------------------------------------- // Name Date Reason // This will not change the output ///////////////////////////////////////////////////////////////// #include  #include  #include "Book.h" #include "Utils.h" #include "Date.h" using namespace std; using namespace sdds; Book readBook(istream& istr) { Book P; istr >> P; return P; } Book getNextRec(ifstream& ifstr) { Book P; ifstr >> P; ifstr.ignore(1000, ' '); return P; } int main() { sdds::sdds_test = true; Book pd; cout << "An Invalid Book printout:" << endl; cout << ">" << pd << "<" << endl; cout << endl << "Enter the following: " << endl << "P1234" << endl << "------------------------------" << endl; pd = readBook(cin); if (!cin) { cin.clear(); cin.ignore(1000, ' '); } else { cout << "This is not supposed to be printed!" << endl; } cout << "You entered:" << endl; cout << ">" << pd << "<" << endl; cout << endl << "Enter the following: " << endl << "P123" << endl << "Seneca Handbook" << endl << "2021/13/17" << endl << "------------------------------" << endl; pd = readBook(cin); if (!cin) { cin.clear(); cin.ignore(1000, ' '); } else { cout << "This is not supposed to be printed!" << endl; } cout << "You entered:" << endl; cout << ">" << pd << "<" << endl; cout << endl << "Enter the following: " << endl << "P123" << endl << "The Story of My Experiments with Truth" << endl << "2021/11/17" << endl << "Mohandas Karamchand Gandhi" << endl << "------------------------------" << endl; pd = readBook(cin); cout << "You entered:" << endl; cout << pd << endl; cout << "And the title is agian: \"" << (const char*)pd << "\"" << endl; pd.set(12345); if (pd.onLoan()) { cout << "Now this publication is on loan to a member with the id: 12345" << endl; cout << "The checkout date is: " << pd.checkoutDate() << endl; pd.setRef(9999); cout << "The library unique reference id is: " << pd.getRef() << endl; cout << pd << endl; cout << "----------------------------------------------------------------" << endl; } cout << "Adding the Book to the end of the data file:" << endl; ofstream fileout("Books.txt", ios::app); if (pd) { cout << "appeneded to the file" << endl; fileout << pd << endl; } fileout.close(); cout << endl << "Contents of the file:" << endl; ifstream filein("Books.txt"); char pType{}; for (int row = 1; filein; row++) { filein >> pType; if (pType != 'B') { cout << "The Record type signature is supposed to be B, but it is: " << pType << endl; filein.setstate(ios::failbit); } filein.ignore(); pd = getNextRec(filein); if (filein) { cout << (pd.onLoan() ? "|*" : "| "); cout.width(4); cout.fill(' '); cout.setf(ios::right); cout << row << (pd.onLoan()? "*": " "); cout.unsetf(ios::right); cout << pd << (pd == "Star" ? "<<<":"") << endl; } } return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

Students also viewed these Programming questions

Question

What is the principle of thermodynamics? Explain with examples

Answered: 1 week ago

Question

Explain the strength of acid and alkali solutions with examples

Answered: 1 week ago