Lab Manual 07 -Inheritance and its Types
Lab Manual 07 -Inheritance and its Types
Object-Oriented Programming (OOP) strongly supports reusability and in this regard, inheritance
is perhaps the strongest way of reusing the features of a class i.e. attributes (data member) and
behaviors (functions). Following the OOP paradigm, C++ allows the inheritance among classes
so that the characteristic(s) of one class is/are inherited by the other class(es). Hence, you can say
that the derived classes receive some their attributes from which the derived class is inherited. A
major advantage of inheritance is that it allows the reusability of code. Hence, the programmer
can simply create new (child) class(es) by using other (parent) class(es).
While considering the advantages of commonality between classes, in C++, you are able to
manage your classes in a way that a class (child/sub class) can extend the functionality by
inheriting the attributes and behaviors of an existing class commonly known as base/super class.
In simple words, you can define a class with certain data fields and/or member functions and
then identify other class(es) that can share these data fields and functions. In typical C++
inheritance, a class or classes known as derived class(es), subclass(es) or child class(es) is/are
able to inherit certain attributes and behavior of pre-existing class(es) that are known as base
class(es), superclass(es), or parent class(es). In practice, base classes are more general while
derived classes are specialized version of base classes and due to this reason, it is said that
inheritance maintains generalization and specialization. With inheritance, you can greatly
save the time and lessen the effort to write duplicate code.
Concerning inheritance syntax in C++, if you have a base class named person inherited by a
derived class named student then you have to write code in the following way.
In case of derived class’s object instantiation, default/no-argument constructor for the base
class(es) are automatically called first and are followed by calling derived class’s constructors.
If base class(es) is/are without default/no-argument constructor, then it is must to call explicitly
any base class’s constructor even if there is no need to call a constructor.
Objective of the Experiment
After completing this lab, the student should be able to:
get basic understanding of inheritance concept in OOP
derive class(es) from other base class(es) through inheritance
understand the constructor and destructor chaining in inheritance hierarchy
know how to invoke the base class’s constructors within derived class(es)
Concept Map
Inheritance is one of the most important building blocks of OOP. The concept of reusable classes
is helpful to manage objects. You can create new classes (derived classes) that are able to inherit
certain attributes and behavior of their ancestor or base class(es). Prior to working with
inheritance, it is important to consider the following aspects of inheritance.
Inheritance represents “is-a” relationship in OOP i.e. any specific object is a type of a
more general class of object. For example, Train is a type of Vehicle
By design, a derived class should inherit all attributes and behaviors of its base class(es)
While declaring its own data fields a derived class can extend the features of its base
class(es)
While defining new functionality, a derived class can modify the behavior provided by
the base class(es)
Inheritance in OOP saves a lot of work and time. You can save additional work as some of the
object definitions(class) already exists. Time saving is due to the reason that much of the code
has already been written and tested.
If a class B inherits from class A then it contains all the characteristics (information structure and behavior
of class A.
The parent class is called base class and the child class is called derived class Besides inherited
characteristics, derived class may have its own unique characteristics
Examples:
Here,
Student IS A Person
Teacher IS A Person
Doctor IS A Person
Here,
Circle IS A Shape
Line IS A Shape
Triangle IS A Shape
Generalization
In OO models, some classes may have common characteristics. We extract these features into a
new class and inherit original classes from this new class. There are many objects with common
characteristics in object model. The common characteristics (attributes and behaviour) of all
these objects are combined in a single general class. Base class encapsulates the idea of
commonality of derived classes. Base class is general class representing common behaviour of
all derived classes.
This concept is known as Generalization.
It reduces the redundancy and gives us reusability, using generalization our solution becomes less
complex.
In generalization there should be “Is a Kind of Relationship” (also called “Is A relationship”)
between base and child classes.
Examples: Line,Circle,Triangle
Common attributes
Color vertices
Common behaviour
Set Color, Move
Example: Student, Doctor and Teacher
Sub-typing (Extension)
Sub-typing means that derived class is behaviorally compatible with the base class Derived class has all the
characteristics of base class plus some extra characteristics Behaviorally compatible means that base class can be
replaced by the derived class.
Note: Every object of derived class has an anonymous object of base class
Different Types of Inheritance
Single Inheritance:
In single inheritance, a class is allowed to inherit from only one
class. i.e. one base class is inherited by one derived class only.
Example:
#include <iostream>
using namespace std;
class Vehicle {
public:
Vehicle() {
cout << "This is a Vehicle"<< endl;
}
};
int main() {
Output
This is a Vehicle
This is a Car
Multiple Inheritance:
#include <iostream>
using namespace std;
class LandVehicle {
public:
LandVehicle() {
cout << "This is a LandVehicle"<< endl;
}
};
class WaterVehicle {
public:
WaterVehicle() {
cout << "This is a WaterVehicle"<< endl;
}
};
int main() {
This is a WaterVehicle
This is a LandVehicle
This is a AmphibiousVehicle
Multilevel Inheritance:
In multilevel inheritance , a derived class is created from another
derived class and that derived class can be derived from a base class
or any other derived class. There can be any number of levels. For
example, a vehicle can be a four-wheeler, and a four-wheeler vehicle
can be a car.
Example:
#include <iostream>
class Vehicle {
public:
Vehicle() {
};
public:
fourWheeler() {
};
public:
Car() {
};
int main() {
return 0;
Output
This is a Vehicle
4 Wheeler Vehicles
Hierarchical Inheritance:
#include <iostream>
class Vehicle {
public:
Vehicle() {
};
public:
Car() {
};
public:
Bus() {
};
int main() {
Car obj1;
Bus obj2;
return 0;
Output
This is a Vehicle
This Vehicle is Car
This is a Vehicle
This Vehicle is Bus
Hybrid Inheritance:
Hybrid Inheritance is implemented by combining more than one type of
inheritance. For example: Combining Hierarchical inheritance and Multiple
Inheritance will create hybrid inheritance in C++.
There is no particular syntax of hybrid inheritance. We can just
combine two of the above inheritance types. Below image shows one of
the combinations of hierarchical and multiple inheritances:
Example:
#include <iostream>
using namespace std;
class Vehicle {
public:
Vehicle() {
cout << "This is a Vehicle"<< endl;
}
};
class Fare {
public:
Fare() {
cout << "Fare of Vehicle"<< endl;
}
};
int main() {
Output
This is a Vehicle
Fare of Vehicle
This Vehicle is a Bus with Fare
Practice Tasks: [CLO 2, 3, 4]
This section will provide more practice exercises which you need to finish during the lab.
You need to finish the tasks in the required time.
Practice Task 1:
Consider a base class named Employee and its derived classes HourlyEmployee and
PermanentEmployee while taking into account the following criteria.
Employee class has two data fields i.e. a name (of type string) and specific
empID (of type integer)
Both classes (HourlyEmployee and PermanentEmployee) have an attribute
named hourlyIncome
Both classes (HourlyEmployee and PermanentEmployee) have three-argument
constructor to initialize the hourlyIncome as well as data fields of the base class
Class HourlyEmployee has a function named calculate_the_hourly_income to
calculate the income of an employee for the actual number of hours he or she
worked. One hour income is Rs. 150
Similarly, PermanentEmployee class has function named calculate_the_income
to calculate the income of an employee that gets paid the salary for exact 240
hours, no matter how many actual hours he or she worked. Again, one hour
salary is Rs. 150.
Implement all class definitions with their respective constructors to initialize all data
members and functions to compute the total income of an employee. In the main()
function, create an instance of both classes (i.e. HourlyEmployee and
PermanentEmployee) and test the working of functions that calculate total income of
an employee.
Derive two classes from the BankAccount class i.e. CurrentAccount and the
SavingsAccount. Both classes (CurrentAccount and SavingsAccount) inherit all
attributes/behaviors from the BankAccount class. In addition, followings are
required to be the part of both classes