I am supposed to come up with a feature for a bookstore management system that i
ID: 3682435 • Letter: I
Question
I am supposed to come up with a feature for a bookstore management system that implements polymorphism in C++. I decided to add an abstract Person class, and an abstract Employee class that inherits from the Person class. I then added a Manager class and a Cashier class, which inherit from the Employee class, and added a Supplier class and Customer class, which both inherit from the Person class. The assignment description said to not use the Factory design pattern or dynamic casting, so I am having trouble coming up with ideas for functions that are invoked polymorphically.
Explanation / Answer
his is an example of C++ polymorphism.
A book store has many books on sale. Some of the books are 15% off. A customer can select books and add them to his cart. When each book is added, the program can find if this book is on sale and then compute the net price of the book. At the end, the total price will be computed. So I can bind objects of both base class and derivatice class to a base class pointer. Then I can call its member function through this pointer without worrying the type of the object. The complier would automatically call the member function of the object’s real type. If it is a base class, the member function from a base class would be called and vice versa. However, I can always find the type of an object by using typeid(). It is something similar to isinstanceof in Java. But I need to remember that typeid(ptr) is not the same as typeid(*ptr). typeid(*ptr) gets the type of the object ptr pointing to.
#include <iostream>
#include <sstream>
#include <string>
#include <cstdlib>
#include <map>
#include <typeinfo>
using namespace std;
class Book
{
public:
Book(string &book_isbn, double sale_price): isbn(book_isbn), price(sale_price) {}
virtual double get_price(int num) const
{
return num*price;
}
protected:
double price;
string isbn;
};
class BookOnSale : public Book
{
public:
BookOnSale(string &book_isbn, double sale_price): Book(book_isbn, sale_price) {}
double get_price(int num) const
{
return num * price * 0.85; // 15% off :D
}
};
int main()
{
map<string, Book *> catalog;
map<string, Book *>::iterator iter;
//The shop owner starts to add books on his shelf
for(int i = 0; i < 10; i++)
{
//let’s make up some isbn, need to convert number to string
int number = 1111 + i;
string isbn;
stringstream convert;
convert << number;
isbn = convert.str();
cout << isbn << “: “;
//make up the prices
double price = (rand() % 400) / 10.0 + 40; //40.0 ~ 79.9
cout <<price << ” “;
if(i%3 == 0) //He randomly selects some book to be on sale
{
Book *ptr = new BookOnSale(isbn, price);
catalog.insert(make_pair(isbn, ptr));
cout << “on sale!”;
}
else{
Book *ptr = new Book(isbn, price);
catalog.insert(make_pair(isbn, ptr));
}
cout << endl;
}
/*for(iter = catalog.begin(); iter != catalog.end(); iter++)
{
cout << iter->first << ” ” << (iter->second)->get_price(1) << endl;
}*/
cout << “Please enter the book isbn and quantity eg. 1111 1 Ctrl+D to end” << endl;
string isbn_inCart; //book I want to buy
int quantity; //quantitaty
double tol = 0;
while(cin >> isbn_inCart >> quantity)
{
//cout << isbn_inCart << “: “;
//cout << quantity << endl;
iter = catalog.find(isbn_inCart);
if(iter == catalog.end())
{
cout << “Sorry, we do not have the book right now.” << endl;
}
else{
if(typeid(*iter->second) == typeid(BookOnSale)) //typeid checks the type of an object in a runtime environment
{
cout << “You are lucky! This item is on sale!” << endl;
}
//polymorphism, we do not need to know the type of the object, the complier will automatically call the correct get_price function
double netprice = (iter->second)->get_price(quantity);
cout << “price : ” << netprice << endl;
tol = tol + netprice;
}
}
cout << “Total price: ” << tol << endl;
//Be careful with memory leakage
for(iter = catalog.begin(); iter != catalog.end(); iter++)
{
delete iter->second;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.