Answers
Code:
#include <iostream>
#include <cstring>
using namespace std;
class bookType
{
private:
string bookName;
string bookISBN;
double price;
int quantity;
public:
bookType()
{ //Default const
cout << "Default Constructor Called" << endl;
}
//Four Parameter Const bellow
bookType(string bookName, string bookISBN, double price, int quantity)
{
this -> bookName = bookName;
this -> bookISBN = bookISBN;
this -> price = price;
this -> quantity = quantity;
}
void setBookTitle(string s)
{
bookName = s;
}
//sets the bookTitle to s
void setBookISBN(string ISBN) {
bookISBN = ISBN;
}
//sets the private member bookISBN to the parameter
void setBookPrice(double cost) {
this->price = cost;
}
//sets the private member bookPrice to cost
void setCopiesInStock(int noOfCopies) {
this->quantity = noOfCopies;
}
//sets the private member copiesInStock to noOfCopies
void printInfo() {
cout << "Book Name " << bookName << endl;
cout << "Book ISBN " << bookISBN << endl;
cout << "Price " << price << endl;
cout << "Quantity " << quantity << endl;
}
//prints the bookTitle, bookISBN, the bookPrice and the //copiesInStock
string getBookISBN() {
return this->bookISBN;
}
//returns the bookISBN
double getBookPrice() {
return price;
}
//returns the bookPrice
int showQuantityInStock() {
return quantity;
}
//returns the quantity in stock
void updateQuantity(int addBooks) {
quantity += addBooks;
}
// adds addBooks to the quantityInStock, so that the quantity //in stock now has it original
// value plus the parameter sent to this function
int operator >=(const bookType& bk) {
if(quantity >= bk.quantity) return 1;
else if(bk.quantity >= quantity) return -1;
else return 0;
}
// Comparison operator overloading
//integer can be returned to the main function
};
int main()
{
bookType b1; //default constructor
int result;
bookType b2("harry potter","jp123",250,5); //parameterized constructor
b1.setBookTitle("Half Girlfriend");
b1.setBookISBN("hg123");
b1.setBookPrice(280);
b1.setCopiesInStock(12);
cout<<endl;
cout<<"First book details:"<<endl;
b1.printInfo();
cout<<endl;
cout<<"Second book details:"<<endl;
cout<<"Book ISBN "<<b2.getBookISBN()<<endl;
cout<<"Price "<<b2.getBookPrice()<<endl;
cout<<"Quantity "<<b2.showQuantityInStock()<<endl;
b2.updateQuantity(10); //updating quantity of second book i.e. adding 10 more books
cout<<endl<<"After Updating Second Book....."<<endl<<endl;
b2.printInfo();
cout<<endl;
result=b1>=b2; //int result stores the value from the operator overloaded function
if(result>0) //result is checked and accordingly the statement is printed
{
cout<<"First Book Quantity is more";
}
else if(result<0)
{
cout<<"Second Book Quantity is more";
}
else
{
cout<<"Both Books have same Quantity";
}
return 0;
}
Output: