Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Programing Book Pile Imagine a pile of books on your desk. Each book is so large and heavy that you can remove only the

C++ Programing

Book Pile Imagine a pile of books on your desk. Each book is so large and heavy that you can remove only the top one from the pile. You cannot remove a book from under another one. Likewise, you can add another book to the pile only by placing it on the top of the pile. You cannot add a book beneath another one.

For this assignment, you will design a class called BookPile that represents the pile of books. The class will simply keep track of the titles of the books on the pile. The pile may contain no more than 10 books at any one time.

Your class should support the following functions as part of its public interface:

An accessor function called get_count. It takes no parameters and returns an int. It returns the number of book currently on the pile

. An accessor function called get_top_title. It takes no parameters and returns a string. It returns the title of the book on the top of the pile. If the pile is empty, it should return a null (empty) string

. A mutator function called add_book. It takes a string as a parameter that represents the title of a book to be added to the pile. It should return a bool to indicate whether or not the book was successfully placed on the pile. Adding a book is unsuccessful when attempting to add a book when the pile is full (i.e. the pile contains 10 books).

A mutator function called remove_book. This is a void function that takes no parameters. It removes the top book from the pile.If the pile is empty, the function should have no effect on the pile.

Build your class as a header and implementation file pair. A test program is provided and attached to this assignment item for your convenience. Your implementation should work without modification to the test program.

When submitting this assignment, include your header and implementation file pair as well as your test program. Submit your source files as TGZ archive prepared using your instructors web site.

----------------------------------------------------------------------------------------------------------------------------------------------------------

// bookpile-test - Test Program for Book Pile class, CISC-125 // // This program is provided for testing class "BookPile". It provides a simple // menu-driven user interface for class operations of retrieving the number // of books on a pile, the title of the top book on the pile, adding a book's // title to a pile, and revoving the top book from the pile. #include #include #include #include "BookPile.h" using namespace std; void add_book (BookPile& p); void remove_book (BookPile& p); void show_count (const BookPile& p); void show_top_title (const BookPile& p); int main () { BookPile p; // An object to represent a book pile int menu_option; // Menu option, input by the user cout << "BookPile Test Program" << endl << endl; do { // Display menu of options cout << "1 - Show Number of Books on Pile" << endl; cout << "2 - Show Title of Top Book on Pile" << endl; cout << "3 - Add a Book to the Pile" << endl; cout << "4 - Remove a Book from the Pile" << endl; cout << "9 - Exit the Program" << endl << endl; cout << "Option: "; // Get user's menu option. Assumes that the user will enter an integer. cin >> menu_option; cout << endl; // Based on user's input, call a function, passing the BookPile object // as a parameter, to carry out the user's selected option; switch (menu_option) { case 1: show_count(p); break; // Show book pile count case 2: show_top_title(p); break; // Show title of top book on pile case 3: add_book(p); break; // Add a book to top of pile case 4: remove_book(p); break; // Remove book from top of pile } } while (menu_option != 9); return 0; } // Add a book to top of pile. Prompts for a book title, then calls BookPile // 'add_book' member function to add the title. Function 'add_book' returns // a bool to indicate whether or not the addition of the title was successful. // Adding a book to the pile is unsuccessful when the pile will not accomodate // another book. void add_book (BookPile& p) { // Get title to be added string title; cout << "Enter title: "; cin.ignore(INT_MAX, ' '); getline(cin, title); // Add (or attempt to add) the title, and report success or fail. if (p.add_book(title)) cout << "Book added." << endl << endl; else cout << "Book not added. Pile is full." << endl << endl; } // Remove the top book from the pile. This function checks to see if there // is at least one book before calling the 'remove_book' member function. void remove_book (BookPile& p) { if (p.get_count()) { p.remove_book(); cout << "Book removed." << endl << endl; } else cout << "No books on pile." << endl << endl; } // Report the number of books on the pile by calling the 'get_count' member // function. void show_count (const BookPile& p) { cout << "Books on pile: " << p.get_count() << endl << endl; } // Show the title of the book on top of the pile, by calling the // 'get_top_title' member function. This function first checks to see if // there is at least one book on the pile by calling 'get_count'. void show_top_title (const BookPile& p) { if (p.get_count()) cout << "Top book is " << p.get_top_title() << endl << endl; else cout << "No books on pile"; } 

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

Big Data Fundamentals Concepts, Drivers & Techniques

Authors: Thomas Erl, Wajid Khattak, Paul Buhler

1st Edition

0134291204, 9780134291208

More Books

Students also viewed these Databases questions

Question

Evaluate without using a calculator. cos^1(1)=

Answered: 1 week ago

Question

Evaluate 3x - x for x = -2 Answer:

Answered: 1 week ago

Question

What is group replacement? Explain with an example. (2-3 lines)

Answered: 1 week ago