Inheritance Questions
Inheritance Questions
Question 1: Write a C++ program to demonstrate single-level inheritance. Create a base class
Person with the following attributes name, age and methods displayDetails() to display the
details of the person. Create a derived class Student that inherits from Person and has an
additional attribute: grade Add a method input() to take the details of student and
displayStudentDetails() in the Student class to display all the details (name, age, and
grade). Write a main() function to create an object of the Student class, set values for all
attributes, and call the appropriate methods to display the details.
Question 2: Create a base class Shape with the following: A method setDimensions() to
accept values for length and breadth (both integers). A method displayDimensions() to
display the dimensions. Create a derived class Rectangle that inherits from Shape and adds the
following: A method calculateArea() to calculate the area of the rectangle. A method
displayArea() to display the calculated area. In the main() function, create an object of the
Rectangle class, set dimensions, and display both the dimensions and the calculated area.
Question 3: 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 float) of a
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 float). Each of these three classes should have a
getdata() function to get its data from the user at the keyboard, and a putdata() function to display
its data.
Question 4: Imagine a software company that develops and markets different types of digital
media. Create a base class Media that stores the name (a string) and type (a string, e.g., "Video"
or "Audio") of the media. From this class, derive two classes: Video, which adds a resolution
(type string, e.g., "1080p") and a duration (type float, in minutes). Audio, which adds a bitrate
(type int, in kbps) and a duration (type float, in minutes). Each of these three classes should have
the following methods to gather input from the user and a method to display the media
information.
Multiple Inheritance
Question 5: You are tasked with creating a simple employee management system for a
company. The company has two main types of employee classifications: Departmental
Employees and Contractual Employees. A person may belong to both categories if they are
working on a contract for another department while being a full-time employee. Create a class
DepartmentEmployee with attributes like departmentName and a method showDepartment().
Create another class ContractEmployee with attributes like contractDuration and a method
showContractDetails(). Create a class Employee, which inherits from both DepartmentEmployee
and ContractEmployee, and add a method displayDetails() to show all employee details.
Question 6: You are designing a vehicle management system for a transport company. Some
vehicles can serve dual purposes, such as a vehicle that can act as both a cargo transporter and a
passenger carrier. Create a class CargoVehicle with attributes like cargoCapacity and a method
showCargoCapacity(). Create another class PassengerVehicle with attributes like
passengerCapacity and a method showPassengerCapacity(). Create a class HybridVehicle that
inherits from both CargoVehicle and PassengerVehicle. This class should have a method
displayDetails() to show the capacities for both cargo and passengers. If both base classes have a
method getType(), resolve the ambiguity and specify which getType() is used in the derived
class.
Question 7: Education Management System You are designing a system for a university that
involves roles like students, researchers, and teaching assistants (TAs). Create a class Student
with attributes like rollNumber, course, and a method showStudentDetails(). Create another class
Researcher with attributes like researchTopic and a method showResearchDetails(). Create a
class TeachingAssistant that inherits from both Student and Researcher. This class should add an
attribute subjectTaught and implement a method displayDetails() that combines student,
research, and teaching assistant information.
Question 8: Smart Device Control System You are building a system for managing smart
home devices where certain devices can be multifunctional. For example, a device may function
as both a light controller and a thermostat. Create a class LightController with attributes like
brightnessLevel and a method adjustBrightness(). Create another class Thermostat with attributes
like temperatureSetting and a method adjustTemperature(). Create a class SmartDevice that
inherits from both LightController and Thermostat. This class should have a method
controlDevice() that can call methods from both base classes to adjust brightness and
temperature.