In: Computer Science
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.
#include <iostream>
using namespace std;
class Book
{
private:
string title,author;
float price;
int count;
public:
Book(string title,string author,float price,int
count)//constructor
{
this->title = title;
this->author = author;
this->price = price;
this->count = count;
}
void display()
{
cout<<"\nBook Title\t\tBook
Author\t\tPrice\tCount of Books on Hand";
cout<<"\n"<<title<<"\t"<<author<<"\t"<<price<<"\t"<<count;
}
void modify(char code)
{
string title,author;
float price;
int count;
switch(code)
{
case 'T':
cout<<"\nEnter the new title of the book : ";
cin>>title;
this->title =
title;
break;
case
'A':cout<<"\nEnter the new author of the book : ";
cin>>author;
this->author = author;
break;
case
'P':cout<<"\nEnter the new price of the book : ";
cin>>price;
this->price = price;
break;
case
'C':cout<<"\nEnter the new count of the books on hand :
";
cin>>count;
this->count = count;
break;
default :
cout<<"\nInvalid code ";
break;
}
}
};
int main() {
Book b("Programming in c++","bjarne
stroustrup",70.99,34);
b.display();
b.modify('P');
b.display();
return 0;
}
Output:
Book Title Book Author Price Count of Books on Hand Programming in c++ bjarne stroustrup 70.99 34 Enter the new price of the book : 65.88 Book Title Book Author Price Count of Books on Hand Programming in c++ bjarne stroustrup 65.88 34
Do ask if any doubt. Please upvote.