0% found this document useful (0 votes)
8 views19 pages

Lab Manual 07 -Inheritance and its Types

The document provides an overview of Object-Oriented Programming (OOP) with a focus on inheritance in C++. It explains the concepts of base and derived classes, the advantages of code reusability, and various types of inheritance such as single, multiple, multilevel, hierarchical, and hybrid inheritance. Additionally, it includes practical tasks for students to implement class hierarchies and functions related to employee and bank account management.

Uploaded by

saimrajput7935
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)
8 views19 pages

Lab Manual 07 -Inheritance and its Types

The document provides an overview of Object-Oriented Programming (OOP) with a focus on inheritance in C++. It explains the concepts of base and derived classes, the advantages of code reusability, and various types of inheritance such as single, multiple, multilevel, hierarchical, and hybrid inheritance. Additionally, it includes practical tasks for students to implement class hierarchies and functions related to employee and bank account management.

Uploaded by

saimrajput7935
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
You are on page 1/ 19

Introduction [CLO 1]

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:

Inheritance – “IS A” or “IS A KIND OF” Relationship

Each derived class is a kind of its base class

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

Common attributes, Common behaviour


Name, age, gender Eat, Walk
Sub-typing & Specialization
We want to add a new class to an existing model. We have developed an existing class hierarchy Find an existing class that
already implements some of the desired state and behavior Inherit the new class from this class and add unique behavior to
the new Class.

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.

Sub-typing (Extension) – Example

1. Circle is extending the behavior of shape, it is extending attributes of shape by adding


radius similarly it is extending behavior of shape by adding compute Circumference
and compute Area.
2. Student has two extra attributes program and studyYear Similarly it has extended
behavior by adding study and takeExam.

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;
}
};

// Sub class derived from a single base classes


class Car : public Vehicle {
public:
Car() {
cout << "This Vehicle is Car"<< endl;
}
};

int main() {

// Creating object of sub class will


// invoke the constructor of base classes
Car obj;
return 0;
}

Output
This is a Vehicle
This is a Car

Multiple Inheritance:

Multiple Inheritance is a feature of C++ where a class can inherit


from more than one class. i.e one subclass is inherited from more
than
one bas e class.
Example:

#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;
}
};

// sub class derived from two base classes


class AmphibiousVehicle : public WaterVehicle, public LandVehicle {
public:
AmphibiousVehicle() {
cout << "This is an AmphibiousVehicle"<< endl;
}
};

int main() {

// Creating object of sub class will


// invoke the constructor of base classes.
AmphibiousVehicle obj;
return 0;
}
Output

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>

using namespace std;

class Vehicle {

public:
Vehicle() {

cout << "This is a Vehicle"<< endl;

};

class fourWheeler : public Vehicle {

public:

fourWheeler() {

cout << "4 Wheeler Vehicles"<< endl;

};

class Car : public fourWheeler {

public:

Car() {

cout << "This 4 Wheeler Vehicle is a Car";

};

int main() {

// Creating object of sub class will

// invoke the constructor of base classes.


Car obj;

return 0;

Output

This is a Vehicle

4 Wheeler Vehicles

This 4 Wheeler Vehicle is a Car

Hierarchical Inheritance:

In hierarchical inheritance , more than one subclass is inherited


from a single base class. i.e. more than one derived class is created
from a single base class. For example, cars and buses both are vehicle.
Example:

#include <iostream>

using namespace std;

class Vehicle {

public:

Vehicle() {

cout << "This is a Vehicle"<< endl;

};

class Car : public Vehicle {

public:

Car() {

cout << "This Vehicle is Car"<< endl;

};

class Bus : public Vehicle {

public:

Bus() {

cout << "This Vehicle is Bus"<< endl;

};
int main() {

// Creating object of sub class will

// invoke the constructor of base class.

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;
}
};

class Car : public Vehicle {


public:
Car() {
cout << "This Vehical is a Car"<< endl;
}
};
class Bus : public Vehicle, public Fare {
public:
Bus() {
cout << "This Vehicle is a Bus with Fare";
}
};

int main() {

// Creating object of sub class will


// invoke the constructor of base class.
Bus obj2;
return 0;
}

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.

Practical Task 02 [CLO 2, 3, 4]


Consider a class BankAccount that has
 Two attributes i.e. accountID and balance and
 A function named balanceInquiry() to get information about the current
amount in the account

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

Appropriate constructors to initialize data fields of base class


 A function named amountWithdrawn(amount) to withdraw certain amount
while taken into account the following conditions
 While withdrawing from current account, the minimum balance should not
decrease Rs. 5000
 While withdrawing from savings account, the minimum balance should not
decrease Rs. 10,000
 amountDeposit(amount) to deposit amount in the account

In the main() function, create instances of derived classes (i.e.


CurrentAccount and SavingsAccount) and invoke their respective
functions to test their working.

You might also like