Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The aim of this assignment is to make sure that you understand and are familiar with the concepts covered in the lectures, including C++ inheritance,

The aim of this assignment is to make sure that you understand and are familiar with the concepts covered in the lectures, including C++ inheritance, polymorphism, virtual function, containment, file operations, and exception handling

Q1: Create Comics and Drama - Subclasses of Book Class (Inheritance) (10 points)

File: Comics.h (5 points)

// Q1a: Create Comics Class

// Part 1: Create a child class of the Book class named 'Comics'

// See the add function in MainClass.cpp for proper use of this function.

// Part2: Declare constructor which accepts the same 3 parameters as the parent class Book.

// Pass the 3 parameters to the super constructor in the Book class.

// Part 3: Re-declare the method display (virtual method found inside of parent class Book)

File: Drama.h (5 points)

// Q1b: Create Drama Class

// Part 1: Create a child class of the Book class named 'Drama'

// See the add function in MainClass.cpp for proper use of this function.

// Part2: Declare constructor which accepts the same 3 parameters as the parent class Book.

// Pass the 3 parameters to the super constructor in the Book class.

// Part 3: Re-declare the method display (virtual method found inside of parent class Book)

Q2: Define Comics and Drama Display Functions (Polymorphism) (10 points)

File: Comics.cpp (5 points)

// Q2a: Define Display for Comics class

// Define the method display that you declared within the Comics class in the header file

// Information should be printed in the following format:

// Name:

// Price:

// Type: Comics

// (See the print_all function in MainClass.cpp for proper use of this function.)

File: Drama.cpp (5 points)

// Q2b: Define Display for Drama class

// Define the method display that you declared within the Drama class in the header file

// Information should be printed in the following format:

// Name:

// Price:

// Type: Drama

// (See the print_all function in MainClass.cpp for proper use of this function.)

Q3: Change Price Friend Function of Book Class (10 points)

File: Book.h (5 points)

// Q3a: Declare Friend Function Change Price

// Declare a friend function named changePrice which has 2 parameters and no return value.

// The first parameter is a pointer to a Book, and the second is a string.

// See the helper function in MainClass.cpp for proper use of this function.

File: MainClass.cpp (5 points)

// Q3b: Define Friend Function Change Price

// Define the function changePrice that is declared within the Book.h file.

// This function sets the price value of the Book pointer to the value of the string parameter.

File: MainClass.cpp (In the helper function line 145) (2 points)

// Q3c: Call Change Price Function

Note: A friend function allows you to access the private variables of a class (ex: pet->price)

Q4: Add Book Function (Putting the Inherited Classes to Use) (10 points)

File: MainClass.cpp (10 points)

// Q4: Add Book

// This function will be used to add a new pet to the tail of the global linked list.

// You will need to use the enum type variable to determine which constructor to use.

// Remember that search is called before this function, therefore, the new pet is not on the list.

Q5: Remove book (20 points)

// Q5a: Remove the node that matches name, price, and type

// Q5b: Remove all nodes in the list. Make sure no memory leak will occur

Q6: Save and Load Functions (Using ifstream & ofstream) (20 points)

File: MainClass.cpp (10 points)

// Q6a: Save

// Save the linked list of pets to a file using ofstream.

// You will need to come up with a way to store the amount of Containers in linked list.

// Hint: You may want to cast the enum 'type' to an int before writing it to the file.

File: MainClass.cpp (10 points)

// Q6b: Load

// Load the linked list of pets from a file using ifstream.

// You will need to create the linked list in the same order that is was saved to a file.

// You will need to create a new node for the linked list, then add it to the tail of the list.

// Hint: If you casted the enum 'type' to an int, you will need to cast it back to a 'Type'.

// You will use the 'type' variable read from the file to determine which constructor to use.

Q7: Sort the list of books in the container (20 points)

File: MainClass.cpp (20 points)

// Q7 Use a recursive function, such as quick sort or merge sort, to sort the

// container list by the book title in ascending order.

// You can create a new list. In this case, you must delete (garbage collect) the old list

Files:

MainClass.cpp:

// READ BEFORE YOU START: // You are given a partially completed program that creates a list of books. // Each book has the corresponding information: name, price, and type. // In the Book.h file, you will find the definition for this enum 'type'. // Books on the list can be 2 different 'types' : either a drama or a comics. // The classes Drama and Comics are subclasses of the Book class (found in Book.h). // Both of these classes will have their own use of the virtual display method. // // To begin, you should trace through the given code and understand how it works. // Please read the instructions above each required function and follow the directions carefully. // If you modify any of the given code, the return types, or the parameters, you risk failing the automated test cases. // // You are to assume that all input is valid: // Valid name: String containing alphabetical letters beginning with a capital letter // Valid price: String containing alphabetical letters beginning with a capital letter // All input will be a valid length and no more than the allowed amount of memory will be used

#include "Container.h" #include "Book.h" #include "Drama.h" #include "Comics.h"

#include #include #include

using namespace std;

// forward declarations void flush(); void branching(char); void helper(char); void add_book(string, double, Type); Book* search_book(string, double, Type); void remove_book(string, double, Type); void remove_all(); void print_all(); void listSort(Container **);

void save(string); // 10 points void load(string); // 10 points

Container* list = NULL; // global list

int main() { _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); // Use to check for memory leaks in VS

load("Books.txt");

char ch = 'i';

do { cout << "Please enter your selection" << endl; cout << "\ta: add a new book to the list" << endl; cout << "\tc: change the price of a book" << endl; cout << "\tr: remove a book from the list" << endl; cout << "\ts: sort the list of books" << endl; cout << "\tp: print all books on the list" << endl; cout << "\tq: quit and save list of books" << endl; cin >> ch; flush(); branching(ch); } while (ch != 'q');

save("Books.txt"); remove_all(); list = NULL; return 0; }

void flush() { int c; do c = getchar(); while (c != ' ' && c != EOF); }

void branching(char c) { switch (c) { case 'a': case 'c': case 'r': case 's': case 'p': helper(c); break; case 'q': break; default: printf(" Invalid input! "); } }

// The helper function is used to determine how much data is needed and which function to send that data to. // It uses pointers and values that are returned from some functions to produce the correct ouput. // There is no implementation needed here, but you should study this function and know how it works. // It is always helpful to understand how the code works before implementing new features. // Do not change anything in this function or you risk failing the automated test cases. void helper(char c) { string name; double price; Type type; int type_check = -1;

if (c == 'p') print_all(); else if (c == 's') { listSort(&list); cout << endl << "List sorted." << endl; } else { cout << endl << "Please enter the book's name: " << endl; cin >> name; cout << "Please enter the book's price: " << endl; cin >> price;

while (!(type_check == 0 || type_check == 1)) { cout << endl << "Please select one of the following: " << endl; cout << "0. Drama " << endl; cout << "1. Comics" << endl; cin >> type_check; }

type = (Type)type_check;

Book* book_result = search_book(name, price, type);

if (c == 'a') // add book { if (book_result == NULL) { add_book(name, price, type); cout << endl << "Book added." << endl << endl; } else cout << endl << "Book already on list." << endl << endl; } else if (c == 'c') // change book price { if (book_result == NULL) { cout << endl << "Book not found." << endl << endl; return; }

cout << endl << "Please enter the new price for this book: " << endl; cin >> price; flush();

// Q3c: Call Change Price Function

cout << endl << "Book's price changed." << endl << endl; } else if (c == 'r') // remove book { if (book_result == NULL) { cout << endl << "Book not found." << endl << endl; return; }

remove_book(name, price, type); cout << endl << "Book removed from the list." << endl << endl; } } }

// Q3b: Define Friend Function Change Price // Define the function changePrice that is declared within the Book.h file. // This function sets the price value of the Book pointer to the value of the double parameter.

// Q4: Add Book // This function will be used to add a new book to the beginning of the global linked list. // You will need to use the enum type variable to determine which constructor to use. // Remember that search is called before this function, therefore, the new book is not on the list. void add_book(string name, double price, Type type) {

}

// No implementation needed here, however it may be helpful to review this function Book* search_book(string name, double price, Type type) { Container* container_traverser = list;

while (container_traverser != NULL) { if (container_traverser->book->getName() == name && container_traverser->book->getType() == type && container_traverser->book->getPrice() == price) return container_traverser->book;

container_traverser = container_traverser->next; }

return NULL; }

// Q5a: Remove the node that matches name, price, and type void remove_book(string name, double price, Type type) {

}

// Q5b Remove all nodes in the list. Make sure no memory leak will occur void remove_all() {

}

// This function uses the virtual display() method of the Drama and Comics classes to print all Books in an oragnized format. void print_all() { Container *container_traverser = list;

if (list == NULL) cout << endl << "List is empty!" << endl << endl;

while (container_traverser != NULL) { container_traverser->book->display(); container_traverser = container_traverser->next; } }

// Q6a: Save (5 points) // Save the linked list of books to a file using ofstream. // You will need to come up with a way to store the amount of Containers in linked list. // Hint: You may want to cast the enum 'type' to an int before writing it to the file. void save(string fileName) {

}

// Q6b: Load (5 points) // Load the linked list of books from a file using ifstream. // You will need to create the linked list in the same order that is was saved to a file. // You will need to create a new node for the linked list, then add it to the tail of the list. // Hint: If you casted the enum 'type' to an int, you will need to cast it back to a 'Type'. // You will use the 'type' variable read from the file to determine which constructor to use. void load(string fileName) {

} // Q7 Use quick sort or merge sort to sort the container list by the book title in ascending order. You must use a recursive function to perform sorting void listSort(Container **head) {

}

Book.cpp:

#include "Book.h"

Book::Book(string book_name, double book_price, Type book_type) { name = book_name; price = book_price; type = book_type; }

string Book::getName() { return name; }

double Book::getPrice() { return price; }

Type Book::getType() { return type; }

Book.h:

#ifndef _BOOK_H_ #define _BOOK_H_

#include using namespace std;

enum Type { drama = 0, comics };

class Book { private: string name; // private local variables double price; Type type;

public: Book(string book_name, double book_price, Type book_type); // constructor

// accessor methods string getName(); double getPrice(); Type getType();

// Q3a: Declare Friend Function Change Price // Declare a friend function named changePrice which has 2 parameters and no return value. // The first parameter is a pointer to a Book, and the second is a double. // See the helper function in hw10.cpp for proper use of this function.

virtual void display(){ } };

#endif // _BOOK_H_

Comics.cpp:

// Q2b: Define Display for Comics class // Define the method display that you declared within the Comics class in the header file // Information should be printed in the following format: // Name: // Price: // Type: Comics // (See the print_all function in hw10.cpp for proper use of this function.)

Comics.h:

// Q1b: Create Comics Class // Part 1: Create a child class of the Book class named 'Comics' // See the add function in hw10.cpp for proper use of this function. // Part2: Declare constructor which accepts the same 3 parameters as the parent class Book. // Pass the 3 parameters to the super constructor in the Book class. // Part 3: Re-declare the method display (virtual method found inside of parent class Book)

Container.cpp:

#include "Container.h"

// Constructor for Container class Container::Container(){ book = NULL; next = NULL;

}

Container.h:

#ifndef _CONTAINER_H_ #define _CONTAINER_H_

#include "Book.h"

class Container { public: Book *book; Container *next; Container(); // constructor };

#endif // _CONTAINER_H_

Drama.cpp:

// Q2a: Define Display for Drama class // Define the method display that you declared within the Drama class in the header file // Information should be printed in the following format: // Name: // Price: // Type: Drama // (See the print_all function in hw10.cpp for proper use of this function.)

Drama.h:

// Q1a: Create Drama Class // Part 1: Create a child class of the Book class named 'Drama' // See the add function in hw10.cpp for proper use of this function. // Part2: Declare constructor which accepts the same 3 parameters as the parent class Book. // Pass the 3 parameters to the super constructor in the Book class. // Part 3: Re-declare the method display (virtual method found inside of parent class Book)

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_2

Step: 3

blur-text-image_3

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

Information Modeling And Relational Databases

Authors: Terry Halpin, Tony Morgan

2nd Edition

0123735688, 978-0123735683

More Books

Students also viewed these Databases questions

Question

What behaviors do you expect from a good leader?

Answered: 1 week ago

Question

Why do HCMSs exist? Do they change over time?

Answered: 1 week ago