You are on page 1of 16

Constructors and Destructors

November 2, 2022

Constructors and Destructors November 2, 2022 1 / 16


Outline

1 Access Specifiers and Simple Inheritance

2 Types of Inheritance

3 Single Inheritance

4 Multilevel Inheritance

5 Multiple Inheritance

6 Hybrid Inheritance

7 Hierarchical Inheritance

8 Multipath Inheritance

Constructors and Destructors November 2, 2022 2 / 16


Access Specifiers and Simple Inheritance

Access Specifiers and Simple Inheritance

The procedure of creating a new class from one or more existing


classes is termed inheritance.
Access Specifiers
public
private
protected:Protected access modifier is similar to that of private
access modifiers, the difference is that the class member declared as
Protected are inaccessible outside the class but they can be accessed
by any subclass(derived class) of that class.

Constructors and Destructors November 2, 2022 3 / 16


Access Specifiers and Simple Inheritance

syntax

derived class name: access specifiers baseclass name


{
// member variables of new class (derived class)
};
Example:
class B: public A
{
// Members of class B
};

Constructors and Destructors November 2, 2022 4 / 16


Access Specifiers and Simple Inheritance

syntax

class B: private A // private derivation


{
// members of class B
};
______________________________________________________________
class B: A // by default private derivation
{
// members of class B
};
______________________________________________________________
class B: protected A // same as private
{
// members of class B
};

Constructors and Destructors November 2, 2022 5 / 16


Access Specifiers and Simple Inheritance

Important Points

1 When a public access specifier is used (example 1), the public


members of the base class are public members of the derived class.
Similarly, the protected members of the base class are protected
members of the derived class.
2 When a private access specifier is used, the public and protected
members of the base class are the private members of the derived
class.
3 The use of protected access specifier is the same as private access.

Constructors and Destructors November 2, 2022 6 / 16


Access Specifiers and Simple Inheritance

Examples

Write a program to derive a class publicly from base class. Declare


the base class with its member under public section.
Write a program to derive a class publicly from base class. Declare
the base class member under private section.
Write a program to derive a class privately. Declare the member of
base class under public section.
Write a program to declare protected data in base class. Access data
of base class declared under Protected section using member
functions of derived class.

Constructors and Destructors November 2, 2022 7 / 16


Types of Inheritance

Types of Inheritance

The process of inheritance can be either simple or complex.


This depends on the following points:
Number of base classes: The program can use one or more base classes
to derive a single class.
Nested derivation: The derived class can be used as a base class, and a
new class can be derived from it.
Single Inheritance
Multiple Inheritance
Hierarchical Inheritance
Multilevel Inheritance
Hybrid Inheritance
Multi-path Inheritance

Constructors and Destructors November 2, 2022 8 / 16


Single Inheritance

Single Inheritance

Constructors and Destructors November 2, 2022 9 / 16


Multilevel Inheritance

Multilevel Inheritance

Constructors and Destructors November 2, 2022 10 / 16


Multiple Inheritance

Multiple Inheritance

Constructors and Destructors November 2, 2022 11 / 16


Hybrid Inheritance

Hybrid Inheritance

Constructors and Destructors November 2, 2022 12 / 16


Hierarchical Inheritance

Hierarchical Inheritance

Constructors and Destructors November 2, 2022 13 / 16


Multipath Inheritance

Multipath Inheritance(Virtual Base class)


Virtual base classes in C++ are used to prevent multiple instances of a
given class from appearing in an inheritance hierarchy when using multiple
inheritances.

Constructors and Destructors November 2, 2022 14 / 16


Multipath Inheritance

Problem without virtual


class A {
public:
void display() {
cout << "Hello form Class A \n";
}
};
class B: public A { };
class C: public A { };
class D: public B, public C { };
int main() {
D object;
object.display();
}
If a virtual base class is not used, all the derived classes will get duplicated
data members. In this case, the compiler cannot decide which one to
execute.
Constructors and Destructors November 2, 2022 15 / 16
Multipath Inheritance

How to Declare Virtual Base Class in C++?

The word “virtual” can be written before or after the word “public”.
class B: virtual public A {
// statement 1
};
class C: public virtual A {
// statement 2
};

Constructors and Destructors November 2, 2022 16 / 16

You might also like