Write a c++ class definition for an abstract data type describing a bookstore in
ID: 3881464 • Letter: W
Question
Write a c++ class definition for an abstract data type describing a bookstore inventory. Each book has the following attributes:
Book Title (character string);
Book Author (character string);
Book Price (Floating point number having two decimal places);
Count of books on hand (int);
The member functions are as follows:
A constructor that is used to initialize all four elements of the structure to values inputted by the user;
A function that displays in a readable tabular form the contents of a book in inventory;
A modify function that has an argument a code ('T', 'A', 'P', or 'C') indicating which attribute (title,author,price or count) of the indicated book is to be changed. Based upon the attribute code, the appropriate prompt should be displayed to allow the user to enter the new value of the attribute.
Explanation / Answer
// C++ class definition for an abstract data type describing a bookstore inventory
class Book
{
private:
string title;
string author;
float price;
int qty;
public:
Book(string title,string author,float price,int qty)
{
Book::title = title;
Book::author = author;
Book::price = price;
Book::qty = qty;
}
void displayBookContents()
{
cout<<" Title : "<<title;
cout<<" Author: "<<author;
cout<<" Price: "<<price;
cout<<" Count of books on hand: "<<qty;
}
void modify(char code)
{
switch(code)
{
case 'T' : cout<<" Enter the new value for the title : ";
getline(cin,title);
break;
case 'A' : cout<<" Enter the new value for the author: ";
getline(cin,author);
break;
case 'P' : cout<<" Enter the new value for the price: ";
cin>>price;
break;
case 'C' : cout<<" Enter the new value for count of books on hand: ";
cin>>qty;
break;
default: cout<<" Invalid option";
}
}
};
//end of class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.