Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can anyone help me with this question? Thanks a bunch Lab 5. Class Inheritance O

ID: 3916206 • Letter: C

Question

Can anyone help me with this question? Thanks a bunch

Lab 5. Class Inheritance

Objectives:

to be able to define derived classes

to be able to define class constructors, accessors, mutators in a derived class

to be able to override base class member functions

to be able to redefined base class member functions

to be able to properly test a class as it is defined

Note: This lab uses class InventoryItem that you defined in Lab 4.

Consider the following scenario: Company allForSale.com is an online store that sells several types of products online including books, electronic items (cell phones, notebooks, laptops, cameras, etc), and small home appliances. The company hires you to write a program to manage its inventory. Your first thought is to create a class to represent each type of inventory sold by company. However, you quickly realize that all inventory items have several common attributes such as product description, price, and quantity on hand. So you decide to use the InventoryItem class defined in Lab 4 as a based class (with necessary changes).

In this lab, you will (1) modify class InventoryItem to include a price data member and (2) defined a derived class of InventoryItem to represent books.

Task 1:   Add a data member to class InventoryItem

Exercise 1.Add a data member to class InventoryItem to represent the item’s price

Exercise 2.Update the class constructors to initialize the price data member

Exercise 3.Add an accessor (getPrice) that returns the value of price and a mutator to set it (setPrice).

Exercise 4.Use the new accessor function to test the class constructors and setPrice().

Exercise 5.Update the display() member function to display inventory item price, in addition to the other data members. (Note: make sure to change its name to display() if you haven’t done so already).

Exercise 6.Update the read() member function to read price, in addition to description and quantity on hand from an input stream. Note: Make sure to change its name to read(ifstream &). You may assume a simplified CSV (Comma Separated Values) format for the input: every line has the data values of one inventory item separated by commas. Example:

Iphone 6, 33, 599.00

Samsung Galaxy s6, 21, 439.50

   You may use the following function to read data for one inventory item:

void InventoryItem::read(ifstream &inFile)

       {

              string line;

              getline(inFile, line, ',');

              setDescription(line);

              int q; double p; char comma;

              inFile >> q;

              setQuantityOnHand(q);

              inFile >> comma;

              inFile >> p;

              setPrice(p);

              inFile.ignore(100, ' ');

}

Below is a UML class diagram of class InventoryItem:

InventoryItem

-          description: string

-          qantityOnHand: int

-          price: double

+ InventoryItem()

+ InventoryItem(string, int, double)

+ getDescription() const: string

+ getQuantityOnhand() const: int

+ getPrice() const : double

+ setDescription(string): void

+ setQuantityOnhand(int): void

+ setPrice(double): void

+ display() const: void

+ read(ifstream &): void

Task 1 Submission (50%):

Write a test program (i.e. function main() ) to read from the an input file any number of inventory items (i.e. read until end of file) and then displays the data on the screen using the read() and display() member functions of class InventoryItem. Use the sample data below to test your class. Submit a screenshot of your program execution.

Iphone 6, 33, 599.00

Samsung Galaxy s6, 21, 439.50

Submit a copy of your class definition and test program in a text file.

Task 2: Derived class Book

Exerise 1.Declare a class Book as a derived class of InventoryItem. You only need to do the class specification in this task. Write a specification in a header file called Book.h. Make sure to include InventoryItem.h file from Task 1.

Exerise 2.class Book should have the following data members:

Title

Name of author (assume only one author for now)

Publisher

Exerise 3.Write the prototypes of the following member functions in class Book

A default constructor that initializes the above data members to default values of your choice and uses the default constructor of the base class

Accessor and mutator functions for book title, author, and publisher

A parametrized constructor that initializes all data members including description, price, and quantity on hand. Remember to use as much of the code from the base class and to use the set functions to set data members.

A member function display to display all attributes of a book

A member function read to read all attributes of a book from an input stream.

Exerise 4.Draw a UML class diagram of class Book

Task 3: Incremental implementation and testing

Exercise 1.Write a stub for each member function of class Book. You can this either in Book.h or in a separate implementation file (Book.cpp). A function stub consists of the function header and an empty body if the function is a void function. For value-returning functions, have the function return a dummy value.

Exercise 2.Write a driver program to test your new class. In function main() of the driver program, declare one instance of class Book. Build your project and fix all syntax errors.

Exercise 3.Incrementally implement the member functions of class Book. Each time you implement a function test it. Start with the set functions, then implement the get functions. Use the get function to test the set functions. Then implement and test the constructors. Finally, implement and test the display and read member functions.

When implementing your class, you need to adhere to the following practices:

Use code from the base class whenever possible

Any changes to the data members should be done using the class mutator functions.

Task 4: Final testing of Book class

Exercise 1.Use the driver program below to test your Book class. Make sure to include the header file with the Book class you defined in the previous tasks. Create a sample input file with two or three book records for testing purpose.

//*******************************************************

// Driver program to test class Book.

// Note that it uses the vector class from the Standard Template Library

// to create a dynamic list of Book instances

#include <iostream> include <fstream>

#include <iomanip>

#include <string>

#include <vector>

#include “Book.h”

using namespace std;

int main()

{

  

       ifstream file("data.csv");   //Assume the name of input file is “data.csv”

       if (!file)

{

              cerr <<"Can't open file "<< "data.csv"<<endl;

              return(EXIT_FAILURE);

       }

      

       vector<Book> bookList;

       Book temp;

       while (file)

       {

              temp.read(file);

              bookList.push_back(temp);

       }

       for (int i = 0; i < bookList.size(); ++i)

              bookList[i].display();

   system("pause");

   return 0;

}

Task 4 Submission (50%):

Submit a copy of your input file (text format)

Submit a copy of your class definitions and test program (text format)

Submit a screenshot of your program execution

InventoryItem

-          description: string

-          qantityOnHand: int

-          price: double

+ InventoryItem()

+ InventoryItem(string, int, double)

+ getDescription() const: string

+ getQuantityOnhand() const: int

+ getPrice() const : double

+ setDescription(string): void

+ setQuantityOnhand(int): void

+ setPrice(double): void

+ display() const: void

+ read(ifstream &): void

Explanation / Answer

// File Name: InventoryItem.h
#ifndef InventoryItem_h
#define InventoryItem_h
#include <iostream>
using namespace std;

// Class InventoryItem definition
class InventoryItem
{
// Data member to store data
string description;
int quantityOnHand;
double price;
public:
// Default constructor to initialize data member
InventoryItem()
{
description = "";
quantityOnHand = 0;
price = 0.0;
}// End of default constructor

// Parameterized constructor to initialize parameter value to data member
InventoryItem(string des, int qty, double pr)
{
description = des;
quantityOnHand = qty;
price = pr;
}// End of parameterized constructor

// Function to return item description
string getDescription() const
{
return description;
}// End of function

// Function to return item quantity
int getQuantityOnhand() const
{
return quantityOnHand;
}// End of function

// Function to return item price
double getPrice() const
{
return price;
}// End of function

// Function to set item name
void setDescription(string des)
{
description = des;
}// End of function

// Function to set quantity
void setQuantityOnHand(int qty)
{
quantityOnHand = qty;
}// End of function

// Function to set price
void setPrice(double pr)
{
price = pr;
}// End of function

// Function to display item information
void display() const
{
cout<<" Item Name: "<<description<<" Quantity On Hand: "<<quantityOnHand<<" Price: "<<price;
}// End of function

// Function to read file contents
void read(ifstream &inFile)
{
string line;
int q; double p; char comma;
// Reads item description from file
getline(inFile, line, ',');
// Calls the function to set item description
setDescription(line);
// Reads item quantity from file
inFile >> q;
// Calls the function to set item quantity
setQuantityOnHand(q);
// Reads comma from file and ignores it
inFile >> comma;
// Reads item price from file
inFile >> p;
// Calls the function to set item price
setPrice(p);
}// End of function
};// End of class
#endif

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

// File Name: Book.h
#ifndef Book_h
#define Book_h
#include <iostream>
#include "InventoryItem.h"
using namespace std;

// Class Book derived from class InventoryItem
class Book : public InventoryItem
{
// Data member to store data
string title;
string author;
string publisher;
public:
// Default constructor to initialize data member
Book()
{
title = author = publisher = "";
}// End of default constructor

// Parameterized constructor to initialize parameter value to data member
Book(string des, int qty, double pr, string ti, string au, string pu) : InventoryItem(des, qty, pr)
{
title = ti;
author = au;
publisher = pu;
}// End of parameterized constructor

// Function set book title
void setTitle(string ti)
{
title = ti;
}// End of function

// Function set author name
void setAuthor(string au)
{
author = au;
}// End of function

// Function set publisher
void setPublisher(string pu)
{
publisher = pu;
}// End of function

// Function return book title
string getTitle()
{
return title;
}// End of function

// Function return author name
string getAuthor()
{
return author;
}// End of function

// Function return publisher
string getPublisher()
{
return publisher;
}// End of function

// Function to display item information
void display() const
{
InventoryItem::display();
cout<<" Book Title: "<<title<<" Author Name: "<<author<<" Publisher: "<<publisher;
}// End of function

// Function to read file contents
void read(ifstream &inFile)
{
InventoryItem::read(inFile);
string line;
// Reads item description from file
getline(inFile, line, ',');
// Calls the function to set item description
setTitle(line);
// Reads item quantity from file
getline(inFile, line, ',');
// Calls the function to set item quantity
setAuthor(line);
// Reads item price from file
getline(inFile, line);
// Calls the function to set item price
setPublisher(line);
}// End of function
};// End of class
#endif

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

// File Name: BookInventoryMain.cpp
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <vector>
#include <stdlib.h>
#include "Book.h"
using namespace std;

// main function definition
int main()
{
//Assume the name of input file is “data.csv”
ifstream file("data.csv");
// Checks whether file can be opened or not
if (!file)
{
cerr <<"Can't open file "<< "data.csv"<<endl;
exit(0);
}// End of if condition

// Declares a vector to store book information
vector<Book> bookList;
// Declares a object of Book class
Book temp;
// Loops till end of the file
while (file)
{
// Calls the function to read the file contents
temp.read(file);
// Adds the object to vector
bookList.push_back(temp);
}// End of while loop

// Loops till end of the vector
for (int i = 0; i < bookList.size()-1; ++i)
// Calls the function to display book information
bookList[i].display();
return 0;
}// End of main function

Sample Output:

Item Name: Programming Book
Quantity On Hand: 30
Price: 599
Book Title: C Programming
Author Name: Balguru Swami
Publisher: PER
Item Name: Programming Book
Quantity On Hand: 52
Price: 639.5
Book Title: C++ Prime
Author Name: Prem Chand
Publisher: TML
Item Name: Accounting Book
Quantity On Hand: 83
Price: 571
Book Title: Financial Accounting
Author Name: Ram Sankar
Publisher: TT
Item Name: Accounting Book
Quantity On Hand: 41
Price: 631.5
Book Title: Company Accounting
Author Name: Ravi Sharma
Publisher: TT

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote