Inheritance NEW
Inheritance NEW
Vehicles
Vehicles
Pulled Vehicles
Automobiles
Man Woman
Grad UG
Father Mother
Inheritance in c++ allows us to create new
classes which are derived from older
Notation:
classes. A class is a group of objects
that share common properties & base
relationship.
The derived classes are
called subclasses or simply subclass1 subclass2
derived classes
The older classes are superclasses
or parent classes or base classes
of
writing new classes.
Using well-tested base classes helps reduce
2. Enhance extensibility
Inheritance may take place in many forms, which
are as follows:-
1. Single Inheritance
2. Multiple Inheritance
3. Hierarchical Inheritance
4. Multilevel Inheritance
5. Hybrid Inheritance
When a sub-class is inherited only
from one base-class.
X
e.g. base class
Y derived class
When a sub class is inherited from
multiple base classes.
e.g. x y Base classes
z derived class
A base class
B C D
derived-
classes
When a sub-class is inherited from a
class that itself is inherited from another
class, is known as multilevel inheritance.
x
Base class of Y
sub class of X
y Base class of Z
z Sub class of Y
When a sub-class is inherited from multiple base-
classes and all of its base-classes are inherited
from a single base-class, is called hybrid
inheritance.
e.g.
w
Base class of Y
x
y sub class of X
Base class of Z
z
Sub class of Y and
X
We have seen two access modes in C+
+ classes: public and private
Public members are directly accessible by
users of the class
Private members are NOT directly
accessible by users of the class, not even
by inheritors
There is a 3rd access mode: protected
Protected members are directly accessible
by derived classes but not by other users
class derivedClass_name : visibility mode baseClass_name
{
private :
public:
protected:
};
The derived class inherits from the base class: all public members,
all protected members (see later), and the default constructor
The additional members defined can have the same name (and type) as
those of the base class (as when some base members are to be redefined)
class Sub : public Super
{
private:
public:
protected:
};
class derivedClass_name : visibility mode baseClass_name1,
visibility mode baseClass_name2 , visibility mode baseClass_name3
{ private :
public:
protected:
};
class Sub : public SuperA , private
SuperB
{
private:
public:
protected:
};
The access mode or visibility mode of
derived class specifies whether the
features of the base class are
privately , publicly , protected derived.
The visibility modes basically control
the access-specifier of inheritable
members of base class , in the derived
class.
Visibility of inherited
base class members in
derived class:-
Visibility In derived class In derived class
mode is PUBLIC MEMBERS PROTECTED
OF BASE CLASS MEMBERS OF Private
become BASE CLASS Members of
become base class are
not directly
Public Public Protected accessible to
derived class.
Protected Protected Protected
private private private
It means that the derived class can access the public
and protected members of the base class but not the
private members.
With publicly derived class , the public members of
the base class become the public members of the
derived class and the protected members of the
base class become the protected members of the
derived class.
Class Super Class Sub
Private section
Private section
a init()
x Check()
Public section
Protected section b readit()
z getval()
Protected section
c writeit()
It means that the derived class can access
the public and protected members of the
base class privately.
With protectedly derived class , the public
members and the protected members of the
base class become protected members of the
derived class.
Class Super Class Sub
y display()
z Getval()
Protected section
Check() c Writeit()
z Getval()
While defining a base class following things
should be kept in mind-
private Yes No No
When a sub-class is inherited only from one base-
class.
X
e.g. base class
Y
derived class
Syntax:-
class subclass_name : visibility-mode baseclass_name
{
// members of derived class
};
When a sub class is inherited from multiple base classes.
e.g. Base classes
x y
derived class
z
There are ‘x’ and ‘y’ are super classes and sub class ‘z’ inherits the
properties from both super classes.
Syntax:-
class subclass : visibility mode superclass1, visibility mode
superclass2……
{
// members of derived class.
};
When many sub-classes is inherited from a single super-class, is known as
hierarchical inheritance.
e.g.
A Base-class
B C D derived-classes
Syntax:-
class sub_class1: visibility-mode super-class
{
//members of derived class sub_class1
};
class sub_class2 : visibility-mode super-class
{
//members of derived class sub_class2
};
When a sub-class is inherited from a class that itself is inherited from
another class, is known as multilevel inheritance.
e.g. Base class of Y
x
y sub class of X
Base class of Z
z Sub class of Y
Syntax:-
class sub_class1: visibility-mode super_class
{
//members of derived class of super_class and base class of
sub_class2
};
class sub_class2 : visibility-mode sub_class1
{
//members of derived class of sub_class1
};
When a sub-class is inherited from multiple base-
classes and all of its base-classes are inherited
from a single base-class, is called hybrid
inheritance. w
e.g. x y
Base class of Y
sub class of X
z Base class of Z
Sub class of Y
Syntax:-
class sub_2 : visibility-mode super
class super
{
{
};
};
class sub1 : visibility-mode sub_1,
class sub1 : visibility-mode super
visibility_mode sub_2
{
{
};
};
class Shape {
class Triangle: public Shape {
protected:
public:
int width, height;
int area ( ) {
public:
return (width * height/2);
void setDims (int a, int b){
}
width=a; height=b;} };
};
class Rectangle: public Shape {
class Square: public Rectangle {
public:
public:
int area ( ) {
void setDims (int a){
return (width * height);
width=a; height=a;}
}
};
};
class derivedClass : protected baseClass { …};
// Effect: ????
Here even derived constructor does not need a parameter for itself , yet it accepts
parameters for base constructor and then invokes base class constructor with
these parameters.
Class Base
{ int a;
float b;
public:
Base (int i , float j)
{
a=I;
b=j;
}
};
Class Derived : public Base
{ int x;
float y;
public:
Derived (int I, float j , int p , float q) : Base (p , q)
{ x=I;
y=j; }
};
Here derived constructor need a parameter for itself and base class also so it accepts
parameters for base constructor and itself also.
Qn What should we do to make a
private member to be inheritable in
derived class?
Ans It can be done by two methods-
1 by making the visibility mode of the
private members as public.
2 by making the visibility mode of the
private members as protected.