Exercise 4. Update theread member functions of class Book and class InventoryItem to read book information from an input stream in the following format:
category, description, quantity on hand, price, number of authors, list of author names separated by commas, publisher
where category is a string that indicates the inventory item category. Assume the allowable categories are: book for books and general for anything else.
Note the following:
1. Book::read must call InventoryItem::read
2. The comma after price must be consumed by Book:read
3. You may use the following function to trim leading and trailing white spaces from strings after you call getline():
voidtrim(string&s)
{
size_t p = s.find_first_not_of(" \t\n");
s.erase(0, p);
p = s.find_last_not_of(" \t\n");
if (string::npos != p)
s.erase(p+1);
}
Exercise 5. Overload the stream extraction operator >> for both classes. Again, Do NOT replace theread function, simply have the operator >> call functionread.
Exercise 6. Use the driver program below to demo your updated Book class. You may also download a text version of it from file demoNewLab in the Lab6 folder (on EagleOnline). Make sure to include the header files with the Book class definition.
#include"book.h"
#include
//void testList();
intdemoNewBook();
intmain() {
demoNewBook();
system("pause");
return 0;
}
intdemoNewBook()
{
ifstream file("data.csv"); //Assume the name of input file is "data.csv"
if(!file)
{
cerr<<"Can't open file "<<"data.csv"< return(EXIT_FAILURE);
}
vectorbookList;
Book temp;
while (while(!file.eof())
{
file >> temp;
bookList.push_back(temp);
}
for (size_ti = 0; i cout<<"Book "< cout< }
system("pause");
return 0;
}
Task 5: Polymorphism
Assume inventory items are of two categories: books or general items (category list can be expanded). As discussed above, a general inventory item has the data fields description, quantity on hand, and price whereas a book has the additional fields title, author list (number of authors and list of their names), and publisher name. Assume that inventory data files contain information from the two categories, each tagged with their category. Example;
general, IPhone 6, 35, 599.0
book, CS book, 15, 199.50, Intro to Programming with C++, 2, D.S. Malik, T. J. Holly, Pearson
general, Samsung Galaxy 7S, 376.99
book, Novel Sci Fi, 2, 6.99, The Martians, 1, Andy Weir, Media Type
Exercise 1. Consider the following test program. What do you expect it to output if run with the sample input in the box above?
#include"book.h"
#include
//void testList();
//intdemoNewBook();
inttestPolymorphism();
intmain()
{
testPolymorphism();
system("pause");
return 0;
}
//*******************************************************
// This program is used to test polymorphism of input and output operations of class Book
inttestPolymorphism()
{
ifstream file("data2.csv"); //Assume the name of input file is "data2.csv"
if(!file)
{
cerr<<"Can't open file "<<"data.csv"< return(EXIT_FAILURE);
}
InventoryItem *temp;
string category;
getline(file, category, ','); // get the category for first item
trim(category); // remove leading and trailing spaces
if (category == "general")
temp = newInventoryItem;
elseif (category == "book")
temp = newBook;
else
{ cout< return(EXIT_FAILURE);
}
file >> *temp;
cout<< *temp;
getline(file, category, ','); // get the category for 2nd item
trim(category);
if (category == "general")
temp = newInventoryItem;
elseif (category == "book")
temp = newBook;
else
{ cout< return(EXIT_FAILURE);
}
file >> *temp;
cout<< *temp;
return 0;
}
Exercise 2. Run the test program and verify your prediction. Why isn't the book information displayed correctly?
Exercise 3. Make the required changes to base classInventoryItem so that the appropriate input and output functions are called depending on the type of object calling them i.e. make the functions polymorphic.
Exercise 4. Demo you program with the test data and program of exercise 1 above.
Final Project Demo and Submission:
Demo your project with the test driver function below and the sample input shown in the textbox above. Submit a zip file containing the final version of your project on EagleOnline by the assignment due date.
//*******************************************************
// This test function is used to demo programming assignment #3
intdemoProject()
{
ifstream file("data2.csv"); //Assume the name of input file is "data.csv"
if(!file)
{
cerr<<"Can't open file "<<"data.csv"< return(EXIT_FAILURE);
}
vectorproductList;
InventoryItem *temp;
string category;
while(!file.eof())
{
getline(file, category, ','); // get the category for next item
trim(category);
if (category == "general")
temp = newInventoryItem;
elseif (category == "book")
temp = newBook;
else
{
cerr< return(EXIT_FAILURE);
}
file >> *temp;
productList.push_back(temp);
}
for (inti = productList.size() -1; i>= 0 ;i--){
cout<<"Book "< cout< }
return 0;
}
Attachment:- Attachments.rar