OOP Lab Manual 6
OOP Lab Manual 6
Inheritance
Objectives
In this lab students will learn,
The concepts of inheritance
Base and derived classes
Public inheritance
Private inheritance
Protected inheritance
Equipment required
Visual Studio/ Dev C++/Eclipse installed in Windows/PC
DISCUSSION
Inheritance enables reusability and helps in enhancing the functionality of any class.
Inheritance is one of the major pillars of Object-Oriented programming and aids in code
maintainability and avoids redundancy.
Inheritance
Inheritance is a form of software reuse in which you create a class that absorbs an existing
class’s data and behaviour and enhances them with new capabilities. In inheritance, a class
derives the behaviour and structure of another (existing) class.
Advantages
Saves time
Reuse of proven, debugged, high quality software
Object Oriented Programming Lab Prepared by: Engr. M. Farooq Khan
Data members in the base class are part of the derived class. Behaviours defined in the base
class are part of the derived class. Note that private aspects of the base class are part of the
child but are not (directly) accessible within the derived class.
LAB TASKS
Q1. Create a class hierarchy representing different types of vehicles in C++. The hierarchy
should include a parent class called "Vehicle" and at least two child classes representing
specific types of vehicles (e.g., "Car" and "Bicycle"). Each class should have:
1. Member variables (attributes) that are specific to that type of vehicle (e.g., "fuel_type",
”num_doors” for a Car, "num_wheels" for a Bicycle, and common attributes like "color"
and "brand" in the parent class).
3. Methods (functions) that can perform actions related to the specific vehicle type.
In the Car class, create a method "void startEngine()" that prints "Starting the
car's engine. Fuel type: {fuel_type}, Number of doors: {num_doors}, Color:
{color}, Brand: {brand}".
In the Bicycle class, create a method "void pedal()" that prints "Pedaling the
bicycle. Number of wheels: {num_wheels}, Color: {color}, Brand: {brand}".
Q2. Create a class hierarchy representing different types of animals in C++. The hierarchy
should include a parent class called "Animal" and at least three child classes representing
specific types of animals (e.g., "Dog," "Cat," and "Bird"). Each class should have:
1. Member variables (attributes) that are specific to that type of animal (e.g., "breed" for
a Dog, "fur_color" for a Cat, and common attributes like "name" and "age" in the parent
class).
3. Methods (functions) that can perform actions related to the specific animal type.
In the Dog class, create a method "void bark()" that prints "The dog named
{name} is barking. Breed: {breed}, Age: {age}".
In the Cat class, create a method "void meow()" that prints "The cat named
{name} is meowing. Fur color: {fur_color}, Age: {age}".
In the Bird class, create a method "void chirp()" that prints "The bird named
{name} is chirping. Age: {age}".