Chapter 3(INHERITANCE)
Chapter 3(INHERITANCE)
INHERITANCE:
The capability of a class to derive properties and characteristics
from another class is called Inheritance. Re-accessability is yet
another feature of OOP’s. C++ strongly supports the concept of
re-usability. Once a class has been written and tested, it can be
adopted by another programmers. The mechanism of deriving a
new class from the old one is called INHERITANCE. The old
class is called BASE class and the new one is called DERIVED
class.
When we say derived class inherits the base class, it means, the
derived class inherits all the properties of the base class, without
changing the properties of base class and may add new features
to its own.
• 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 a
subclass is called Base Class or Super Class.
MODES OF INHERITANCE:
There are 3 modes of inheritance
• Public mode
• Protected mode
• Private mode
• Public mode: If we derive a subclass from a public base
class then the public member of the base class will become
public in the derived class and protected members of the
base class will become protected in the derived class.
• Protected mode: If we derive a subclass from a protected
base class then both public members and protected
members of the base class will become protected in the
derived class.
• Private mode: If we derive a subclass from a private base
class then both public and protected members of the base
class will become private in the derived class.
Example:
class A
{ public: int x;
protected: int y;
private: int z;
};
class B : public A
{ // x is public
// y is protected
// z is not accessible from B
};
class C : protected A
{ // x is protected
// y is protected
// z is not accessible from C
};
class D : private A // 'private' is default for classes
{ // x is private
// y is private
// z is not accessible from D
};
• Hybrid inheritance
class A
{ ... ..
... };
class B: public A
{ ... ..
... };
Program:
// C++ program to explain Single inheritance
#include<iostream>
using namespace std;
// base class
class Vehicle {
public:
Vehicle()
{
cout << "This is a Vehicle\n";
}
};
// sub class derived from a single base classes
class Car : public Vehicle {
};
// main function
int main()
{
// Creating object of sub class will invoke the constructor of base
classes
Car obj;
return 0;
}
Output
This is a Vehicle
2.Multiple Inheritance: Multiple Inheritance is a feature of
C++ where a class can inherit from more than one class. i.e
one subclass is inherited from more than one base class.
Syntax:
Class subclass_name : access_mode base_class1,
access_mode base_class2, ....
{
// body of subclass
};
class B { ... .. ... };
class C { ... .. ... };
class A: public B, public C { ... ... ... };
Program:
// C++ program to explain multiple inheritance
#include <iostream>
using namespace std;
// first base class
class Vehicle {
public:
Vehicle()
{ cout << "This is a Vehicle\n"; }
};
// second base class
class FourWheeler {
public:
FourWheeler()
{
cout << "This is a 4 wheeler Vehicle\n";
}
};
// sub class derived from two base classes
class Car : public Vehicle, public FourWheeler {
};
// main function
int main()
{ // Creating object of sub class will invoke the constructor of base
classes.
Car obj;
return 0;
}
Output
This is a Vehicle
This is a 4 wheeler Vehicle
Syntax:-
class A {
// body of the class A. }
class B : public A {
// body of class B. }
class C : public A {
// body of class C. }
class D : public A {
// body of class D. }
Program:
// C++ program to implement Hierarchical Inheritance
#include <iostream>
using namespace std;
// base class
class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};
// first sub class
class Car : public Vehicle {
};
// second sub class
class Bus : public Vehicle {
};
// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base class.
Car obj1;
Bus obj2;
return 0;
}
Output
This is a Vehicle
This is a Vehicle
Program:
// C++ program for Hybrid Inheritance
#include <iostream>
using namespace std;
// base class
class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};
// base class
class Fare {
public:
Fare() { cout << "Fare of Vehicle\n"; }
};
// first sub class
class Car : public Vehicle {
};
// second sub class
class Bus : public Vehicle, public Fare {
};
// main function
int main()
{ // Creating object of sub class will invoke the constructor of base
class.
Bus obj2;
return 0;
}
Output
This is a Vehicle
Fare of Vehicle
PUBLIC, PROTECTED AND PRIVATE
INHERITANCE IN C++:
OUTPUT:
public derivation: 30
protected derivation: 30
private derivation: 30
Another example of public, protected and private
inheritance:
#include <iostream>
using namespace std;
class Base {
private:
int pvt = 1;
protected:
int prot = 2;
public:
int pub = 3;
// function to access private member
int getPVT()
{ return pvt; }
};
class Public Derived : public Base
{
public:
// function to access protected member from Base
int getProt()
{
return prot;
}
};
class Protected Derived : protected Base {
public:
// function to access protected member from Base
int getProt() { return prot; }
// function to access public member from Base
int getPub() { return pub; }
};
class Private Derived : private Base {
public:
// function to access protected member from Base
int getProt() { return prot; }
// function to access public member
int getPub() { return pub; }
};
int main()
{
Public Derived object1;
cout << "Private = " << object1.getPVT() << endl;
cout << "Protected = " << object1.getProt() << endl;
cout << "Public = " << object1.pub << endl;
OUTPUT:
Private=1
Protected=2
Public=3
Private cannot be accessed
Protected=2
Public=3
Private cannot be accessed
Protected=2
Public=3
#include <iostream>
using namespace std;
class A
{ public:
int a = 5;
A()
{ cout << "Constructor for class A" << endl;
}
};
class B
{ public:
int b = 10;
B()
{ cout << "Constructor for class B" << endl;
}
};
class C: public A, public B
{ public:
int c = 20;
C()
{ cout << "Constructor for class C" << endl;
cout<<"Class C inherits from class A and class B" << endl;
}
};
int main()
{ C obj;
cout<<"a = "<< obj.a <<endl;
OUTPUT:
Constructor for class A
Constructor for class B
Constructor for class C
Class C inherits from class A and class B
a=5
b = 10
c = 20
CONSTRUCTOR AND DESTRUCTOR IN
INHERITANCE:
When we are using the constructors and destructors in the
inheritance, parent class constructors and destructors are
accessible to the child class hence when we create an object for
the child class, constructors and destructors of both parent and
child class get executed.
#include <iostream>
{ public:
parent() //constructor
~parent()//destructor
};
{ public:
child() //constructor
~ child() //destructor
};
int main()
{ //automatically executes both child and parent class constructors and destructors
because of inheritance
child c;
return 0;
OUTPUT:
derived-constructor(arg-list) : base1(arg-list),
base2(arg-list), ...
baseN(arg-list);
Program:
#include <iostream>
class base
{ protected:
int i;
public:
base(int x)
{ i = x;
~base()
};
{ int j;
{ j = x;
~derived()
}
void show()
};
int main()
ob.show();
return 0;
OUTPUT:
Constructing base
Constructing derived
43
Destructing derived
Destructing base
Another Program:
#include <iostream>
class base1
{ protected:
int i;
public:
base1(int x)
{ i = x;
~base1()
};
class base2
{ protected:
int k;
public:
base2(int x)
{ k = x;
~base2()
};
public:
{ j = x;
~derived()
void show()
{ cout << i << " " << j << " " << k << "\n";
} };
int main()
ob.show();
return 0;
OUPUT:
Constructing base1
Constructing base2
Constructing derived
435
Destructing derived
Destructing base2
Destructing base1
#include <iostream>
class A {
public:
void show()
};
class B : public A {
};
class C : public A {
};
};
int main()
D object;
object.show();
Syntax 1:
Program:
#include <iostream>
class A {
public:
int a;
A() // constructor
a = 10;
};
};
};
class D : public B, public C {
};
int main()
return 0;
OUTPUT:
a=10
GRANTING ACCESS:
When a base class is inherited as private, all public and
protected members of that class become private members
of the derived class.
However, in certain circumstances, you may want to
restore one or more inherited members to their original
access specification
For example, you might want to grant certain public
members of the base class public status in the derived class
even though the base class is inherited as private.
In Standard C++, you have two ways to accomplish this.
First, you can use a using statement, which is the preferred
way.
The second way to restore an inherited member's access
specification is to employ an access declaration within the
derived class.
An access declaration takes this general form:
base-class::member;
#include <iostream>
class base
public:
int j, k;
void seti(int x)
{ i = x; }
int geti()
{ return i; }
};
{ public:
int a; // public
};
int main()
{ derived obj;
obj.seti(10);
cout << obj.geti() << " " << obj.j << " " << obj.a;
return 0;
OUTPUT:
10 20 40