Chapter - 6 Inheritance CBSE CLASS 2019-20 C++ NOTES (OLD)
Chapter - 6 Inheritance CBSE CLASS 2019-20 C++ NOTES (OLD)
Chapter - 6 Inheritance CBSE CLASS 2019-20 C++ NOTES (OLD)
CHAPTER 6 - INHERITANCE
Mechanism of transferring the properties of one class to another class is called
inheritance. That is, a new class is created from an existing class.
Existing class is known as the base class or super class.
New class is known as the derived class or sub class.
Derived class can inherit the properties (data members & member functions) of base
class.
Advantages/Need for inheritance
1. Code reusability
2. Transitive nature
Code reusability
Once when a base class is created and checked, it can be used in various situations
without rewriting or redefining.
Advantages are faster development, easy maintenance and easy to extend.
Here UG & PG classes can inherit the properties of class person and student.
Once after including a function in person class, it can be accessed by the student, PG &
UG classes.
Transitive Nature
If a class „B‟ inherits the properties of class „A‟ then automatically all the subclasses of „B‟ will
inherit the properties of „A‟. This is known as the transitive nature of inheritance. If you make
any changes in the features of „A‟, then automatically that changes will get reflected in all its sub
classes.
Type of inheritance
1. Single inheritance
2. Multiple inheritance
3. Hierarchical inheritance
4. Multilevel inheritance
5. Hybrid inheritance
Single inheritance
A derived class with only one base class is known as single inheritance.
A
B
Multiple inheritance
When a class is derived from multiple base classes, it is known as multiple inheritance.
A B
C
2
Hierarchical inheritance
When many derived classes are created from a single base class, it is known as
hierarchical inheritance
A
B C D
Multilevel inheritance
The mechanism of deriving a class from another derived class is known as
multilevel inheritance.
Transitive nature of inheritance is reflected in this type.
A Base class
C Derived class of B
Hybrid inheritance
A combination of two or more types of inheritance is known as hybrid. It can be
any combination of multiple, multilevel or hierarchical
inheritance
A B C
A Hierarchical Multiple
B C D
Multiple Hierarchical
E F
D
If visibility mode = public, then public of base class becomes public to derived
class and protected of base class becomes protected to derived class.
If visibility mode=private, then public and protected data of base class becomes
private to derived class.
3
Single inheritance
#include<iostream.h>
Object can be created only for the derived class.
class base
By using the object of the derived class, only the public
{
and protected data and member functions of base
public:
int x; class can be inherited in the derived class.
void set x (int i)
{
x=i;
}
void display()
{
cout<<x;
}
};
class derived: public base
{
int y;
public:
void set y (int j)
{
y=j ;
}
void dis ()
{
cout <<y;
}
};
void main ()
{ derived dl ;
dl.setx(10) ;
dl.display ();
dl.sety(20);
dl.disp();
}
Eg:10, 20
4
Multiple inheritance
The syntax of defining a derived class using multiple inheritance is,
class derivedclassname : visibilitymode baseclass_1 , visibilitymode baseclass_2, ……
{
:
};
Example:
Let „A‟ be a base class which contains internal marks & „B‟ be another base class which contains
external marks. Class „C‟ is derived from both „A‟ & „B‟ and is used to find total marks.
class A
{
int internal;
public: A B
void setinternal( )
{
cin>>internal;
}
int getinternal( ) C
{
return internal;
}
};
class B
{
int external;
public:
void setexternal( )
{
cin>>external;
}
int getexternal( )
{
return external;
}
};
class C : public A, public B
{
int total;
public:
void cal( )
{
total = getinternal( ) + getexternal( );
cout<<total;
}
};
int main( )
{
C O1;
O1.setinternal( );
O1.setexternal( );
O1.cal( );
};
5
int main( )
{
manager m1;
m1.getdata1( );
m1.getdata2( );
m1.putdata1( );
m1.putdata2( );
return 0;
}
Access control in privately derived class
A class is privately derived by giving the visibility mode as private.
Syntax is,
class derivedclassname : private baseclassname
{
…..
};
Here the public & protected members of base class become private in derived class. So it
cannot be accessed directly by the objects of derived class. It can be accessed by the
member functions or friend functions of derived class.
Example
class manager : private employee
{
char design[20];
public:
void getdata2( )
{
getdata1( );
getsalary( );
cin>>design;
}
void putdata2( )
{
putdata1( );
cout<<design;
}
};
int main( )
{
manager m1;
m1.getdata2( );
m1.putdata2( );
return 0;
}
Some facts of inheritance
1. If the name of a data member is same as a global variable, then global variable
becomes hidden inside the class.
int t = 10;
class A
{
int t;
public:
void getdata( )
{
t= 20;
7
In the case of ANSI compiler, to change the access type, the keyword “using”
need to be used.
i.e. using Base::x;
4. Abstract class
An abstract class is a class that serves only as a base class from which other classes can
be derived and no objects exists for this base class.
Making a private member inheritable
This can be done in 2 ways.
1. By making the access specifier of the private member as public.
Then it becomes accessible throughout the program and this eliminates the concept of
data hiding.
2. By making the access specifier of the private member as protected.
Here the concept of data hiding is retained and at the same time makes it inheritable.
Overriding base class functions in derived class
If the derived class defines a function with the same name as that of base class, then the base
class function becomes hidden or shadowed in derived class. This situation is called as
function overriding or shadowing.
Example
class Base
{
….
public:
void f1( )
{
cout<<”Base class f1”;
}
void f2(char ch)
{
cout<<”Base class f2”;
}
void f3( )
{
cout<<”Base class f3”;
}
};
class Derived : public Base
{….
public:
void f1(double d)
{
cout<<”Derived class f1”;
}
void f2(char ch)
{
cout<<”Derived class f2”;
}
};
void main( )
{Derived O1;
O1.f1(32.5); //Derived class function is invoked
O1.f1( ); //Invalid
O1.f2(„A‟); //Derived class function is invoked
O2.f3( ); //Base class function is invoked
}
9
In case if we want to access the Base class f1 & f2 functions, it can be possible using the syntax,
O1.Base::f1( );
O1.Base::f2(„A‟ );
If a function is defined with the same name as that of the base class in derived class, then
irrespective of the argument list and return type, derived class hides the base class function.
To make functions with the same name of the base class available in derived class, declare it
again in derived class with the help of „using‟ keyword.
Syntax is,
using Baseclassname::functionname;
This is possible in code blocks c++ compiler.
Example:
class Derived : public Base
{
…
public:
using Base::f1;
using Base::f2;
void f1(double d)
{
….
}
void f2(char ch)
{
….
}
};
void main( )
{
Derived O1;
O1.f1( );
}
Constructors in multiple inheritance
If base class is not having any constructors with arguments, then it is not necessary for a
constructor in the derived class.
If base class contains constructors with arguments, then it is must that the derived class should
have a constructor for passing values to base constructor.
This is because constructors/destructors cannot be inherited.
Example:
class A
{ int x;
public:
A(int i)
{
x=i;
}
};
class B : public A
{public:
B(int i) : A(i)
{
…
}
};
10
The derived class constructor receives the entire list of arguments and passes them to the base
class constructor.
The base class constructor body is executed before executing the body of derived class
constructor.
Syntax is,
Derived-constructor(argument list) : base1(argument list1), base2(argument list2),……,baseN(argument listN)
{
……….
}
Example
class B1
{
public:
B1(int i)
{
….
cout<<”\nConstructor of B1”;
}
~B1( )
{
cout<<”\nDestructor of B1”;
}
};
class B2
{
public:
B2(int i)
{
….
cout<<”\nConstructor of B2”;
}
~B2( )
{
cout<<”\nDestructor of B2”; Output
} Constructor of B1
}; Constructor of B2
class Der : public B1, public B2 Derived constructor
{ Derived destructor
public: Destructor of B2
Der(int a, int b, int c) : B1(a), B2(b) Destructor of B1
{
cout<<”\nDerived constructor”;
}
~Der( )
{
cout<<”\nDerived destructor”;
}
};
void main( )
{
Der D1(10,20,30);
}
11
Base | int a
Derived3 | int d
class B
{
public:
int a;
};
class D1 : virtual public B
{
public:
int b;
};
class D2 : virtual public B
{
public:
int c;
};
class D3 : public D1, public D2
{
…
};
void main( )
{
D3 O1;
O1.a=5; //valid
}
If class „x‟ has a constructor with arguments, then arguments are passed through the
constructor of „z‟.
Example
class z
{
x O1;
y O2;
public:
z(int a, int b, int c) : O1(a), O2(b)
{
….
}
};
Explicit and implicit calls to the constructor
1.
class B
{
public: If a class inherits from another class,
B(int a)
then the derived class has is-a
{
….. relationship with its base class.
}
};
class D1: public B
{
public:
D(int a, int b) : B(a)
{
…. Explicit call
}
};
2.
class B
{
public: If a class contains objects of another
B(int a)
class, then the enclosing class has has-a
{
….. relationship with the contained class.
}
};
class D1
{
B O1;
public:
D(int a, int b) : O1(a)
{
…. Implicit call
}
};
14
3.
If a class indirectly contains objects of another class through pointer, then it is said to
have holds-a relationship with the other class.
Example
class A
{
….
};
class B
{
A *op1;
….
public:
B(int a, int b, A *p) : op1(p)
{
….
}
};