Answered step by step
Verified Expert Solution
Question
1 Approved Answer
#pragma once /* * Topic: Lab 4 - Dynamic Memory * File name: Book.h * This file declares a class named Book */ // Which
#pragma once /* * Topic: Lab 4 - Dynamic Memory * File name: Book.h * This file declares a class named Book */ // Which header files should be included? #includeusing namespace std; class Book { private: static int booksCreated; string title; int pages; int bookNumber; public: Book(); Book(Book&); ~Book(); void setTitle(string); void setPages(int); int getBooksCreated() const; string getTitle() const; int getPages() const; int getBookNumber() const; void printBook() const; };
/* * Topic: Lab 4 - Dynamic Memory * File name: Book.cpp * This file defines a class named Book */ // Which header files should be included? #include "Book.h" // The class variable is initialized to 0 int Book::booksCreated = 0; // The default constructor initializes the title to the // empty string and pages to 0. bookNumber is initialized // to one more than the current value of booksCreated, // and booksCreated is incremented. Book::Book() { title = ""; pages = 0; bookNumber = ++booksCreated; } // The copy constructor initializes bookNumber to one more // than the current value of booksCreated, and booksCreated // is incremented. It initializes the title and pages to the // same value as that of the title and pages of the parameter. Book::Book(Book& other) { bookNumber = ++booksCreated; title = other.getTitle(); pages = other.getPages(); } // The destructor prints the phrase "Book number [number] has // been removed from the collection" Book::~Book() { printf("Book number %i has been removed from the collection ", bookNumber); } // define the setters and getters void Book::setTitle(string aTitle) { title = aTitle; } void Book::setPages(int aPages) { pages = aPages; } int Book::getBooksCreated() const { return booksCreated; } string Book::getTitle() const { return title; } int Book::getPages() const { return pages; } int Book::getBookNumber() const { return bookNumber; } // The printBook method uses printf to print the book's state // in a table ready format. It prints the book number aligned // to the left in a 3-space column, the number of pages also // aligned to the left in an 8-space column, and finally the // book's title (no column size). void Book::printBook() const { printf("%-3i%-8i%s ",getBookNumber(),getPages(),getTitle().c_str()); }
#pragma once /* * Topic: Lab 5 - Composition * File name: Library.h * Review the code, it might have errors! */ // write the required include statements class Library { private: int books; // the quantity of books in the library Book** library; // the current book collection public: Library(); // the default constructor ~Library(); void addBook(string, int); void removeBook(int); int findBook(string) const; int howManyBooks() const; void printBookList() const; } ------------------------------------------------------------------------ /* * Topic: Lab 5 - Composition * File name: Library.cpp * This file defines a class named Library * Complete the code as required */ // write the required include statement // The default constructor initializes books to 0 and library to the null pointer // The destructor releases the memory allocated to the book array by calling the // destructor for each Book object in the array. // The addBook method receives a book's title and number of pages as parameters // It then adds an element to the array, copies the values from the old array, // and creates a new book to be added at the end of the array. This method // increments the value of books. // The removeBook method receives as parameter the index number where the book // to be removed is located and proceeds to remove the book from the library // creating a smaller array. This method decrements the value of books. // The findBook method receives the book's title as parameter and returns the // index number where the book is fund in the array. If the book is not found // the method returns -1. //howManyBooks returns the quantity of books in the library // printBookList uses printf to print the header PAGES TITLE // The PAGES column has a width of 7 spaces and content is // aligned to the left. The method implements an iteration control // structure to call the printBook method for all books in the library. // Make sure that the printBook method prints the numbers of pages first, // then the title, using the same column size for numbr of pages. ------------------------------------------------------------------------------------ /* * Topic: Lab 5 - Composition * File name: lab05.cpp * This file implements a class named Library */ // Which header files should be included? int menu(); int main() { // declare a Library object named myBooks // declare an integer varaible named option and initialize to 0 // implement a do/while iteration control structure which uses the // value of option as sentinel. First, assign the return value of // menu to option. Then implement a switch structure that uses // option as the switching value. The cases must implement the // actions described in the menu. Don't forget to include the // default case! If the value of option is 4, the program prints // the phrase "Closing the library application..." before exit. // To test you application, addd at least three books to the collection // and remove at least one. // print a statement with your personal information using the phrase // "--- Program developed by [YOUR NAME], ID# [YOUR ID NUMBER] ---" // where the square brackets and the text within is substituted with your personal // information. Make sure to add a blank line before and after the phrase is printed. system(" pause"); // For Visual Studio only! return 0; } // The menu method prints the list of actions and returns the selected one int menu() { int choice = 0; do{ cout << "Select one of the following: "; cout << "\t1. Add a book to the library "; cout << "\t2. Remove a book from the library "; cout << "\t3. Print the book list "; cout << "\t4. Exit the program "; cout << " \tYour selection: "; cin >> choice; cout << endl; }while(choice < 1 || choice > 4); return choice; }
Finish code please.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started