Chapter 9  Inheritance

 

Write a program that keeps track of the books in your private library.  The base class will be called Book.  The attributes of a book (a Book object) will be title, author, and year published.  You will also need a Reference class to keep track of all your reference books.  In addition to a title, author, and year, each reference book will also have a very brief description of its subject and private data member for the number of pages.  Since Reference objects have several of the same attributes as Book objects, it is natural for Reference to inherit from Book.  (That is, Reference objects are just like Book objects except the former have two extra data members.) 

 

Use the new operator in your constructors to allocate just enough memory for the title, author, and subject strings.  In the line following each “new,” use the assert operator to check to make sure memory was indeed allocated.  Include a destructor for each class that deletes the string pointers to free up memory.

 

Each class should have a member function called “print” that prints all the info on a book.  Make the print functions const, since they shouldn’t be modifying any data anyway.  The print function body for Reference should contain a line such as:

 

Book::print();  //call print function from base class

 

This will print a reference book’s title, author, and year.  The function body should also contain a cout that prints the subject and pages.

 

Instantiate (create) several books in your main, both regular and reference books.  Call the print function for objects of each class.