Inheritance
Inheritance
• Inheritance is the ability of one class to inherit the properties of
another class. A new class can be created from an existing class. The
existing class is called BASE CLASS or SUPER CLASS and the new class
is called DERIVED CLASS or SUB CLASS.
• Example:
Car inherits from another class auto_mobile
Computer_student inherits from another class student
Arrange concepts into an inheritance
hierarchy
• Concepts at higher levels are more general
• Concepts at lower levels are more specific (inherit
properties of concepts at higher levels)
Vehicle
Wheeled vehicle Boat
Car Bicycle
2-door 4-door
Advantages of inheritance
• When a class inherits from another class, there are three benefits:
(1) You can reuse the methods and data of the existing class
(2) You can extend the existing class by adding new data and new
methods
(3) You can modify the existing class by overloading its methods
with your own implementations
Inheritance is the property that allows the
reuse of an existing class to build a new class
Base Class Derived class
Methods Base class methods
and +
Properties Additional methods
General Syntax for Inheritance
class <derived classname > : access-specifier <base classname >
{
---
----
};
Access specifier
• Public - The members declared as Public are accessible
from outside the Class through an object of the class.
• Protected - The members declared as Protected are
accessible from outside the class BUT only in a class
derived from it.
• Private - These members are only accessible from
within the class. No outside Access is allowed.
Accessibility of base class member
Access Specifier Accessible from Accessible from Accessible from
own class derived object outside class
class(inheritable)
Public Yes Yes Yes
Protected Yes Yes No
Private Yes No No
Inheritance
Single Inheritance Hierarchical
Inheritance
Multilevel Multiple Inheritance
Inheritance
Single Inheritance
A class can be derived from a single base class is called single
inheritance
Person
Employee
Multilevel
Inheritance
A class be can derived from a derived class which is known as multilevel
inheritance.
Vehicle Person
Car Employee
Racing car Part time
Employee
Multiple Inheritance
It is the process of creating new class from more than one base
classes.
Syntax :
class <derived class >:<access specifier>
base_class1,<access specifier> base_class2...
{
private :
// members;
protected :
Person Employee
// members;
public :
//memebers;
};
Teacher
Hierarchical Inheritance
If more than one class is inherited from a base class, it's known as hierarchical
inheritance. In general, all features that are common in child classes are included
in base class in hierarchical inheritance.
Employee
Permanent Temporary
Employee Employee
Hybrid Inheritance
Hybrid is nothing but the combination of Multilevel
and multiple Inheritance
Multipath problem void sum()
class B: virtual public A {
#include<iostream>
{ d=a+b+c;
using namespace std; protected:
}
class A int b;
public: void display()
{
void setb() {
protected: {
b=20; cout<<"Sum of a,b,c is: " << d ;
int a;
} }
public: };
};
void seta() class C: virtual public A
{ int main()
{
protected: {
a=10; int c;
public: D d1;
}
void setc() d1.seta();
}; {
d1.setb();
c=30;
} d1.setc();
}; d1.sum();
class D:public B,public C
{ d1.display();
private: return 0;
int d;
}
Same Data Member Name in Base
and Derived Class
#include<iostream> class B:public A
using namespace std; { main()
{
class A int x; B b;
{ public: A a;
protected: B(){x=10;} a.print();
int x; b.print();
public: }
void print()
A(){x=5;}
{
void print()
cout<<"inside B"<<endl;
{
cout<<x<<endl;
cout<<"inside
A"<<endl;} }
Order of Constructor Call
Base class constructors are always called in the derived class constructors.
Whenever you create derived class object, first the base class default
constructor is executed and then the derived class's constructor finishes
execution.
Points to Remember
1.Whether derived class's default constructor is called or parameterised is
called, base class's default constructor is always called inside them.
2.To call base class's parameterised constructor inside derived class's
parameterised constructor, we must mention it explicitly while declaring
derived class's parameterized constructor.
Default constructor
class Derived : public Base
#include <iostream>
{
using namespace std; int y;
class Base
{ public:
Derived()
int x;
{
cout << "Derived default constructor\n";
public: }
Base() };
{
int main()
cout << "Base default {
constructor\n";
Base b;
} Derived d1;
parameterized constructor(Base derived not called)
class Derived : public Base
#include <iostream>
{
using namespace std; int y;
class Base public:
{ Derived()
int x; {
public: cout << "Derived default constructor\n";
Base() }
{ Derived(int i)
{
cout << "Base default constructor\n";
cout << "Derived parameterized constructor\n";
}
}
Base(int i) };
{ int main()
x = i; {
cout << "Base Parameterized Constructor\ Base b;
n"; Derived d1;
} Derived d2(10);
parameterized constructor(Base derived called)
class Derived : public Base
#include <iostream> {
using namespace std; int y;
class Base
public:
{
Derived()
int x; {
cout << "Derived default constructor\n";
public: }
Base()
Derived(int i):Base(i)
{
{
cout << "Base default constructor\n"; cout << "Derived parameterized constructor\n";
} }
Base(int i) };
{
int main()
x = i; {
cout << "Base Parameterized Constructor\n"; Base b;
} Derived d1;
Derived d2(10);
};
Base class Parameterized Constructor in Derived class Constructor
We can explicitly mention to call the Base class's parameterized constructor
when Derived class's parameterized constructor is called.
Constructor call in Multiple Inheritance
Its almost the same, all the Base class's constructors are called inside derived class's
constructor, in the same order in which they are inherited.
class A : public B, public C ;
In this case, first class B constructor will be called, then class C constructor and then
class A constructor.
3 Types of Access Specifiers
• Type 1: inherit as private
Base Derived
private members inaccessible
protected members private members
public members private members
3 Types of Access Specifiers
• Type 2: inherit as protected
Base Derived
private members inaccessible
protected members protected members
public members protected members
3 Types of Access Specifiers
• Type 3: inherit as public
Base Derived
private members inaccessible
protected members protected members
public members public members
Public Inheritance:
All Public members of the Base Class become Public Members of the derived class &
All Protected members of the Base Class become Protected Members of the Derived
Class.
class Base {
public: int a;
protected: int b;
private: int c;
};
class Derived:public Base {
void doSomething() {
a = 10; //Allowed
b = 20; //Allowed
c = 30; //Not Allowed, Compiler Error
}
};
int main() {
Derived obj;
obj.a = 10; //Allowed
obj.b = 20; //Not Allowed, Compiler Error
obj.c = 30; //Not Allowed, Compiler Error
}
Private Inheritance:
All Public members of the Base Class become Private Members of the Derived class &
All Protected members of the Base Class become Private Members of the Derived Class.
Class Base {
public: int a;
protected: int b;
private: int c; };
class Derived:private Base //defaults access specifier is private {
void doSomething() {
a = 10; //Allowed
b = 20; //Allowed
c = 30; //Not Allowed, Compiler Error }
};
class Derived2:public Derived {
void doSomethingMore() {
a = 10; //Not Allowed, Compiler Error, a is private member of Derived now
b = 20; //Not Allowed, Compiler Error, b is private member of Derived now
c = 30; //Not Allowed, Compiler Error }
};
int main() {
Derived obj; obj.a = 10; //Not Allowed, Compiler Error
obj.b = 20; //Not Allowed, Compiler Error
obj.c = 30; //Not Allowed, Compiler Error }
• Protected Inheritance:
All Public members of the Base Class become Protected Members of the derived class
& All Protected members of the Base Class become Protected Members of the Derived
Class.
Class Base {
public: int a;
protected: int b;
private: int c; };
class Derived:protected Base {
void doSomething() {
a = 10; //Allowed
b = 20; //Allowed
c = 30; //Not Allowed, Compiler Error }
};
class Derived2:public Derived {
void doSomethingMore() {
a = 10; //Allowed, a is protected member inside Derived & Derived2 is
public derivation from Derived, a is now protected member of Derived2
b = 20; //Allowed, b is protected member inside Derived & Derived2 is public
derivation from Derived, b is now protected member of Derived2
c = 30; //Not Allow ed, Compiler Error }
};
int main() {
Derived obj; obj.a = 10; //Not Allowed, Compiler Error
obj.b = 20; //Not Allowed, Compiler Error
obj.c = 30; //Not Allowed, Compiler Error
}