Lab Exercise on Polymorphism
1. Create a base class Shape with a virtual function area(). Derive classes Rectangle, Circle, and Triangle that override
the area() function to calculate and return the area of each shape. Write a program that creates objects for each shape
and calculates their area using runtime polymorphism.
2. Create a base class Employee with a virtual function calculateSalary(). Derive two classes: PermanentEmployee and
ContractEmployee. Each class should have different formulas to calculate the salary based on base salary and
additional factors such as benefits for permanent employees and hours worked for contract employees.
● PermanentEmployee: salary = base salary + benefits
● ContractEmployee: salary = hourly wage * hours worked
3. Create an abstract base class Animal with a pure virtual function makeSound(). Derive classes Dog, Cat, and Bird
from Animal. Each derived class should provide its own implementation of the makeSound() function (e.g., "Bark",
"Meow", "Chirp"). Write a program that creates objects of Dog, Cat, and Bird, and demonstrates polymorphism using
the makeSound() function.
4. Create an abstract base class Shape with a pure virtual function calculateArea(). Derive classes Rectangle, Circle, and
Triangle from Shape. Each derived class should override the calculateArea() function and implement it based on the
geometry of the shape.
5. Create a class Person as a base class. Derive two classes Employee and Student from Person. Then, derive a class
PartTimeEmployee from both Employee and Student. Implement constructors in each class that display a message
when an object is created.
● Without virtual inheritance, you’ll observe that the Person constructor is called twice (due to the diamond
problem).
● Modify the inheritance to use virtual base classes to ensure the Person constructor is called only once when
creating a PartTimeEmployee.
6. Create a class Person with attributes like name and age. Create two derived classes Faculty and Student, both
inheriting from Person. Further, create a class TA (Teaching Assistant) that inherits from both Faculty and Student.
Use virtual inheritance to ensure the attributes name and age are inherited only once.
● Person: name, age
● Faculty: department
● Student: roll number
● TA: working hours, courses
7. Create a class Rectangle with private members length and breadth. Implement the following methods:
● A constructor that initializes the length and breadth using the this pointer.
● A method setDimensions() that accepts parameters with the same names as the class members (length and
breadth), and uses this pointer to differentiate between the member variables and the parameters.
● A method displayArea() that calculates and displays the area of the rectangle.
8. Create a class ObjectCounter with a static data member count that tracks the number of objects created. The class
should have a static member function getCount() to return the current count. Implement the following steps:
● Declare a static data member count.
● In the constructor, increment the count every time an object is created.
● Implement a static member function getCount() to return the current count.
● In the main() function, create multiple objects and display the count using the static function.