Inheritance Notes Class XII
Inheritance Notes Class XII
Lecture -1
Definition: Creating or deriving a new class using another class as a base is called inheritance in C++.
The new class created is called a Derived class and the old class used as a base is called a Base class
in C++ inheritance terminology.
The derived class will inherit all the features of the base class in C++ inheritance. The derived class can
also add its own features, data etc., It can also override some of the features (functions) of the base
class, if the function is declared as virtual in base class.
C++ inheritance is very similar to a parent-child relationship. When a class is inherited all the functions
and data member are inherited, although not all of them will be accessible by the member functions of the
derived class. But there are some exceptions to it too.
Let us see a piece of sample code for C++ inheritance. The sample code considers a class named
vehicle with two properties to it, namely color and the number of wheels. A vehicle is a generic term and it
can later be extended to any moving vehicles like car, bike, bus etc.,
class vehicle //Sample base class for c++ inheritance tutorial
{
protected:
char colorname[20];
int number_of_wheels;
public:
vehicle();
~vehicle();
void start();
void stop();
void run();
};
class Car: public vehicle //Sample derived class for C++ inheritance tutorial
{
protected:
char type_of_fuel;
public:
Car();
};
The derived class Car will have access to the protected members of the base class. It can also use the
functions start, stop and run provided the functionalities remain the same.
1. Reusability – Inheritance allows to add more features to an existing class without modifying it , and in this
way provides reusability of attributes defined resulting into faster development time, easier maintenance
and easier control.
Inheritance is considered to be as Extended classes because a class can extend its feature in the form
of derived class without modifying the existing class.
Base Class/ Super Class - A class from which another class is inheriting its properties is called base
class.
Derived /Sub Class – The class inheriting properties is known as a sub class.
Type of Inheritance
1. Single Inheritance – When a sub class inherits only from a single base class.
2. Multiple Inheritance – when a sub class inherits from multiple base classes, it is known as
multiple inheritance.
3. Hierarchical Inheritance – When many sub classes inherit from a single base class, it is known as
hierarchical Inheritance.
5. Hybrid Inheritance- This type of Inheritance combines two or more forms of Inheritance eg. when
a sub class inherits from multiple base classes and all of its base classes inherit from a single
base class is known as Hybrid Inheritance.
class Base { …. };
{……};
C C1;
in the above example the constructor A( ) will invoked followed by B ( ) and followed by C( ) and the
destructor behaves in reverse manner i.e. ~C( ) , ~B( ), ~A( ) in the case when an object expires.(example
related to multilevel Inheritance)
If the Base class has parameterized constructors also, then this is the responsibility of derived
class to provide the parameters to the Base class constructor so it is mandatory for the derived
Comp.Sc. - Inheritance Notes by Chandni Agarwal
class to have a constructor and pass the arguments to the base class constructor. The derived
class takes the responsibility of supplying the value to the parameters of the base class
constructor.
few examples are given:
Case - I Multilevel Inheritance , Only base class has parameterised constructor.
class A
{
private : int x;
public : A( int a) { x=a;}
void getA( );
void putA( );
~A( );
};
class B : public A
{
private : int Z;
public : B( int c,int d) : A(d) { Z=c; }
void getB( );
~B( );
};
class C : public B
{
private : int W;
public : C( int a,int b) : B(a,b)
{ };
void getC( );
~C( );
};
C C1(13,5);
In the above example value of x will be 13 and Z will 5. The number of parameters ithe derived class
object requires are to be passed as parameters.
Constructors in Multiple Inheritance -
If this is the case of multiple Inheritance The construcors are called in the order of their Inheritance eg.
Class C: public A,private B , then first A class consrtuctor will invoke then B( ) and then C( ) will invoke.
in eg. C(int a, Int b) : B(a,b) , the part immmediately following the colon is called initialisation and the list
containing values for base class initialisation is called Mem-Initialisation.
Inheritance and access control - Read from Book Pgno. 279 - 283.
Containership
When a class contains objects of other class types as its members, it is referred to as CONTAINERSHIP
or CONTAINMENT or AGGREGATION.
Containership establishes “has-a” relationship among classes. This relationship is different from “a kind
of” relationship of Inheritance. For example, a microcomputer has a microprocessor whereas it is a kind of
digital computer. Thus, a microcomputer can inherit the properties of a digital computer but it must contain
a microprocessor. For eg.
class address{ int hno;
char addr_line[40];
public :
void getdata();
void putdata();
};
class person{
char name[20];
int age;
address add; // CONTAINERSHIP
public :
void read_data();
void display_data();
};
Constructors in Containership
We can use as many member objects as are required in a class. For each member object we add a
constructor call in the initialization list. If a base class contains an object of some other class then, a class
deriving from this base class needs to pass all the arguments required by the base class which in turn to
its member objects. Consider the following class definition where student class is deriving from person as
base class.
class student : public person
{
…… // data members
public:
student( int a, int b, int c) : person(a,b) // class name is written , i.e. explicit call
{….// constructor body of class student
} };
The constructor for a member object of a class is invoked implicitly (through the object name) and the
constructor for the base class is invoked explicitly (through the class name).
VIRTUAL BASE CLASS – Suppose you have two derived classes B and C that have a common base
class A, and you also have another class D that inherits from B and C. You can declare the base class A
as virtual to ensure that B and C share the same subobject of A. In the following example, an object of
class D has two distinct subobjects of class L, one through class B1 and another through class B2. You
can use the keyword virtual in front of the base class specifiers in the base lists of classes B1 and B2 to
indicate that only one sub object of type L, shared by class B1 and class B2, exists. For example:
Using the keyword virtual in this example ensures that an object of class D inherits only one subobject of
class L. A derived class can have both virtual and non virtual base classes. For example:
class V { /* ... */ };
In the above example, class X has two sub objects of class V, one that is shared by classes B1 and B2
and one through class B3.
Point to remember