class bookType { public: void setBookTitle(string s); //sets the bookTitle to s
ID: 3807344 • Letter: C
Question
class bookType
{
public:
void setBookTitle(string s);
//sets the bookTitle to s
void setBookISBN(string ISBN);
//sets the private member bookISBN to the parameter
void setBookPrice(double cost);
//sets the private member bookPrice to cost
void setCopiesInStock(int noOfCopies);
//sets the private member copiesInStock to noOfCopies
void printInfo() const;
//prints the bookTitle, bookISBN, the bookPrice and the //copiesInStock
string getBookISBN() const;
//returns the bookISBN
double getBookPrice() const;
//returns the bookPrice
int showQuantityInStock() const;
//returns the quantity in stock
void updateQuantity(int addBooks);
//adds addBooks to the quantityInStock, so that the quantity //in stock now has it original value plus the parameter sent //to this function
private:
string bookTitle;
string bookISBN;
double price;
int quantity;
};
a) Write the function defintion for getBookPrice. (Write the actual code for the function. It would be in the implementation .cpp file)
b) Write the function definition for updateQuantity. (Write the actual code for the function. It would be in the implementation .cpp file.)
c) You want to add two constructors to the above class. One is the default constructor and the other is a constructor that takes four parameters and sets the bookName, bookISBN, price, and quantity to the four parameters. Show the two function prototype lines that must be added to the class definition. (YOU DO NOT HAVE TO WRITE THE FULL CONSTRUCTOR DEFINITION)
d) Assume the following in your main program (the constructors above have been written and added):
bookType bestSeller("The Help","0399155341",18.65,0);
i) Write the code in the main program that will print out the information about bestSeller.
ii) Write the code in the main program that will add 50 to the number of copies in stock for bestseller.
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
class bookType
{
public:
bookType();// default constructor
bookType(string s,string ISBN,double cost,int noOfCopies);// paramaterized constructor
void setBookTitle(string s);
//sets the bookTitle to s
void setBookISBN(string ISBN);
//sets the private member bookISBN to the parameter
void setBookPrice(double cost);
//sets the private member bookPrice to cost
void setCopiesInStock(int noOfCopies);
//sets the private member copiesInStock to noOfCopies
void printInfo() const;
//prints the bookTitle, bookISBN, the bookPrice and the //copiesInStock
string getBookISBN() const;
//returns the bookISBN
double getBookPrice() const;
//returns the bookPrice
int showQuantityInStock() const;
//returns the quantity in stock
void updateQuantity(int addBooks);
//adds addBooks to the quantityInStock, so that the quantity //in stock now has it original value plus the parameter sent //to this function
private:
string bookTitle;
string bookISBN;
double price;
int quantity;
};
bookType::bookType(){
cout << "Object is being created" << endl;
}
bookType::bookType(string s,string ISBN,double cost,int noOfCopies){
bookTitle = s;
bookISBN = ISBN;
price = cost;
quantity = noOfCopies;
}
void bookType::setBookTitle(string s) {
bookTitle = s;
}
void bookType::setBookISBN(string ISBN) {
bookISBN = ISBN;
}
void bookType::setBookPrice(double cost) {
price = cost;
}
void bookType::setCopiesInStock(int noOfCopies) {
quantity = noOfCopies;
}
void bookType::printInfo() const {
cout<<"Title : "<<bookTitle<<endl;
cout<<"ISBN : "<<bookISBN<<endl;
cout<<"PRICE : "<<price<<endl;
cout<<"COPIES IN STOCK : "<<quantity<<endl;
}
string bookType::getBookISBN() const {
return bookISBN;
}
double bookType::getBookPrice() const {
return price;
}
int bookType::showQuantityInStock() const {
return quantity;
}
void bookType::updateQuantity(int addBooks){
quantity+=addBooks;
}
int main()
{
bookType bestSeller("The Help","0399155341",18.65,0);// make object of bookType
bestSeller.printInfo(); // calling the printinfo function to print all the values
bestSeller.updateQuantity(50); // update the quantity of stock
bestSeller.printInfo();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.