Call us at 0700-922-6559 or Click here

Consider a publishing company that markets both book and audio cassette version to its works. Create a class Publication that stores the title (a string) and price (type float) of a publication. Derive the following two classes from the above Publication class: Book which adds a page count (int) and Tape which adds a playing time in minutes(float). Each class should have get_data() function to get its data from the user at the keyboard. Write the main() function to test the Book and Tape classes by creating instances of them asking the user to fill in data with get_data() and then displaying it using put_data()

#include <iostream>

#include <string>

using namespace std;

// Base class Publication

class Publication {

protected:

    string title;

    float price;

public:

    void get_data() {

        cout << “Enter title: “;

        getline(cin, title);

        cout << “Enter price: “;

        cin >> price;

    }

    void put_data() {

        cout << “Title: ” << title << endl;

        cout << “Price: ” << price << endl;

    }

};

// Derived class Book

class Book : public Publication {

private:

    int pageCount;

public:

    void get_data() {

        Publication::get_data();  // Call base class get_data()

        cout << “Enter page count: “;

        cin >> pageCount;

    }

    void put_data() {

        Publication::put_data();  // Call base class put_data()

        cout << “Page count: ” << pageCount << endl;

    }

};

// Derived class Tape

class Tape : public Publication {

private:

    float playingTime;

public:

    void get_data() {

        Publication::get_data();  // Call base class get_data()

        cout << “Enter playing time (minutes): “;

        cin >> playingTime; }

    void put_data() {

        Publication::put_data();  // Call base class put_data()

        cout << “Playing time: ” << playingTime << ” minutes” << endl;  }

};

int main() {

    Book book;

    Tape tape;

    cout << “\nEnter book details:\n”;

    book.get_data();

    cout << “\nEnter tape details:\n”;

    tape.get_data();

    cout << “\nBook details:\n”;

    book.put_data();

    cout << “\nTape details:\n”;

    tape.put_data();

    return 0;

}

Output: 

Enter book details:

Enter title: The Adventures of Sherlock Holmes

Enter price: 250

Enter page count: 300

Enter tape details:

Enter title: The Jungle Book

Enter price: 180

Enter playing time (minutes): 90

Book details:

Title: The Adventures of Sherlock Holmes

Price: 250

Page count: 300

Tape details:

Title: The Jungle Book

Price: 180

Playing time: 90 minutes

Here’s a step-by-step explanation of the code:

1. Headers and Namespace:

  • #include <iostream>: Includes the iostream header for input/output operations.
  • #include <string>: Includes the string header for string manipulation.
  • using namespace std;: Brings the std namespace into scope for convenient use of its elements.

2. Base Class Publication:

  • class Publication { … }: Defines the base class Publication, representing a general publication.
    • protected: string title; float price;: Protected members accessible to derived classes, storing title and price.
    • public: void get_data() { … }; void put_data() { … };: Public member functions to get input and display data.

3. Derived Class Book:

  • class Book : public Publication { … }: Derived class representing a book, inheriting from Publication.
    • private: int pageCount;: Private member to store page count.
    • public: void get_data() { … }; void put_data() { … };: Overrides get_data() and put_data() to handle book-specific data.

4. Derived Class Tape:

  • class Tape : public Publication { … }: Derived class representing an audio cassette, inheriting from Publication.
    • private: float playingTime;: Private member to store playing time.
    • public: void get_data() { … }; void put_data() { … };: Overrides get_data() and put_data() to handle tape-specific data.

5. Main Function:

  • int main() { … }: The program’s entry point.
    • Book book; Tape tape;: Creates objects of Book and Tape classes.
    • **Prompts for input and calls get_data() for both objects.
    • **Displays details using put_data() for both objects.
    • return 0;: Indicates successful program termination.

Leave a Reply

Your email address will not be published. Required fields are marked *