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

Unit 5 Inheritance

Inheritance allows classes to inherit properties and characteristics from other classes. New derived classes inherit from existing base classes and can modify properties as needed. There are different types of inheritance including single, multiple, multilevel, hierarchical, and hybrid inheritance. Derived classes can override methods from base classes. Constructors initialize classes from base to derived while destructors operate from derived to base.

Uploaded by

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

Unit 5 Inheritance

Inheritance allows classes to inherit properties and characteristics from other classes. New derived classes inherit from existing base classes and can modify properties as needed. There are different types of inheritance including single, multiple, multilevel, hierarchical, and hybrid inheritance. Derived classes can override methods from base classes. Constructors initialize classes from base to derived while destructors operate from derived to base.

Uploaded by

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

Unit -5 Inheritance

Intro
 Inheritance is the capability of class to derive properties and
characteristics from another class.
 It is one of the important feature of OOP
 New classes are created using the existing classes
 The new class created is called “derived” class or “child” class
 The existing class is known as “base” class or “parent” class
 Derived class inherits the base class along with modification of its
own
 For example: A Dog class can inherit Animal class. Here
Animal is base or parent class and Dog is child or derived
class.
 Based on number of base classes with respect to derived
classes there are following types of inheritance:
 Single inheritance
 Multiple inheritance
 Multilevel inheritance
 Hierarchical inheritance
 Hybrid inheritance
Defining Derived Classes
 A derived class can be defined by specifying its
relationship with the base class in addition to its own
implementation details.
 Syntax :
class derived_class: visibility_mode base_class
{
// data members and member functions of derived_class
}
 Examples
class Dog: private Animal{ private definition

// members of Dog
}

class Dog: public Animal{ Public


// members of Dog definition

}
visibility_modes
1)Public visibility means “public members” of base class becomes “public
members” of derived class and hence they are accessible to the objects of the
derived class
2)Private visibility means “public members” of base class becomes “private
members” of the derived class and hence they can be accessed only by the
member functions
3)Protected visibility means it can be accessed within class and from any class
derived from this class.

Summary:
 Private – visible to its own member function only
Protected – visible to member functions and derived class
Public – visible to all functions and objects
Overriding member function
 We can define data member and member
function with same name in both base and
derived class
 In this case, the function in derived class will be
executed which means derived class function
overrides the base class function
class Parent {
public:
void getData() { }

};

class Child : public Parent{


public:
void getData() { }

}
void main(){
Child c1;
c1.getData();

}
 When the statement obj.getData() gets
executed, the function getData() of the derived
class executed. This means the derived class
function override the base class function.
 The scope resolution operator (::) can be used
to access base class function through an object
of the derived class.
Types of inheritance
Single Inheritance
 The derived class with only one base class is
called single inheritance.
Parent Base
Class

Child Derived
Class
Fig : Single Inheritance
Single Inheritance with
public access Specifier.
Single Inheritance with
private access Specifier.
Multiple Inheritance
 The derived class with more than one base
class is called multiple inheritance.
Base Class Base Class Base Class Base Class
B1 B2 B3 Bn

D Derived Class
 Syntax:
class derived:visibility base1,visibilityb2,..
{
// members of derived
}
Multilevel Inheritance
 The derived class from base class flowing
through intermediate classes is called multilevel
inheritance.
A

C
Multilevel Inheritance
Example
Hierarchical inheritance
 If base class inherits to more than one derived
classes then it is called hierarchical inheritance.
A

B C D
Hybrid Inheritance
 The inheritance in which the derivation of a class
involves more than one form of any inheritance is
called hybrid inheritance.
 Basically in C++ hybrid inheritance is
combination of two or more types of inheritance.
 It can also be called multi path inheritance.
BaseClass

DerivedClass1 DerivedClass2

DerivedClass3

Hybrid Inheritance
 In hybrid inheritance there are two or more
types of inheritance.
 The attributes of base class into DerivedClass3
in the figure will inherit due to which
DerivedClass3 would have duplicate sets of
members
 So to avoid the problem we use DerivedClass1
and DerivedClass2 as virtual base class as
follows :
class DerivedClass1 : virtual public BaseClass{
// members
};

class DerivedClass2 : virtual public BaseClass{


// members
};
Abstract Classes

 It is the class that cannot be instantiated and is


designed to be used as base class for others
 It generally contains one or more pure virtual functions
 A pure virtual function is a virtual function that is
declared in the base class but has no implementation
Abstract Class Example
Ambiguity in multiple inheritance
 Ambiguity in C++ occurs when a derived class
have two base classes and these two base
classes have one common base class
A
(Base Class)

B C

D
The Problem:
 In the figure, both B and C inherit class A. They both
have copy of A
 Class D inherits both B and C, hence D has two
copies of class A (one from B and one from C)
 If we want to access data member of class A from D,
we need to specify the path from which data member
will be accessed(either from B or C).
 In this case compiler cant differentiate between two
copies of class A in Class D.
 There are two ways to avoid C++ ambiguity.
 One is using virtual base class as described earlier in
hybrid inheritance
 Another is using scope resolution operator
 To use scope resolution operator, we can specify
members as:
object.B::display();
object.B::x = 10;

Note: The two copies of class A in D still exists


Aggregation (nested class)
 It is a way of representing relationship between two
classes, where one class contains an object of other
class.
 It allows one class to have a member variable that is
instance of another class (the aggregated object)
 This relationship is often referred to as a “has-a”
relationship.
Aggregation Example:

In the example, a Person class has-a Car.

This shows the aggregation relationship


between the “Person” class and “Car”
class, where a person can have a car
(aggregated object) and the car does not
define the identity of the person.
Constructors in derived class
 A constructor is declared in order to initialize data members
 It is not possible to use single constructor on all the classes
 Every class has its own constructors and destructors
 It is possible to define constructor in derived class in order to
initialize the data members of both base and derived classes
 Thus constructor defined in derived class works for its base
class, such constructors are called constructors in the derived
class or common constructors.
Example 1:
Constructors in derived
class
Example 1:
Constructors in derived class

Here constructor of Horse and


Donkey
are called from derived class to
initialize their names.

The execution is from base to derived


class.
Destructors in derived class
 The execution of destructors are just opposite
of constructors I.e from derived to base class
 Hence the sequence is that the object created
last is destroyed first.
Destructor in derived class exampe.

The execution is from derived to base


class I.e opposite pattern of constructor
in derived class as seen before.

You might also like