0% found this document useful (0 votes)
19 views

OOPS Notes - Unit-5

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

OOPS Notes - Unit-5

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Course Name: Object Oriented Programming Systems (OOPS)

Course Code : CS/IT103

Credits: 4

Prepared by: Dr. Rakesh Chowdhury

Assistant Professor (EEE)


Email: [email protected]
UNIT- 5: INHERITANCE in OOPS
Inheritance >> Introduction

• In object-oriented programming (OOP), inheritance is a mechanism that allows a class to


inherit properties and behaviors from another class.
• It is a fundamental concept in OOP that promotes code reuse and establishes relationships
between classes.
Inheritance >> Introduction

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

Why and when to use inheritance?

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.

class — keyword to create a new class

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

Base_class_name — name of the base class


Inheritance >> Introduction

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

Modes of Inheritance: There are 3 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

// C++ Implementation to show that a derived class class C : protected A {


// doesn’t inherit access to private data members. // x is protected
// However, it does inherit a full parent object. // y is protected
class A { // z is not accessible from C
public: };
int x;
class D : private A // 'private' is default for classes
protected: {
int y; // x is private
// y is private
private: // z is not accessible from D
int z; };
};

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

class Car : public Vehicle {

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

2. 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 base class.

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; {
// first base class // Creating object of sub class will
class Vehicle { // invoke the constructor of base classes.
public: Car obj;
Vehicle() { cout << "This is a Vehicle\n"; } return 0;
}; }

// second base class


class FourWheeler { Output:
public: This is a Vehicle
FourWheeler() This is a 4 wheeler Vehicle
{
cout << "This is a 4 wheeler
Vehicle\n";
} The constructors of inherited classes are
}; called in the same order in which they are
// sub class derived from two base classes
inherited.
class Car : public Vehicle, public FourWheeler {
};
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; }
};

The destructors are called in reverse order of constructors.


Inheritance >> Multiple inheritance

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.

// Base class A int main() {


class A {
// Created an object of class C
public:
void func() { C obj;
cout << " I am in class A" << endl;
} // Calling function func()
}; obj.func();
// Base class B return 0;
class B { }
public:
void func() {
In this example, derived class C inherited the
cout << " I am in class B" << endl;
two base classes A and B having the same
}
function name func(). When the object of class
};
C is created and called the function func() then
// Derived class C
the compiler gets confused that which base
class C: public A, public B {
class member function func() should be called.
};
Inheritance >> inheritance Ambiguity

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();

// Calling function func() in class B


obj.B::func();

return 0;
}
Inheritance >> Multi-level Inheritance

3. Multilevel Inheritance: In this type of inheritance, a derived class is


created from another derived class.
Inheritance >> Multi-level Inheritance

#include <iostream> // main function


using namespace std; int main()
// base class {
class Vehicle { // Creating object of sub class will
public: // invoke the constructor of base classes.
Vehicle() { cout << "This is a Vehicle\n"; } Car obj;
}; return 0;
}
// first sub_class derived from class vehicle
class fourWheeler : public Vehicle {
public:
fourWheeler()
{
cout << "Objects with 4 wheels are
vehicles\n";}
};
// sub class derived from the derived base class fourWheeler
class Car : public fourWheeler {
public:
Car() { cout << "Car has 4 Wheels\n"; }
};
Inheritance >> Multi-level Inheritance
Inheritance >> Multi-level Inheritance
Inheritance >> Hierarchical Inheritance

4. Hierarchical Inheritance: In this type of 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.
Inheritance >> Hierarchical Inheritance

// C++ program to implement // main function


// Hierarchical Inheritance int main()
#include <iostream> {
using namespace std; // Creating object of sub class will
// invoke the constructor of base class.
// base class Car obj1;
class Vehicle { Bus obj2;
public: return 0;
Vehicle() { cout << "This is a Vehicle\n"; } }
};

// first sub class


class Car : public Vehicle {
};

// second sub class


class Bus : public Vehicle {
};
Inheritance >> Hierarchical Inheritance

5. Hybrid (Virtual) Inheritance: Hybrid Inheritance is implemented by combining more than


one type of inheritance. For example: Combining Hierarchical inheritance and Multiple
Inheritance.
Below image shows the combination of hierarchical and multiple inheritances:
Inheritance >> Hierarchical 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"; }
};

// first sub class


class Car : public Vehicle {
};

// second sub class


class Bus : public Vehicle, public Fare {
};

You might also like