Inheritance : Extending Classes
Hello people, after knowing that C++ is an Object Oriented
Programming Language, we must read about the features of C++ which actually helps
it to fall in this category and Inheritance is one of such type. It is actually
the most important feature of C++ language. We really need to have knowledge
regarding this topic if we actually want to work on C++. So, here we go.
In C++, inheritance is a process in which one object acquires all
the properties and behaviours of its parent object automatically. In such way,
we can reuse, extend or modify the attributes and behaviours which are defined
in other class.
In C++, the class which inherits the members of another class is
called derived class and the class whose members are inherited is called base
class. The derived class is the specialized class for the base class.
Well, Inheritance is a very important, highlighting and
useful feature of C++. By this we reuse the members of our parent class. There
is no need to define member again. Hence, no need of writing the code again.
Derived
Classes
A Derived Class is known as the class derived from some bass/parent
class. We can define a derived class in the following manner :
The colon here indicates that DerivedClassName is derived from BaseClassName. The visibility is optional, we can have private or public visibility modes. By default the visibility is Private.
When the
base class is privately inherited by the derived class, public members of the
base class becomes the private members of the derived class. Therefore, the
public members of the base class are not accessible by the objects of the
derived class only by the member functions of the derived class.
And, When
the base class is publicly inherited by the derived class, public members of
the base class also become the public members of the derived class. Therefore,
the public members of the base class are accessible by the objects of the
derived class as well as by the member functions of the base class.
The
private members of the base class are never inherited.
Types of
Inheritances
C++ supports five types of inheritances :
1. Single
Inheritance
2. Multiple Inheritance
3. Hierarchical
Inheritance
4. Multilevel
Inheritance
5. Hybrid
Inheritance
SINGLE
INHERITANCE
Single Inheritance is defined as an inheritance in which a derived class is only inherited from a single base class. Let us a look at an example of this type :
OUTPUT :
a = 10
c = 200
Making
Private Members inheritable
The private member is not inheritable. We can make it
inheritable, if we modify the visibility mode by making it public, but this
takes away the advantage of data hiding.
C++ introduces a third
visibility modifier, i.e., protected.
The member which is declared as protected will be accessible to all the member
functions within the class as well as the class immediately derived from it.
Visibility modes can be classified into three
categories:
Public: When the member is declared as
public, it is accessible to all the functions of the program.
Private: When the member is declared as private, it is
accessible within the class only.
Protected:
When the member is declared as protected, it is accessible within its own class
as well as the class immediately derived from it.
MULTILEVEL INHERITANCE
Multilevel
inheritance is a process of deriving a class from another derived
class.
When one class inherits another class
which is further inherited by another class, it is known as multi level
inheritance in C++. Inheritance is transitive so the last derived class
acquires all the members of all its base classes.
Let's see the example of multilevel inheritance in C++.
OUTPUT
:
Hey…
Coding
Winds
MULTIPLE INHERITANCE
Multiple
inheritance is the process of deriving a new class that inherits
the attributes from two or more classes.
Syntax of multilevel inheritance :
For better understanding look at the
example given below :
OUTPUT :
The value of a is : 10
The
value of b is : 20
Addition
of a and b is : 30
HYBRID INHERITANCE
Hybrid Inheritance is a type of
inheritance in which many types of inheritances combine.
We can look for the example given
below :
OUTPUT :
a =
5
b =
10
C =
15
Multiplication of a,b,c is : 750
Hierarchical inheritance is defined as the
process of deriving more than one class from a base class.
Syntax of Hierarchical inheritance:
An example of Hierarchical Inheritance,
is given below :
OUTPUT :
Enter the value of a1 and a2:
10
15
Product of a1 and a2 is : 150
Enter the value of a3 and a4:
20
25
product of a3 and a4 is : 500
Thank you so much for reading this blog, hope you have gain some knowledge from this.
No comments:
Post a Comment