Question
ONLY USE: #include #include #include using namespace std; Write a C++ program that inputs from a file and outputs data to the screen. A base
ONLY USE:
#include
#include
#include
using namespace std;
Write a C++ program that inputs from a file and outputs data to the screen. A base class called book defines the name, type and number of pages in a book. An integer called code is set to 0 for Fiction, 1 for Nonfiction, 2 for Reference. Fiction, Nonfiction and Reference derive from book. Fiction contains a field describing the genre of the Fiction. Nonfiction has two fields: subject and number of illustrations. The data file (books.txt) contains
0 Thunderball 245 Spy
0 Larceny 289 Mystery
1 Airplanes 456 Aviation 250
0 Killer 198 Drama
1 Football 434 Sports 400
1 Lincoln 432 History 307
2 Dictionary 987
2 Atlas 414
producing this output:
Name:Thunderball Type:Fiction Pages:245 Genre:Spy
Name:Larceny Type:Fiction Pages:289 Genre:Mystery
Name:Airplanes Type:Nonfiction Pages:456 Subject:Aviation Illustrations:250
Name:Killer Type:Fiction Pages:198 Genre:Drama
Name:Football Type:Nonfiction Pages:434 Subject:Sports Illustrations:400
Name:Lincoln Type:Nonfiction Pages:432 Subject:History Illustrations:307
Name:Dictionary Type:Reference Pages:987
Name:Atlas Type:Reference Pages:414
Total pages:3455
In main, a single class variable of Book called book is used to store each book of either fiction, nonfiction or reference. Make an enum type for books called Books. Create a BookFactory to construct the required objectand use the enum Books. Use virtual functions to read and print the data. Each class can read only the data declared in it. Each class can print only the data declared in it. A count of the total number of pages of all books in the data is maintained by class book.
Create a PrintAll function for printing books. PrintAll's prototype is
void PrintAll(Book* book);
PrintAll calls the polymorphic Print method of book (the parameter in PrintAll). Remember that each class is responsible for reading and printing its own data. So Fiction, Nonfiction and Reference shouldn't print name, type and the number of pages from Book.
This assignment is about polymorphism, virtual methods, and proper assignment of class responsibility. Each class must read and print its data. MUST FOLLOW THESE INSTRUCTIONS EXACTLY
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