Inheritance
Inheritance is the capability of one
class to acquire properties and
characteristics from another class.
The class whose properties are
inherited by other class is called
the Parent or Base or Super class.
The class which inherits properties of
other class is
called Child or Derived or Sub class.
Inheritance makes the code reusable.
When we inherit an existing class, all its
methods and fields become available in the
new class.
NOTE: All members of a class except Private,
are inherited
Syntax:
class DerivedClass : accessSpecifier BaseClass
While defining a subclass like this, the
super class must be already defined or at least
declared before the subclass declaration.
Purpose of Inheritance.
Code Reusability
Method Overriding (Hence, Runtime
Polymorphism.)
Use of Virtual Keyword
Access specifiers in inheritance
Access specifier can be public, protected and
private.
The default access specifier is private.
Access specifiers affect accessibility of data
members of base class from the derived class.
In addition, it determines the accessibility of
data members of base class outside the
derived class.
Inheritance Access Specifiers
Public Inheritance:
class DerivedClass : public BaseClass
This inheritance mode is used mostly.
In this mode the protected member of Base
class becomes protected members of Derived
class and public becomes public.
Derived class of Derived Classes
If we are inheriting a derived class using a
public inheritance as shown below
class B : public A
class C : public B
then public and protected members of
class A will be accessible in class C as public and
protected respectively.
Protected Inheritance:
class DerivedClass : protected BaseClass
In protected mode, the public and
protected members of Base class becomes
protected members of Derived class.
Derived class of Derived Classes
If we are inheriting a derived class using a
protected inheritance as shown below
class B : protected A
class C : protected B
then public and protected members of
class A will be accessible in class C as protected
Private Inheritance
class DerivedClass : private BaseClass
(Or)
class DerivedClass : BaseClass
// By default inheritance is private//
In private mode the public and protected
members of Base class become private members
of Derived class.
Derived class of Derived Classes
If we are inheriting a derived class using a
private inheritance as shown below
class B : private A
class C : private B
then public and protected members of
class A will not be accessible in class C.
All the visibility modes
Types of Inheritance
There are different types of inheritance:
1. Single Inheritance
2. Multiple Inheritance
3. Multilevel Inheritance
4. Hierarchical Inheritance
5. Hybrid (Virtual) Inheritance
Single Inheritance
Single inheritance represents a form of
inheritance when there is only one base class
and one derived class.
Syntax:
class base_classname
{
properties;
methods;
};
class derived_classname : visibility_mode base_classname
{
properties;
methods;
};
Example
Cont…
Multiple Inheritance
Multiple inheritance represents a kind of
inheritance when a derived class inherits
properties of multiple classes.
There are three classes A, B and C and
derived class is D.
Syntax
Class DerivedClass: accessSpecifier BaseClass1, BaseClass2, …, BaseClassN
Example
class A
{
public:
int x;
void getx()
{
cout << "enter value of x: "; cin >> x;
}
};
class B
{
public:
int y;
void gety()
{
cout << "enter value of y: ";
cin >> y;
}
};
class C : public A, public B
//C is derived from class A and class B
{
public:
void sum()
{
cout << "Sum = " << x + y;
}
};
Cont…
int main()
{
C obj1;
//object of derived class C
obj1.getx();
obj1.gety();
obj1.sum();
return 0;
} //end of program
Multilevel Inheritance
A class is derived from another derived class
then it is called multilevel inheritance.
Multilevel inheritance, a class has more than
one parent class.
Example
#include<iostream.h>
class Student
{
protected:
int marks;
public:
void accept()
{
cout<<" Enter marks";
cin>>marks;
}
};
class Test :public Student
{
protected:
int p=0;
public:
void check()
{
if(marks>50)
{
p=1;
}
}
};
Cont…..
class Result :public Test
{
public:
void print()
{
if(p==1)
cout<<"\n You have passed";
else
cout<<"\n You have not passed";
}
};
int main()
{
Result r;
r.accept();
r.check();
r.print();
return 0;
}
Hierarchical Inheritance
In this type of inheritance, one parent class has
more than one child class.
Class Person is the base class of
both Student and Employee classes.
Class Student is the base class
for ITStudent and MathStudent classes.
Employee is the base class
for Driver and Engineer classes.
Syntax
Example
#include <iostream.h>
class A //single base class
{
public:
int x, y;
void getdata()
{
cout << "\nEnter value of x and y:\n";
cin >> x >> y;
}
};
class B : public A //B is derived from class base
{
public:
void product()
{
cout << "\nProduct= " << x * y;
}
};
class C : public A
//C is also derived from class base
{
public:
void sum()
{
cout << "\nSum= " << x + y;
}
};
int main()
{
B obj1; //object of derived class B
C obj2; //object of derived class C
obj1.getdata();
obj1.product();
obj2.getdata();
obj2.sum();
return 0;
}
Hybrid Inheritance ( Virtual Inheritance)
Hybrid inheritance is performed when we
have to mix different types of inheritance
within a single program.
For example, mixing single
inheritance with multiple
inheritance or multilevel inheritance within a
single program.
Cont…..
Protected data with Private inheritance
The member function of a derived class
cannot access the private member variable of
the base class.
The private members of the base class can be
accessed using the public member function of
the same class.
This approach makes the program lengthy.
To overcome this problem, the creator of C+
+introduced another access specifier
protected.
The protected is similar to private, but it
allows the derived class to access the private
members directly.
Example
Cont…
Constructor in derived class
When a class is derived from another class, it is
possible to define a constructor in the derived
class.
The data members of both base and derived
classes can be initialized.
It is not essential to declare a constructor in a
base class.
Thus, the constructor of the derived class works
for it’s base class also.
It is called as constructor in the derived class or
common constructors.
Example