INHERITANCE IN C++
The capability of a class to derive properties
and characteristics from another class is called Inheritance. Inheritance is one of the most important feature
of Object Oriented Programming.
Sub Class: The class that inherits properties from another class is called Sub class or Derived Class.
Super Class:The class whose properties are inherited by sub class is called Base Class or Super class.
Sub Class: The class that inherits properties from another class is called Sub class or Derived Class.
Super Class:The class whose properties are inherited by sub class is called Base Class or Super class.
The
article is divided into following subtopics:
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 same for all of the 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 in below figure:
You can clearly see that above process results
in duplication of same code 3 times. This increases the chances of error and
data redundancy. To avoid this type of situation, inheritance is used. 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 increas re-usability. Look at the below diagram in
which the three classes are inherited from vehicle class:
Using
inheritance, we have to write the functions only one time instead of three
times as we have inherited rest of the three classes from base class(Vehicle).
Implementing inheritance in C++: For
creating a sub-class which is inherited from the base class we have to follow
the below syntax.
Syntax:
Syntax:
class subclass_name : access_mode base_class_name
{ //body of subclass
};
Here, subclass_name is the name of the
sub class, access_mode is the mode in
which you want to inherit this sub class for example: public, private etc. and base_class_name is the name of the
base class from which you want to inherit the sub class.
Note: private member of the base class will never get inherited in the sub class.
Note: private member of the base class will never get inherited in the sub class.
// C++ program to demonstrate
implementation
// of Inheritance
#include <bits/stdc++.h>
using namespace std;
//Base class
class Parent
{
public:
int id_p;
};
// Sub class inheriting from
Base Class(Parent)
class Child :
public Parent
{
public:
int id_c;
};
//main function
int main()
{
Child
obj1;
//
An object of class child has all data members
//
and member functions of class parent
obj1.id_c
= 7;
obj1.id_p
= 91;
cout
<< "Child id is " <<
obj1.id_c << endl;
cout
<< "Parent id is " <<
obj1.id_p << endl;
return 0;
}
Output:
Child id is 7
Parent id is 91
In the above program the ‘Child’ class is
publicly inherited from the ‘Parent’ class so the public data members of the
class ‘Parent’ will also be inherited by the class ‘Child’.
Thank you guys for reading my blogs.
Follow me on :
0 comments:
Post a Comment