Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help adding the following to my C++ code Add the following functions to the Book class code: A constructor method with no parameters.

I need help adding the following to my C++ code

Add the following functions to the Book class code:

  • A constructor method with no parameters. It should set title to "TBA", author to "Anonymous", price to 0, and publication year to 2019
  • A second constructor method with parameters that sets the book member variables with initial values
  • A class method with four parameters that will set the member variables. It behaves exactly like the constructor above
  • A destructor method that simply outputs "Book is destroyed"
  • A class method that will update the price of a book. It will have one parameter, a discount rate, that simply is multiplied by the price. If the rate is 0.9 for example, the new book price would be 90% of what it was before. A rate of 1.25 would raise the book price by 25%.
  • A function outside of the class definition that will read data from a text file into an array of Book objects by calling the set book method described above. It should also update a count of the number of books that were actually read in from the file. The file should be named "books.txt" and should have 3 lines (1st line title, 2nd line author, 3rd line price and year separated by a space) for each book. Assume the data file will contain a maximum of 10 books.
  • A function outside of the class definition that will print all of the books. It should loop through all of the books by making a call to the object's print method.
  • Modify the main driver function so that it calls the functions to read all of the book objects and then print them all to the screen. Make a call to the update price method and include necessary print statements to indicate that it worked.

The Code to add to:

#include #include #include #include using namespace std; class bookInformation { private: double price; string title, author; int publication; public: void setPrice(double); void setTitle(string); void setAuthor(string); void setPublication(int); double getPrice() const; string getTitle() const; string getAuthor () const; int getPublication() const; void print() const; }; int main() { bookInformation book, book2; cout << "Book #1" << endl; book.setPrice(15.99); book.setTitle("1984"); book.setAuthor("George Orwell"); book.setPublication(1949); book.print(); cout << endl; cout << "Book #2" << endl; book2.setPrice(10.56); book2.setTitle("Hamlet"); book2.setAuthor("William Shakesphere"); book2.setPublication(1603); book2.print(); return 0; }

void bookInformation::setPrice(double pri) { price= pri; }

void bookInformation::setTitle(string titl) { title=titl; }

void bookInformation::setAuthor(string aut) { author= aut; } void bookInformation::setPublication(int pub) { publication= pub; } double bookInformation::getPrice() const { return price; }

string bookInformation::getTitle() const { return title; }

string bookInformation::getAuthor() const { return author; }

int bookInformation::getPublication() const { return publication; }

void bookInformation:: print() const { cout << "Book Title: " << title << endl; cout << "Author: " << author << endl; cout << "Price: $" << price << endl; cout << "Publication Year: " << publication << endl; }

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

Database Marketing The Ultimate Marketing Tool

Authors: Edward L. Nash

1st Edition

0070460639, 978-0070460638

More Books

Students also viewed these Databases questions