OOPS Notes - Unit-5
OOPS Notes - Unit-5
Credits: 4
Inheritance is a feature or a process in which, new classes are created from the existing
classes. The new class created is called “derived class” or “child class” and the existing
class is known as the “base class” or “parent class”. The derived class now is said to be
inherited from the base class.
When we say derived class inherits the base class, it means, the derived class inherits all the
properties of the base class, without changing the properties of base class and may add new
features to its own. These new features in the derived class will not affect the base class. The
derived class is the specialized class for the base class.
•Sub Class: The class that inherits properties from another class is called Subclass or
Derived Class.
•Super Class: The class whose properties are inherited by a subclass is called Base Class or
Superclass.
Inheritance >> Introduction
Consider a group of vehicles. You need to create classes for Bus, Car, and Truck. The methods fuelAmount(),
capacity(), applyBrakes() will be the same for all 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 below figure:
You can clearly see that the above process results in duplication of the same code 3 times. This increases the
chances of error and data redundancy. To avoid this type of situation, inheritance is used.
Inheritance >> Introduction
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.
Using inheritance, we have to write the functions only one time instead of three times as we have
inherited the rest of the three classes from the base class (Vehicle).
Inheritance >> Introduction
For creating a sub-class that is inherited from the base class we have to follow the below syntax.
derived_class_name — name of the new class, which will inherit the base class
access-specifier — either of private, public or protected. If neither is specified, PRIVATE is taken as default
Protected: The protected access modifier is similar to the private access modifier in the
sense that it can’t be accessed outside of its class unless with the help of a friend class. The
difference is that the class members declared as Protected can be accessed by any
subclass (derived class) of that class as well.
Inheritance >> Implementation
// C++ program to demonstrate implementation // An object of class child has all data members
// of Inheritance // and member functions of class parent
obj1.id_c = 7;
#include <bits/stdc++.h> obj1.id_p = 91;
using namespace std; cout << "Child id is: " << obj1.id_c << '\n';
cout << "Parent id is: " << obj1.id_p << '\n';
// Base class
class Parent { return 0;
public: }
int id_p;
};
In the above program, the ‘Child’ class is
// Sub class inheriting from Base Class(Parent)
publicly inherited from the ‘Parent’ class so
class Child : public Parent {
public: the public data members of the class ‘Parent’
int id_c; will also be inherited by the class ‘Child’.
};
// main function
int main()
{
Child obj1;
Inheritance >> Modes of Inheritance
Inheritance >> Modes of Inheritance
1.Public Mode: If we derive a subclass 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 the derived class.
2.Protected Mode: If we derive a subclass from a Protected base class. Then both public
members and protected members of the base class will become protected in the derived class.
3.Private Mode: If we derive a subclass from a Private base class. Then both public members
and protected members of the base class will become Private in the derived class.
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 the next example. It is just a question of access.
Inheritance >> Modes of Inheritance >> Implementation
class B : public A {
// x is public
// y is protected
// z is not accessible from B
};
Output?
AB
Output?
A4B5
Inheritance >> Single Inheritance
#include<iostream>
using namespace std;
class Vehicle {
public:
Vehicle()
{
cout << "This is a Vehicle\n";
}
};
};
1. Single Inheritance: In single
inheritance, a class is allowed to inherit int main()
from only one class. i.e. one subclass is {
Car obj;
inherited by one base class only return 0;
}
Output: This is a Vehicle
Inheritance >> Multiple Inheritance
Here, the number of base classes will be separated by a comma (‘, ‘) and the access mode
for every base class must be specified.
Inheritance >> Multiple Inheritance
#include<iostream>
int main()
using namespace std;
{
class A C c;
{ return 0;
public: }
A() { cout << "A's constructor called" << endl; }
};
class B
{
public:
B() { cout << "B's constructor called" << endl; }
};
class C: public B, public A // Note the order
{
public:
C( ) { cout << "C's constructor called" << endl; }
};
Output?
CAB
Inheritance >> Multiple inheritance
Output?
A5C2B9
Inheritance >> Multiple inheritance
Inheritance >> inheritance Ambiguity
In multiple inheritances, when one class is derived from two or more base classes then there may be a possibility that the
base classes have functions with the same name, and the derived class may not have functions with that name as those of its
base classes. If the derived class object needs to access one of the similarly named member functions of the base classes
then it results in ambiguity because the compiler gets confused about which base’s class member function should be called.
Solution to Ambiguity:
To solve this ambiguity scope resolution operator is used denoted by ‘ :: ‘
Syntax:
int main() {
// Created an object of class C
C obj;
// Calling function func() in class A
obj.A::func();
return 0;
}
Inheritance >> Multi-level Inheritance
#include <iostream>
using namespace std; // main function
int main()
// base class {
class Vehicle { // Creating object of sub class will
public: // invoke the constructor of base class.
Vehicle() { cout << "This is a Vehicle\n"; } Bus obj2;
}; return 0;
}
// base class
class Fare {
public:
Fare() { cout << "Fare of Vehicle\n"; }
};