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

OOP Lab Manual 6

The document discusses inheritance in object-oriented programming. It defines inheritance and its advantages. It provides examples of creating class hierarchies for vehicles and animals with base and derived classes. Methods are defined to perform actions specific to each class.

Uploaded by

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

OOP Lab Manual 6

The document discusses inheritance in object-oriented programming. It defines inheritance and its advantages. It provides examples of creating class hierarchies for vehicles and animals with base and derived classes. Methods are defined to perform actions specific to each class.

Uploaded by

Tabassum Raza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

EXPERIMENT NO 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.

Class DerivedClass : kind BaseClass

Where kind is one of public, private, or protected.

Order of Constructors & Destructors


When a program creates a derived-class object, the derived-class constructor immediately
calls the base-class constructor; the base-class constructor’s body executes, then the derived-
class’s member initializers execute and finally the derived-class constructor’s body executes.
This process cascades up the hierarchy if it contains more than two levels.
When a derived-class object is destroyed, the program calls that object’s destructor. This
begins a chain (cascade) of destructor calls in which the derived-class destructor and the base
destructors execute in reverse of the order in which the constructors executed.
Object Oriented Programming Lab Prepared by: Engr. M. Farooq Khan

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).

2. Constructors for each class, including a default constructor and a parameterized


constructor that allows setting the attributes when an object is created.

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).

2. Constructors for each class, including a default constructor and a parameterized


constructor that allows setting the attributes when an object is created.

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}".

You might also like