0% found this document useful (0 votes)
5 views3 pages

Inheritance Concepts in OOP Tasks

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Inheritance Concepts in OOP Tasks

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Task 1: Single inheritance

Scenario: A company has different types of employees. All employees have a name
and an employee ID. A manager is a special type of employee who also has a
department.

Instructions:
1. Create a base class Employee with protected members for the
employee's name (string) and employeeID (integer). Include a constructor to initialize
these members.

2. Create a derived class Manager that publicly inherits from Employee .

3. Add a department member (string) to the Manager class.

4. Include a constructor in Manager that calls the base class constructor and initializes its
own member.

5. Implement a displayInfo() method in both classes.


The Manager 's displayInfo() should call the Employee 's displayInfo() and then
print the department.

6. In main() , create an object of Manager and demonstrate that it can access and
display all the inherited and its own members.

Task 2: Multilevel inheritance


Scenario: Model a vehicle system with multiple levels of specialization.

Instructions:
1. Create a base class Vehicle with protected members
for speed and fuelCapacity . Add a method startEngine() .

2. Create a derived class Car that inherits from Vehicle . Add


a protected member numberOfDoors and a drive() method.

3. Create another class ElectricCar that inherits from Car . Add


a protected member batteryCapacity and override the drive() method to reflect
that it's electric.
4. In main() , create an ElectricCar object and call the methods from all three classes
to demonstrate the inheritance chain.

Task 3: Hierarchical inheritance


Scenario: A shape drawing program needs to calculate the area of different shapes.

Instructions:
1. Create a base class Shape with protected members width and height . Include a
method getArea() that returns 0 (since a generic shape has no specific area).

2. Create two derived classes, Rectangle and Triangle , that both publicly inherit
from Shape .

3. Override the getArea() method in both derived classes to calculate the correct area
for that shape.

4. In main() , create objects of both Rectangle and Triangle and call getArea() on
each to prove that the base class functionality has been specialized.

Task 4: Multiple inheritance with ambiguity


Scenario: A device that has both audio and video capabilities.

Instructions:
1. Create two independent base classes: AudioDevice with a playAudio() method
and VideoDevice with a playVideo() method. Both should have a
common Device base class with a turnOn() method.

2. Create a derived class MultimediaDevice that inherits from


both AudioDevice and VideoDevice .

3. Attempt to call turnOn() using an object of MultimediaDevice and observe the


compiler error. Explain why this happens (the "Diamond Problem").

4. Resolve the ambiguity by using a scope resolution operator to specify which base
class's turnOn() you want to call.

Task 5: Hybrid inheritance with virtual base classes


Scenario: Resolve the diamond problem from Task 4 using virtual inheritance.

Instructions:

1. Recreate the class hierarchy from Task 4


( Device , AudioDevice , VideoDevice , MultimediaDevice ).

2. Use virtual public inheritance when


deriving AudioDevice and VideoDevice from Device .

3. Verify that creating a MultimediaDevice object now results in only one


shared Device sub-object.

4. In main() , call the turnOn() method on the MultimediaDevice object and confirm
that it compiles without ambiguity.

Task 6: Inheritance access modes


Scenario: Experiment with different inheritance access specifiers
( public , protected , private ).

Instructions:
1. Create a base class Parent with public , protected , and private members.

2. Create three different derived classes:

1. PublicChild that inherits publicly from Parent .

2. ProtectedChild that inherits protectedly from Parent .

3. PrivateChild that inherits privately from Parent .

3. In the main() function, create objects of each child class.

4. Write code and comments to explain which members can be accessed from
the main() function for each child type. Specifically, explain why a public member
of Parent might not be accessible from main() via a PrivateChild object.

You might also like