Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

Imagine a publishing company that markets both book and audiocassette versions of its works. Create a class publication that stores the title (a string) and price (type fl oat) of publication. From this class, derive two classes: book, which adds a page count (type int), and tape, which adds a playing time in minutes (type fl oat). Each of these three classes should have a getdata() function to get its data from the user as the keyboard, and a putdata() function to display its data. Write a main() program to test the book and tape classes by creating instance of them, asking the user to fi ll in data with getdata(), and then displaying data with putdata().

Short Answer

Expert verified
Define 'Publication', derive 'Book' and 'Tape'; implement 'getdata()' and 'putdata()'; use these in main().

Step by step solution

01

Create the Base Class

Define a base class 'Publication' in a programming language like Python. This class should have attributes for storing the title (a string) and price (a float). It should also define two methods: 'getdata', which prompts the user to enter the title and price, and 'putdata', which displays these values.
02

Create the Book Class

Derive a new class 'Book' from the 'Publication' class. In addition to the inherited attributes, add an integer attribute for the page count. Override the 'getdata' method to ask the user for page count as well, and the 'putdata' method to display this additional information.
03

Create the Tape Class

Similar to the 'Book' class, derive the 'Tape' class from 'Publication'. This class should include a float attribute for playing time (in minutes). Override the 'getdata' method to prompt for playing time, and the 'putdata' method to include this data in the output.
04

Implement the Main Function

Write a main function that creates instances of 'Book' and 'Tape'. Use the 'getdata' method to collect data from the user for a book and a tape instance. Finally, invoke the 'putdata' methods to display the information about the created instances.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

Key Concepts

These are the key concepts you need to understand to accurately answer the question.

Inheritance in Programming
Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a new class to inherit attributes and methods from an existing class. This helps in reducing redundancy by enabling reusability of code, and structuring related classes in a hierarchy. In the provided exercise, the base class 'Publication' is used as a parent class and it provides common properties, like 'title' and 'price'. These properties are shared across all derived classes. The 'Book' and 'Tape' classes inherit these properties from 'Publication', and then extend their functionalities by adding their own unique attributes: 'page count' for Book and 'playing time' for Tape.
  • Parent Class: Defines common attributes and methods.
  • Child Class: Inherits from the parent and can have additional attributes.
Inheritance is not just about sharing attributes, but also about sharing behavior through methods. This inheritance of methods is what allows 'Book' and 'Tape' to redefine how they gather and display data through the 'getdata' and 'putdata' methods.
Classes and Objects
In OOP, classes are blueprints for creating objects. An object is an instance of a class and represents a real-world entity. This exercise illustrates the creation of classes such as 'Publication', 'Book', and 'Tape'.
  • **Class**: Defines attributes (properties like title and price) and methods (functions like getdata and putdata).
  • **Object**: An individual instance of a class, e.g., a specific book with a title, price, and page count.
To create an object, you first need to define its class. Classes define the properties and functionalities that the objects will have. In the exercise, 'Book' and 'Tape' are specific object types that can then be instantiated to represent different books and tapes from the publishing company.
Each object can hold its own data, and when methods are called (like 'getdata' and 'putdata'), they can perform operations that are specific to that data.
Methods in Programming
Methods in OOP are the actions that objects can perform. They are typically functions defined inside a class. In the exercise, methods like 'getdata' and 'putdata' are integral to how objects of classes interact with the user and manage data.
- **getdata()**: This method is responsible for accepting input from the user. For 'Book' and 'Tape' classes, it means expanding the base class 'Publication' method to gather additional data like page count or playing time information.
- **putdata()**: This method is tasked with displaying the attribute values of an object. In 'Book' and 'Tape', it will include outputs relevant to their specific attributes in addition to displaying the common ones from 'Publication'.
Using methods:
  • Encapsulates behaviors within a class, enhancing modular design.
  • Allows for function overriding, enabling child classes to modify a method inherited from parent classes as per specific requirements.
By implementing `getdata()` and `putdata()` methods, the exercise showcases how you can design functions that embody behaviors for data input and output in an object-oriented layout.

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Study anywhere. Anytime. Across all devices.

Sign-up for free