Question

In: Computer Science

Write a c++ class definition for an abstract data type describing a bookstore inventory. Each book...

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.

Solutions

Expert Solution

#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.


Related Solutions

Here is a C++ class definition for an abstract data type LinkedList of string objects. Implement...
Here is a C++ class definition for an abstract data type LinkedList of string objects. Implement each member function in the class below. Some of the functions we may have already done in the lecture, that's fine, try to do those first without looking at your notes. You may add whatever private data members or private member functions you want to this class. #include #include typedef std::string ItemType; struct Node { ItemType value; Node *next; }; class LinkedList { private:...
A Bookstore Application C++ Design the class Book. Each object of the class Book can hold...
A Bookstore Application C++ Design the class Book. Each object of the class Book can hold the following information about a book: title, authors, publisher, ISBN Include the member functions to perform the various operations on the objects of Book. For example, the typical operations that can be performed on the title are to show the title, set the title. Add similar operations for the publisher, ISBN , and authors. Add the appropriate constructors and a destructor (if one is...
Implement a program as an object using a class (abstract data type) in C++ that does...
Implement a program as an object using a class (abstract data type) in C++ that does the following: 1) reads the firstName, lastName and 3 test scores of at least five students. 2) calculate student test score totals, average, letter grade for each student. 3) Display the results in a table format showing firstName, lastName, test1, test2, test3, total, average, letterGrade, of all the students. 3 files .h, .cpp, main.cpp create an object that can hold records. must get records...
Week 3 In-Class Exercise C++ Payroll Design a PayRoll class that is an abstract data type...
Week 3 In-Class Exercise C++ Payroll Design a PayRoll class that is an abstract data type for payroll. It has data members for an employee’s hourly pay rate, number of hours worked, and total pay for the week. Your class must include the following member functions: a constructor to set the hours and pay rate as arguments, a default constructor to set data members to 0, member functions to set each of the member variables to values given as an...
1. Implement the stack abstract data type. Write it as a separate class called Stack. For...
1. Implement the stack abstract data type. Write it as a separate class called Stack. For simplicity the data type to be stored in the stack is String but you can also use generic type. 2. Test your class implementation by calling push() and pop(). If the stack is empty (size equals zero) pop() should return null and print that “stack is empty”. If stack is full (size equals max) push() returns false and prints that “stack is full”. This...
Assume you have created the following data definition class: public abstract class Organization { private String...
Assume you have created the following data definition class: public abstract class Organization { private String name;    private int numEmployees;    public static final double TAX_RATE = 0.01;    public String getName() { return this.name; }    public int getNumEmployees() { return this.numEmployees; }         public abstract double calculateTax() {       return this.numEmployees * TAX_RATE;    } } In your own words, briefly (1-2 sentences) explain why the data definition class will not compile. Then, write modified code that...
Write in C++ Abstract/Virtual Rock Paper Scissors Create an abstract Player class that consists of private...
Write in C++ Abstract/Virtual Rock Paper Scissors Create an abstract Player class that consists of private data for name, selection, wins, and losses. It must have a non-default constructor that requires name. It may not contain a default constructor. Create overloaded functions for the ++ and - - operator. The overloaded ++operator will add to the number of wins, while the - - operator will add to the losses. You will create two different child classes of player, Human and...
Write a C++ PROGRAM: Add the function min as an abstract function to the class arrayListType...
Write a C++ PROGRAM: Add the function min as an abstract function to the class arrayListType to return the smallest element of the list. Also, write the definition of the function min in the class unorderedArrayListType and write a program to test this function.
(In java language) Write an abstract class called House. The class should have type (mobile, multi-level,...
(In java language) Write an abstract class called House. The class should have type (mobile, multi-level, cottage, etc.) and size. Provide the following methods: A no-arg/default constructor. A constructor that accepts parameters. A constructor that accepts the type of the house and sets the size to 100. All other required methods. An abstract method for calculating heating cost. Come up with another abstract method of your own. Then write 2 subclasses, one for mobile house and one for cottage. Add...
Java - Write an abstract class called Shape with a string data field called colour. Write...
Java - Write an abstract class called Shape with a string data field called colour. Write a getter and setter for colour. Write a constructor that takes colour as the only argument. Write an abstract method called getArea()
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT