Lab - 07
Lab - 07
Contents:
1. Introduction to Inheritance
2. Types of Inheritance based on Base Class Access Control
3. Types of Inheritance based on Derived Classes
4. UML Representation for Inheritance
5. Lab Tasks
INTRODUCTION TO INHERITANCE
Inheritance is one of the key features of Object-oriented programming in C++. It allows
us to create a new class (derived class) from an existing class (base class).
Base Class:
A base class is the class from which features are to be inherited into another
class.
Derived Class:
A derived class is the one which inherits features from the base class. It can
have additional properties and methods that are not present in the parent class
that distinguishes it and provides additional functionality.
1 OF 15
CL217 - Object Oriented Programming Lab Lab Manual - 07
Public Inheritance:
With public inheritance, every object of a derived class is also an object of that
derived class’s base class. However, base class objects are not objects of their
derived classes.
Is – A Relationship:
Inheritance is represented by an is-a relationship which means that an object of
a derived class also can be treated as an object of its base class for example, a
Car is a Vehicle, so any attributes and behaviors of a Vehicle are also attributes
and behaviors of a Car.
Class (name of the derived class) : public (name of the base class)
Class Car : public Vehicle
2 OF 15
CL217 - Object Oriented Programming Lab Lab Manual - 07
Single Inheritance:
In this type of inheritance there is one base class and one derived class.
As shown in the figure below, in single inheritance only one class can be
derived from the base class. Based on the visibility mode used or access
specifier used while deriving, the properties of the base class are derived.
3 OF 15
CL217 - Object Oriented Programming Lab Lab Manual - 07
int main()
{
derive a; //object of derived class
a.getdata();
a.readdata();
4 OF 15
CL217 - Object Oriented Programming Lab Lab Manual - 07
a.product();
return 0;
} //end of program
Sample Run
Enter the value of x = 3
Enter the value of y = 4
Product = 12
Multiple Inheritance:
In multiple inheritance, a class is derived from two or more base classes. In
multiple inheritance a derived class has more than one base class.
As shown in the figure below, class C is derived from two base classes A and
B.
5 OF 15
CL217 - Object Oriented Programming Lab Lab Manual - 07
#include
6 OF 15
CL217 - Object Oriented Programming Lab Lab Manual - 07
Sample Run
enter value of x: 5
enter value of y: 4
Sum = 9
Multilevel Inheritance:
If a class is derived from another derived class then it is called multilevel
inheritance, so in multilevel inheritance, a class has more than one parent class.
As shown in the figure below, class C has class B and class A as parent classes.
As in other inheritance, based on the visibility mode used or access specifier
used. while deriving, the properties of the base class are derived. Access
specifier can be private, protected or public.
7 OF 15
CL217 - Object Oriented Programming Lab Lab Manual - 07
8 OF 15
CL217 - Object Oriented Programming Lab Lab Manual - 07
{
cout << "Enter value of x= "; cin >> x;
}
};
class derive1 : public base // derived class from base class
{
public:
int y;
void readdata()
{
cout << "\nEnter value of y= "; cin >> y;
}
};
class derive2 : public derive1 // derived from class derive1
{
private:
int z;
public:
void indata()
{
cout << "\nEnter value of z= "; cin >> z;
}
void product()
{
cout << "\nProduct= " << x * y * z;
}
};
int main()
{
derive2 a; //object of derived class
a.getdata();
a.readdata();
a.indata();
a.product();
return 0;
} //end of program
Sample Run
Enter value of x= 2
Enter value of y= 3
Enter value of z= 3
Product= 18
Hierarchical Inheritance:
When several classes are derived from a common base class it is called as
hierarchical inheritance.
9 OF 15
CL217 - Object Oriented Programming Lab Lab Manual - 07
In C++ hierarchical inheritance, the feature of the base class is inherited onto
more than one sub-class.
For example, a car is a common class from which Audi, Ferrari, Maruti etc can
be derived.
As shown in the figure below, in C++ hierarchical inheritance all the derived
classes have a common base class. The base class includes all the features
that are common to derived classes.
10 OF 15
CL217 - Object Oriented Programming Lab Lab Manual - 07
{
public:
int x, y;
void getdata()
{
cout << "\n Enter value of x and y:\n"; cin >> x >> y;
}
};
class B : public A //B is derived from class base
{
public:
void product()
{
cout << "\n Product= " << x * y;
}
};
class C : public A //C is also derived from class base
{
public:
void sum()
{
cout << "\n Sum= " << x + y;
}
};
int main()
{
B obj1; //object of derived class B
C obj2; //object of derived class C
obj1.getdata();
obj1.product();
obj2.getdata();
obj2.sum();
return 0;
} //end of program
Sample Run
Enter value of x and y:
2
3
Product= 6
Enter value of x and y:
2
3
Sum= 5
Hybrid Inheritance:
The inheritance in which the derivation of a class involves more than one
form of any inheritance is called hybrid inheritance.
11 OF 15
CL217 - Object Oriented Programming Lab Lab Manual - 07
Syntax for
hybrid Inheritance:
C class A // base class
{
X // body of the class
};
class B : public A
{ {
X // body of the class
}; };
class C
{ {
// body of the class
};
} ; class D : public B, public C
{ {
// body of the class
};
class A
{
public:
int x;
};
class B : public A
12 OF 15
CL217 - Object Oriented Programming Lab Lab Manual - 07
{
public:
B() //constructor to initialize x in base class A
{
x = 10;
}
};
class C
{
public:
int y;
C() //constructor to initialize y
{
y = 4;
}
};
class D : public B, public C //D is derived from class B and class C
{
public:
void sum()
{
cout << "Sum= " << x + y;
}
};
int main()
{
D obj1; //object of derived class D
obj1.sum();
return 0;
} //end of program
Sample Run
Sum= 14
13 OF 15
CL217 - Object Oriented Programming Lab Lab Manual - 07
BaseClass is referred
to as the base class and DerivedClassOne is referred to as the derived class or
BaseClass contains two private attributes. One is a static, class-wide variable named
count, and the other is an integer variable named its_count. BaseClass contains four
public functions and one protected function.
14 OF 15
CL217 - Object Oriented Programming Lab Lab Manual - 07
LAB TASKS:
Task - 01:
A school library wants to organize its library system by categorizing books according to
their genre. They need an automated system that will allow them to input the details of
the books that are in their library. To do this, you need to implement a program that
contains a base class called Books that will contain a data member to store the genre
of the book. Derive two other classes from the base class and name them accordingly.
Each of these two classes will hold details about a book from a specific genre of your
choice such as Novel, Narrative, Mystery and so on. The derived class will contain data
members to store the title and the author of the book. Display the details of each book
along with their genre.
Task - 02:
A vehicle company is deciding to hire a programmer to develop a system that will allow
the company to enter the details of the vehicles sold by them. As a programmer, you
need to implement a program that contains a base class called Vehicles that contains
a data member to store the price of the vehicles. Derive two other classes named as
Car and Motorcycle.
The Car class will contain data members to store details that include seating
capacity, number of doors and fuel type (diesel or petrol).
The Motorcycle class will contain data members to store details such as the
number of cylinders, the number of gears and the number of wheels.
Derive another subclass named as Audi of Car and Yamaha of Motorcycle.
The Audi class will contain a data member to store the model type.
The Yamaha class will contain a data member to store the make – type.
Display the details of an Audi car (price, seating capacity, number of doors, fuel type,
model type) and the details of the Yamaha motorcycle (price, number of cylinders,
number of gears, number of wheels, make – type).
Task - 03:
A university is deciding to upgrade its system. In order to upgrade, you need to
implement the following scenario as shown in the figure:
Note the following:
The class student has a function that displays all the information about the
student.
Class marks is derived from class student and has a function that displays all the
marks obtained in the courses by the students.
Class result is derived from class marks. This class has a function that calculates
the total marks and then calculates the average marks. It then displays both the
total and the average marks.
In the main function you are required to do the following:
Create an object of the result class.
Then display the student details, the marks obtained in each courses and
the total and the average marks.
15 OF 15
CL217 - Object Oriented Programming Lab Lab Manual - 07
C class student{
int id;
string name;
public:
void
getstudentdetails(){
}
};
16 OF 15