inheritance : extending classes. rickshaw cart bus car pulled vehicles inheritance inheritance...
TRANSCRIPT
INHERITANCE : INHERITANCE : Extending ClassesExtending Classes
RickshawcartBusCar
Pulled Vehicles
InheritanceInheritance
Vehicles
Inheritance is the capability of one class of Inheritance is the capability of one class of things to acquire properties or capabilities things to acquire properties or capabilities from another class of things.from another class of things.
For example:-For example:-
Vehicles
Automobiles
Conceptual Examples of Conceptual Examples of Hierarchical ClassesHierarchical Classes
Animal
PersonReptile Bird
Man Woman
MotherFather
Person
EngineerStudent
UGGrad
Lawyer
Class Inheritance in C++Class Inheritance in C++
Inheritance in c++ allows us to create new Inheritance in c++ allows us to create new classes which are derived from older classes which are derived from older
classes. A class is a group of objectsclasses. A class is a group of objects that share common properties & that share common properties & relationship.relationship.
The derived classes are The derived classes are called subclassescalled subclasses or simply or simply derived classesderived classes
The older classes areThe older classes are superclassessuperclasses oror parent classesparent classes oror base classesbase classes
A derived class automatically includes some A derived class automatically includes some of its parent's members, and can have of its parent's members, and can have additional ones. additional ones.
base
subclass1 subclass2
Notation:
Staff
Need for inheritanceNeed for inheritance
1.1. Capability to express Capability to express the inheritance the inheritance relationship which relationship which makes it ensure the makes it ensure the closeness with the real closeness with the real world models. world models. For example: when we For example: when we want to maintain a want to maintain a database of an database of an organisation, the organisation, the database is divided database is divided into number of classes into number of classes whose relationships whose relationships are as followsare as follows
empcodenamebasic
Clerkgrade
Officerdesignation
Need for inheritanceNeed for inheritance
2.2. It gives the idea of re-It gives the idea of re-usability. It allows the usability. It allows the addition of additional addition of additional features to an existing features to an existing class without modifying it. class without modifying it. One can desire a new class One can desire a new class (sub-class) from an (sub-class) from an existing one (super-class) existing one (super-class) and add new features to a and add new features to a sub-class.sub-class.
Studentsturoll
stunamestuaddress
Graduatestustuyear
B
Need for inheritanceNeed for inheritance
3.3. It has It has transitive nature . transitive nature . If a class A inherits If a class A inherits properties of properties of another class B , another class B , then all subclasses then all subclasses of class A will of class A will automatically automatically inherits properties inherits properties of class B.of class B.
A
C ED
Advantages of inheritanceAdvantages of inheritance
1.1. Increase reuse and software Increase reuse and software qualityquality
Programmers reuse the base classes Programmers reuse the base classes instead ofinstead of
writing new classes.writing new classes. Using well-tested base classes helps reduce Using well-tested base classes helps reduce
bugs in applications that use them.bugs in applications that use them. Reduce object code size.Reduce object code size.
2.2. Enhance extensibility Enhance extensibility
Types of inheritanceTypes of inheritance
Inheritance may take place in many Inheritance may take place in many forms, which are as follows:-forms, which are as follows:-
1.1. Single InheritanceSingle Inheritance
2.2. Multiple InheritanceMultiple Inheritance
3.3. Hierarchical InheritanceHierarchical Inheritance
4.4. Multilevel InheritanceMultilevel Inheritance
5.5. Hybrid InheritanceHybrid Inheritance
1.1. Single InheritanceSingle Inheritance
When a sub-class is inherited only When a sub-class is inherited only from one base-class.from one base-class.
e.g.e.g. base class base class
derived classderived class
X
Y
2.2. Multiple inheritanceMultiple inheritance When a sub class is inherited from When a sub class is inherited from
multiple base classes.multiple base classes.
e.g.e.g. Base classesBase classes
derived class derived class
There are ‘x’ and ‘y’ are super classes There are ‘x’ and ‘y’ are super classes and sub class ‘z’ inherits the properties and sub class ‘z’ inherits the properties from both super classes.from both super classes.
x
z
y
3.3. Hierarchical Inheritance Hierarchical Inheritance When many sub-classes are inherited from a single super-When many sub-classes are inherited from a single super-
class, is known as hierarchical inheritance.class, is known as hierarchical inheritance.
base classbase class
derived-derived-classesclasses
A
DCB
4.4. Multilevel InheritanceMultilevel Inheritance When a sub-class is inherited from a When a sub-class is inherited from a
class that itself is inherited from another class that itself is inherited from another class, is known as multilevel inheritance.class, is known as multilevel inheritance.
Base class of YBase class of Y sub class of Xsub class of X
Base class of ZBase class of Z
Sub class of YSub class of Y
x
y
z
5.5. Hybrid InheritanceHybrid Inheritance When a sub-class is inherited from multiple base-When a sub-class is inherited from multiple base-
classes and all of its base-classes are inherited classes and all of its base-classes are inherited from a single base-class, is called hybrid from a single base-class, is called hybrid inheritance.inheritance.
e.g.e.g.
Base class of YBase class of Y
sub class of Xsub class of X
Base class of ZBase class of Z
Sub class of Y and Sub class of Y and XX
w
xy
z
Access modes or visibility Access modes or visibility modes of inheritancemodes of inheritance
We have seen two access modes in C+We have seen two access modes in C++ classes: public and private+ classes: public and private Public members are directly accessible by Public members are directly accessible by
users of the classusers of the class Private members are NOT directly Private members are NOT directly
accessible by users of the class, not even accessible by users of the class, not even by inheritors by inheritors
There is a 3There is a 3rdrd access mode: protected access mode: protected Protected members are directly accessible Protected members are directly accessible
by derived classes but not by other usersby derived classes but not by other users
Syntax for Inheritance: Syntax for Inheritance: Single InheritanceSingle Inheritance
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)
For ExampleFor Example
class Sub : public Superclass Sub : public Super
{{private:private:
public:public:
protected:protected:
};};
Syntax for Inheritance : Syntax for Inheritance : Multiple InheritanceMultiple Inheritance
class derivedClass_name : visibility mode baseClass_name1, visibility mode baseClass_name2 , visibility mode baseClass_name3
{ private : public:
protected:};
For ExampleFor Example
class Sub : public SuperA , private class Sub : public SuperA , private SuperBSuperB
{{private:private:
public:public:
protected:protected:
};};
Access or Visibility mode of Access or Visibility mode of inheritanceinheritance
The access mode or visibility mode of The access mode or visibility mode of derived class specifies whether the derived class specifies whether the features of the base class are features of the base class are privately , publicly , protected derived.privately , publicly , protected derived.
The visibility modes basically control The visibility modes basically control the access-specifier of inheritable the access-specifier of inheritable members of base class , in the derived members of base class , in the derived class.class.
Access or Visibility mode of Access or Visibility mode of inheritanceinheritance
Visibility of inherited Visibility of inherited base class members in base class members in derived class:-derived class:-
Visibility Visibility mode is mode is
In derived classIn derived class
PUBLIC MEMBERS PUBLIC MEMBERS OF BASE CLASS OF BASE CLASS becomebecome
In derived class In derived class PROTECTED PROTECTED MEMBERS OF MEMBERS OF BASE CLASS BASE CLASS becomebecome
Private Private Members of Members of base class are base class are not directly not directly accessible to accessible to derived class.derived class.
PublicPublic
ProtectedProtected
privateprivate
PublicPublic
ProtectedProtected
privateprivate
ProtectedProtected
ProtectedProtected
privateprivate
The Public visibility modeThe Public visibility mode
It means that the derived class can access the It means that the derived class can access the public and protected members of the base class public and protected members of the base class but not the private members.but not the private members.
With publicly derived class , the public members With publicly derived class , the public members of the base class become the public members of of the base class become the public members of the derived class and the protected members of the derived class and the protected members of the base class become the protected members of the base class become the protected members of the derived class. the derived class.
The Public visibility modeThe Public visibility mode
Class SuperClass Super Class SubClass Sub
Private sectionx Check()
Public section
Protected section Protected section
Public section
Private section
c
y
a
z
y
z
b readit()display()
init()
getval()
display()
writeit()
getval()
Inh
eri
ted f
rom
base
cla
ss
The Private visibility modeThe Private visibility mode
It means that the derived class can access It means that the derived class can access the public and protected members of the the public and protected members of the base class privately.base class privately.
With privately derived class , the public With privately derived class , the public members and the protected members of members and the protected members of the base class become private members of the base class become private members of the derived class. the derived class.
The Private visibility The Private visibility modemode
Class SuperClass Super Class SubClass Sub
Private section
x Check()
Public section
Protected section
Protected section
Public section
c
z
y
b readit()
getval()
display()
writeit()
y Display()
z getval()
a Init()
Private section
Inhe
rited
fro
m b
ase
clas
s
The Protected visibility The Protected visibility modemode
It means that the derived class can access It means that the derived class can access the public and protected members of the the public and protected members of the base class privately.base class privately.
With protectedly derived class , the public With protectedly derived class , the public members and the protected members of members and the protected members of the base class become protected members the base class become protected members of the derived class. of the derived class.
The Protected visibility The Protected visibility modemode
Class SuperClass Super Class SubClass Sub
Private sectionx
Check()
Public sectiony display()
Protected sectionz
Check()
Getval()
Private sectiona Init()
Public sectionb readit()
Protected section
y
z
display()
Getval()
c Writeit()
Inh
etr
ited
fro
m b
ase
cla
ss
Inheritance and Base ClassInheritance and Base Class While defining a base class following things While defining a base class following things
should be kept in mind-should be kept in mind-
1.1. Members intended to be inherited and at the Members intended to be inherited and at the same time intended to be available to every same time intended to be available to every function , even to non members , should be function , even to non members , should be declared public members in base class.declared public members in base class.
2.2. Members intended to be inherited but not Members intended to be inherited but not intended to be public , should be declared as intended to be public , should be declared as protected.protected.
3.3. Members not intended to be inherited should be Members not intended to be inherited should be declared as private.declared as private.
Accessibility of Base Class Accessibility of Base Class MembersMembers
Access Specifiers
Accessible from own class
Accessible from derived class
Accessible from objects outside class
Public Yes Yes Yes
Protected Yes Yes No
private Yes No No
1.1. Single InheritanceSingle Inheritance
When a sub-class is inherited only from one base-When a sub-class is inherited only from one base-class.class.
e.g.e.g. base classbase class
derived classderived class Syntax:-Syntax:-
class subclass_name : visibility-mode baseclass_nameclass subclass_name : visibility-mode baseclass_name
{{
//// members of derived classmembers of derived class
};};
X
Y
2.2. Multiple inheritanceMultiple inheritance
When a sub class is inherited from multiple base classes.When a sub class is inherited from multiple base classes.
e.g.e.g. Base classesBase classes
derived class derived class
There are ‘x’ and ‘y’ are super classes and sub class ‘z’ inherits the There are ‘x’ and ‘y’ are super classes and sub class ‘z’ inherits the properties from both super classes.properties from both super classes.
Syntax:-Syntax:-
class subclass : visibility mode superclass1, visibility mode class subclass : visibility mode superclass1, visibility mode superclass2……superclass2……
{{
// members of derived class.// members of derived class.
};};
x
z
y
3.3. Hierarchical Inheritance Hierarchical Inheritance
When many sub-classes is inherited from a single super-class, is known as When many sub-classes is inherited from a single super-class, is known as hierarchical inheritance.hierarchical inheritance.
e.g.e.g.Base-classBase-class
derived-classesderived-classes
Syntax:-Syntax:-class sub_class1: visibility-mode super-classclass sub_class1: visibility-mode super-class{{
//members of derived class sub_class1//members of derived class sub_class1};};class sub_class2 : visibility-mode super-classclass sub_class2 : visibility-mode super-class{{
//members of derived class sub_class2//members of derived class sub_class2};};
A
DCB
4.4. Multilevel InheritanceMultilevel Inheritance When a sub-class is inherited from a class that itself is inherited from When a sub-class is inherited from a class that itself is inherited from
another class, is known as multilevel inheritance.another class, is known as multilevel inheritance. e.g.e.g. Base class of YBase class of Y
sub class of Xsub class of XBase class of ZBase class of Z
Sub class of YSub class of Y Syntax:-Syntax:-
class sub_class1: visibility-mode super_classclass sub_class1: visibility-mode super_class{{//members of derived class of super_class and base class of //members of derived class of super_class and base class of sub_class2sub_class2};};class sub_class2 : visibility-mode sub_class1class sub_class2 : visibility-mode sub_class1{{//members of derived class of sub_class1//members of derived class of sub_class1
};};
x
y
z
5.5. Hybrid InheritanceHybrid Inheritance When a sub-class is inherited from multiple base-When a sub-class is inherited from multiple base-
classes and all of its base-classes are inherited classes and all of its base-classes are inherited from a single base-class, is called hybrid from a single base-class, is called hybrid inheritance.inheritance.
e.g.e.g. Base class of YBase class of Y
sub class of Xsub class of X
Base class of ZBase class of Z
Sub class of YSub class of Y
Syntax:-Syntax:-
w
x y
z
class super{
};class sub1 : visibility-mode super{
} ;
class sub_2 : visibility-mode super{
};class sub1 : visibility-mode sub_1,visibility_mode sub_2{
} ;
Example of Inherited Example of Inherited ClassesClasses
class Shape { protected: int width, height; public: void setDims (int a, int b){
width=a; height=b;} };
class Rectangle: public Shape { public: int area ( ) {
return (width * height); }};
class Triangle: public Shape { public: int area ( ) { return (width * height/2); }};
class Square: public Rectangle { public: void setDims (int a){
width=a; height=a;}};
More on Inheritance SyntaxMore on Inheritance Syntax
class derivedClass : protected baseClass { …};// Effect: ????
class derivedClass : private baseClass { …};// Effect: ????
Inheritance and Constructors Inheritance and Constructors and Destructorsand Destructors
When an object of derived class is created , first the base class When an object of derived class is created , first the base class constructor is called , followed by the derived class constructor. constructor is called , followed by the derived class constructor. When an object of a derived class expires , first the derived class When an object of a derived class expires , first the derived class destructor is invoked , followed by the base class destructor.destructor is invoked , followed by the base class destructor.
For example: For example: Class x{ Class x{
};};Class y:public xClass y:public x
{ { };};Void main()Void main(){ y ob1; };{ y ob1; };
Qn : What will be the order of constructor and destructor invocation?Qn : What will be the order of constructor and destructor invocation?
AnswerAnswer
Constructor xConstructor x
Constructor yConstructor y
Destructor yDestructor y
Destructor xDestructor x
Passing Arguments to the Base Passing Arguments to the Base class constructorclass constructor
Class BaseClass Base {{ int a;int a;
float b;float b;public:public:
Base (int i , float j)Base (int i , float j){{
a=I;a=I;b=j;b=j;
}}};};Class Derived : public BaseClass Derived : public Base{{
public:public: Derived ( int p , float q) : Base (p , q)Derived ( int p , float q) : Base (p , q)
{{ }}};};
Here even derived constructor does not need a parameter for itself , yet it accepts 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 parameters for base constructor and then invokes base class constructor with these parameters.these parameters.
Passing Arguments to the Base Passing Arguments to the Base class constructor and itselfclass constructor and itself
Class BaseClass Base {{ int a;int a;
float b;float b;public:public:
Base (int i , float j)Base (int i , float j){{
a=I;a=I;b=j;b=j;
}}};};Class Derived : public BaseClass Derived : public Base{{ int x;int x;
float y;float y;public:public: Derived (int I, float j , int p , float q) : Base (p , q)Derived (int I, float j , int p , float q) : Base (p , q)
{ x=I;{ x=I; y=j; }y=j; }
};};
Here derived constructor need a parameter for itself and base class also so it accepts Here derived constructor need a parameter for itself and base class also so it accepts parameters for base constructor and itself also.parameters for base constructor and itself also.
Qn What should we do to make a Qn What should we do to make a private member to be inheritable in private member to be inheritable in derived class?derived class?
Ans It can be done by two methods-Ans It can be done by two methods- 1 by making the visibility mode of the 1 by making the visibility mode of the
private members as public.private members as public. 2 by making the visibility mode of the 2 by making the visibility mode of the
private members as protected.private members as protected.