inherit 2
inherit 2
Week 7
Lecture 1 & 2
Inheritance:
One of the most important concepts in object-oriented programming is that
of inheritance. Inheritance allows us to define a class in terms of another class, which makes it
easier to create and maintain an application. This also provides an opportunity to reuse the code
functionality and fast implementation time.
Inheritance is a mechanism in which one class acquires the property of another class. For
example, a child inherits the traits of his/her parents. With inheritance, we can reuse the fields
and methods of the existing class. Hence, inheritance facilitates Reusability and is an important
concept of OOPs.
The capability of a class to derive properties and characteristics from another class is
called Inheritance. Inheritance is one of the most important features of Object Oriented
Programming.
Sub Class/Derive Class: The class that inherits properties from another class is called Sub
class or Derived Class.
Super Class/Base Class: The class whose properties are inherited by sub class is called
Base Class or Super class.
Consider a group of vehicles. You need to create classes for Bus, Car and Truck. The methods
fuelAmount(), capacity(), applyBrakes() will be same for all of the three classes. If we create
these classes avoiding inheritance then we have to write all of these functions in each of the
three classes as shown in below figure:
Page 1 of 15
Object Oriented Programming
You can clearly see that above process results in duplication of same code 3 times. This
increases the chances of error and data redundancy. To avoid this type of situation, inheritance
is used. If we create a class Vehicle and write these three functions in it and inherit the rest of
the classes from the vehicle class, then we can simply avoid the duplication of data and
increase re-usability. Look at the below diagram in which the three classes are inherited from
vehicle class:
Implementing inheritance in C++: For creating a sub-class which is inherited from the
base class we have to follow the below syntax.
Syntax:
class subclass_name : access_mode base_class_name
Page 2 of 15
Object Oriented Programming
//body of subclass
};
Here, subclass_name is the name of the sub class, access_mode is the mode in which you want
to inherit this sub class for example: public, private etc. and base_class_name is the name of
the base class from which you want to inherit the sub class.
Note: A derived class doesn’t inherit access to private data members. However, it does inherit
a full parent object, which contains any private members which that class declares.
// of Inheritance
#include <bits/stdc++.h>
//Base class
class Parent
public:
int id_p;
};
Page 3 of 15
Object Oriented Programming
public:
int id_c;
};
//main function
int main()
Child obj1;
obj1.id_c = 7;
obj1.id_p = 91;
return 0;
Output:
Child id is 7
Parent id is 91
Page 4 of 15
Object Oriented Programming
In the above program the ‘Child’ class is publicly inherited from the ‘Parent’ class so the
public data members of the class ‘Parent’ will also be inherited by the class ‘Child’.
Modes of Inheritance:
1. Public mode: If we derive a sub class from a public base class. Then the public member of
the base class will become public in the derived class and protected members of the base
class will become protected in derived class.
2. Protected mode: If we derive a sub class from a Protected base class. Then both public
member and protected members of the base class will become protected in derived class.
3. Private mode: If we derive a sub class from a Private base class. Then both public member
and protected members of the base class will become Private in derived class.
Note : The private members in the base class cannot be directly accessed in the derived class,
while protected members can be directly accessed. For example, Classes B, C and D all contain
the variables x, y and z in below example. It is just question of access.
class A
public:
int x;
Page 5 of 15
Object Oriented Programming
protected:
int y;
private:
int z;
};
class B : public A
// x is public
// y is protected
};
class C : protected A
Page 6 of 15
Object Oriented Programming
// x is protected
// y is protected
};
// x is private
// y is private
};
The below table summarizes the above three modes and shows the access specifier of the
members of base class in the sub class when derived in public, protected and private modes:
Page 7 of 15
Object Oriented Programming
Single Inheritance: In single inheritance, a class is allowed to inherit from only one class.
i.e. one sub class is inherited by one base class only.
Syntax:
class subclass_name : access_mode base_class
{
//body of subclass
};
// Single inheritance
#include <iostream>
Page 8 of 15
Object Oriented Programming
// base class
class Vehicle {
public:
Vehicle()
};
};
// main function
int main()
Page 9 of 15
Object Oriented Programming
Car obj;
return 0;
Output:
This is a vehicle
Multiple Inheritance: Multiple Inheritance is a feature of C++ where a class can inherit
from more than one classes. i.e one sub class is inherited from more than one base classes.
Syntax:
class subclass_name : access_mode base_class1, access_mode base_class2, ....
//body of subclass
};
Here, the number of base classes will be separated by a comma (‘, ‘) and access mode for every
base class must be specified.
Page 10 of 15
Object Oriented Programming
// multiple inheritance
#include <iostream>
class Vehicle {
public:
Vehicle()
};
class FourWheeler {
public:
FourWheeler()
Page 11 of 15
Object Oriented Programming
};
};
// main function
int main()
Car obj;
return 0;
Output:
This is a Vehicle
Page 12 of 15
Object Oriented Programming
1.
// Multilevel Inheritance
#include <iostream>
// base class
class Vehicle
Page 13 of 15
Object Oriented Programming
public:
Vehicle()
};
{ public:
fourWheeler()
};
public:
car()
Page 14 of 15
Object Oriented Programming
};
// main function
int main()
Car obj;
return 0;
Output:
This is a Vehicle
Objects with 4 wheels are vehicles
Car has 4 Wheels
Page 15 of 15