C++ experiment---open a bookstore

Let's open a bookstore

Description

A publishing house can publish books and tapes. Books are priced according to the price per page multiplied by the number of pages, and the tape is priced according to the price per 10 minutes multiplied by the number of minutes of tape recording. Please define four categories: Publicatioin, Book, Tape and BookStore. among them:

Publication category:
1) The data member double price represents the unit price (for books, it is the price per page; for tape, it is the price per 10 minutes of recording).

2) The data member int length represents the length of the publication. For a book, it is the number of pages; for a tape, it is the number of minutes.

3) The member function getTotalPrice() is used to return the price of a publication.

4) The constructor Publication(double, int) is used to construct a publication.

5) The member functions double getPrice() const and int getLength() are used to return the unit price and length of the publication.

6) Destructor.

The Book class is a subclass of Publication.
1) Constructor Book(double,int).

2) Override the getTotalPrice of the parent class to return the pricing, which is the unit price multiplied by the length (that is, the number of pages).

3) Destructor.

Tape is a subclass of Publication:
1) Constructor Tape(double,int).

2) Override the getTotalPrice of the parent class to return the price. Note: The price attribute is the unit price per 10 minutes of recording, and the length of the tape is not necessarily an integer multiple of 10. When calculating the price, the portion of less than 10 minutes will be calculated as 10 minutes.

3) Destructor.

4. BookStore is a bookstore with data members Publications **pubs, which is a list of publications owned by the bookstore; int num represents the number of publications owned by the bookstore. The member functions int getNumOfBook() and int getNumOfTape() calculate the number of Books and Tapes in the bookstore, respectively. This class has been given in the appcode code.

Input

Enter multiple lines.

The first line is the integer M>0, indicating that there are M test cases.

Each test occupies one line and is divided into three parts: The first part is the publication type (B for Book, T for Tape), unit price and quantity (number of pages or minutes).

Output

See example.

Sample Input

3
B 0.10 201
T 0.50 100
T 0.40 105

Sample Output

Call Publication’s constructor!
Call Book’s constructor!
Call Publication’s constructor!
Call Tape’s constructor!
Call Publication’s constructor!
Call Tape’s constructor!
Call Publication’s constructor!
Call Book’s constructor!
Call Publication’s constructor!
Call Tape’s constructor!
Call Publication’s constructor!
Call Tape’s constructor!
There are 1 books and 2 tapes. Their total price is 29.50.
Call Book’s de-constructor!
Call Publication’s de-constructor!
Call Tape’s de-constructor!
Call Publication’s de-constructor!
Call Tape’s de-constructor!
Call Publication’s de-constructor!
Call Book’s de-constructor!
Call Publication’s de-constructor!
Call Tape’s de-constructor!
Call Publication’s de-constructor!
Call Tape’s de-constructor!
Call Publication’s de-constructor!
Call BookStore’s de-constructor!

HINT
uses typeid to determine the type of the actual object pointed to by the object pointer.

Title given code

class BookStore
{
    
    
private:
    Publication **pubs;
    int num;
public:
    BookStore(Publication **p, int n)
    {
    
    
        pubs = new Publication*[n];
        num = n;
        for (int i = 0; i < n; i++)
        {
    
    
            if (typeid(*(p[i])) == typeid(Book))
            {
    
    
                pubs[i] = new Book(p[i]->getPrice(), p[i]->getLength());
            }
            else
            {
    
    
                pubs[i] = new Tape(p[i]->getPrice(), p[i]->getLength());
            }
        }
    }
    int getNumOfBook()
    {
    
    
        int c = 0;
        for (int i = 0; i < num; i++)
        {
    
    
            if (typeid(*(pubs[i])) == typeid(Book))
                c++;
        }
        return c;
    }
    int getNumOfTape()
    {
    
    
        int c = 0;
        for (int i = 0; i < num; i++)
        {
    
    
            if (typeid(*(pubs[i])) == typeid(Tape))
                c++;
        }
        return c;
    }
    ~BookStore()
    {
    
    
        for (int i = 0; i < num; i++)
        {
    
    
            delete pubs[i];
        }
        delete[] pubs;
        cout<<"Call BookStore's de-constructor!\n";
    }
};
int main()
{
    
    
    int cases, date;
    char type;
    double total,price;
    Publication **pub;
    cin>>cases;
    pub = new Publication*[cases];
    for (int i = 0; i < cases; i++)
    {
    
    
        cin>>type>>price>>date;
        switch(type)
        {
    
    
        case 'B':
            pub[i] = new Book(price,date);
            break;
        case 'T':
            pub[i] = new Tape(price,date);
            break;
        }
    }
    BookStore bookStore(pub, cases);
    cout<<"There are "<<bookStore.getNumOfBook()<<" books and "<<bookStore.getNumOfTape()<<" tapes.";
    total = 0;
    for (int i = 0; i < cases; i++)
    {
    
    
        total += pub[i] -> getTotalPrice();
    }
    cout<<" Their total price is "<<setprecision(2)<<fixed<<total<<"."<<endl;
    for (int i = 0; i < cases; i++)
    {
    
    
        delete pub[i];
    }
    delete[] pub;
    return 0;
}

Guess you like

Origin blog.csdn.net/timelessx_x/article/details/115146093