Sei sulla pagina 1di 21

INHERITANCE

WHAT IS INHERITANCE ?
INHERITANCE IS A MECHANISM BY WHICH ONE CLASS
ACQUIRES THE PROPERTIES(I.E DATA,OPERATION
ETC…..)OF ANOTHER CLASS.
BASE CLASS
BIRD
DERIVED CLASS
HEN KIWI

BASE CLASS : THE CLASS BEING INHERITED FROM.


DERIVED CLASS : THE CLASS THAT INHERITS.
ADVANTAGES and dis advantages OF
INHERITANCE  In class hierarchy various data elements
remain unused and the memory allocated
 Reuse of exiting code. to them is not utilized.
 Adaption of program to work in similar  Invoking member functions using objects
but different situation. creates more compiler overheads.
 Extraction of commodities from  through object oriented programming is
different classes frequently propagandized as an answer
for complicated projects ,inappropriate
 Organizing of objects into hierarchies.
use of inheritance makes programs more
 Faster development time . complicated.
 Easy to maintain.
 Easy to extend .
 Memory utilization
DERIVED CLASS DECLARATION
CLASS DERIVED_CLASS_NAME : VISIBLITY_MODE BASE_CLASS_NAME
{
--------------------------------------
--------------------------------------
--------------------------------------
};

: SINGLE COLON : INDICATES THAT DERIVED CLASS NAME IS DERIVED


FROM THE BASE CLASS NAME
VISIBLITY MODE : INHERITANCE TYPE

EXAMPLE : CLASS RECTANGLE : PUBLIC SHAPE


{
--------------------------------
--------------------------------
};
IF VISIBLITY MODE IS NOT MENTIONED IT IS DEFAULT BY PRIVATE
VISIBLITY MODE
IT CAN BE PUBLIC,PRIVATE,PROTECTED.THE PRIVATE DATA OF BASE CLASS
CANNOT BE INHERITED
1. PUBLIC: IF INHERITANCE IS DONE IN PUBLIC MODE,PUBLIC MEMBER OF THE
BASE CLASS BECOME THE PUBLIC MEMBER OF DERIVED CLASS AND PROTECTED
MEMBER OF BASE CLASS BECOME THE PROTECTED MEMBER OF DERIVED CLASS.
2. PRIVATE: IF INHERITANCE IS DONE IN A PRIVATE MODE, PUBLIC AND
PROTECTED MEMBER OF BASE CLASS BECOME THE PRIVATE MEMBERS OF
DERIVED CLASS.
3. PROTECTED: IF INHERITANCE IS DONE IN A PROTECTED MODE, PUBLIC AND
PROTECTED MEMBERS OF BASE CLASS BECOME THE PROTECTED MEMBER OF
DERIVED CLASS

DERIVED CLASS DERIVED CLASS


BASE CLASS PUBLIC MODE PRIVATE MODE
PRIVATE NOT INHERITED NOT INHERITED
PROTECTED PROTECTED PRIVATE
PUBLIC PUBLIC PRIVATE
TYPES OF INHERITANCE
SINGLE INHERITANCE

MULTILEVEL INHERITANCE

MULTIPLE INHERITANCE

HIERARCHICAL INHERITANCE

HYBRID INHERITANCE
SINGLE INHERITANCE
A DERIVED CLASS WITH ONLY ONE BASE CLASS IS KNOWN AS SINGLE
INHERITANCE.THIS OCCURS WHEN ONLY BASE CLASS IS USED FOR
DERIVATION OF A DERIVED CLASS. DERIVED CLASS IS NOT USED AS BASE
CLASS.
EXAMPLE
CLASS A
{-------------
};
CLASS B : PUBLIC A
{
--------------
};
Multilevel inheritance
WHEN A CLASS IS DERIVED FROM A CLASS WHICH IS ALSO
DERIVED FROM ANOTHER CLASS, I.E. A CLASS HAVING MORE
THAN ONE PARENT CLASSES, SUCH INHERITANCE IS
CALLED MULTILEVEL INHERITANCE.
EXAMPLE
CLASS A
{--------};
CLASS B : PUBLIC A
{--------};
CLASS C : PUBLIC B
{--------};
MULTIPLE INHERITANCE
IN C++ PROGRAMMING, A CLASS CAN BE DERIVED
FROM MORE THAN ONE BASE CLASS IS MULTIPLE
INHERITANCE
EXAMPLE
CLASS A
{---------};
CLASS B : PUBLIC A
{---------};
CLASS C : PUBLIC A,PUBLIC B
{---------};
HIERARCHICAL INHERITANCE
WHEN MORE THAN ONE CLASSES ARE DERIVED FROM
A SINGLE BASE CLASS, SUCH INHERITANCE IS KNOWN
AS HIERARCHICAL INHERITANCE
EXAMPLE
CLASS A
{--------};
CLASS B : PUBLIC A
{--------};
CLASS C : PUBLIC A
{--------};
HYBRID INHERITANCE
A COMBINATION OF ONE OR MORE TYPES
OF INHERITANCE IS KNOWN AS HYBRID INHERITANCE.
EXAMPLE
CLASS A
{---------};
CLASS B : PUBLIC A
{---------};
CLASS C : PUBLIC B
{---------};
CLASS D : PUBLIC B
{---------};
Constructor and destructor
[In derived class]
Introduction

 Constructor is used to initialized the objects.


 Constructor takes same name as of the class name.
 Constructor does not return any value.
 There are three types of constructor i.e., default
,copy and parameterized constructor.
 Syntax of the constructor :-
Classname()
{….....statements;
}
constructor in derived class:

 While using constructors during inheritance, as long as the base


class constructor does not take any arguments, the derive class
need not have a constructor function.
 If a base class contains a constructor with one or more arguments,
then it is mandatory for the derived class to have a constructor
and pass the arguments to the base class constructor
 When both the derived and base class contains constructors, the
base constructor is executed first and then constructor in
derived class is executed.
 The derived class takes the responsibility of supplying the initial
values to its base class. The constructor receives the entire list of
required values as its argument and passes them on to the base
constructor in the order in which they are declared in the
derived class.
order of constructor call :

 Base class constructor are always called in the


derived class constructors. Whenever derived
class objects are created, first the base class
default constructor is executed and then the
derived class’s constructor finishes execution.

 whether derived class’s default or


parameterised constructor is called, base
class’s default constructor is always called
inside them.
 To call base class’s parameterised constructor
inside derived class’s parameterised
constructor ,we must mention it explicitly
while declaring derived class’s parameterized
constructor.
General form of defining a derived
constructor
Derived__constructor(arg1,arg2,..............argn-1,argn):.
base1(arg1),
Base2(arg2),
....................
....................
basen(argn)
{
body of the constructor;
}
 The header line of the derived constructor function contains two parts separated by a colon.
 The first part provides the declaration of the arguments that are passed to the derived class
constructor.
 The second part lists the function calls to the base class
Destructor
 Destructor is a special member function of a class that is
executed whenever the object of its class is out of class .
 A destructor will have exact same name as the class prefixed
with a tilde (~) and it can neither return a value nor can it take
any parameters.
 Syntax of destructor:-
~classname()
{
………statements;}
 If the constructors are called down the base to the derived
class the destructors are called just in the reverse order .that
is in that is from the derived lass up to the base lass
A simple program on constructor and
destructor in derived class
#include<iostream.h>
#include<conio.h> {
class A1 cout<<”\n name:”<<name;
{ cout<<”\nage:”<<age;
protected: }
char name[15]; };
int age;
A1( )
{
cout<<“Name:”; cin>>name;
cout<<“Age:”; cinn>>age;
}
~A1( )
{
class A2 : public A1
{
protected:
float height;
float weight;
A2()
{
cout<<“height:”; cin>>height;
cout<<“weight:”; cin>>weight;
}
~A2( )
{
cout<<“\nheight :”<<height;
cout<<“\nweight:”<<weight;
}
};
class A3 : public A2
{
char<<“course:”; cin>>course;
}
~A3( )
{
cout<<“\ncourse:” <<course;
}
};
int main()
{
clrscr();
A3 x;
return 0;
}
Oops activity done by:-

Debolina ray Biswas


Niveditha . s
Rachana .s .damle
Tarun raj
supriya

Potrebbero piacerti anche