0% found this document useful (0 votes)
10 views42 pages

Inheritance NEW

Inheritance in programming allows one class to acquire properties and capabilities from another, facilitating the creation of derived classes from base classes. It enhances code reusability, extensibility, and reflects real-world relationships through various forms of inheritance such as single, multiple, hierarchical, multilevel, and hybrid. Access specifiers like public, protected, and private control the visibility of inherited members in derived classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views42 pages

Inheritance NEW

Inheritance in programming allows one class to acquire properties and capabilities from another, facilitating the creation of derived classes from base classes. It enhances code reusability, extensibility, and reflects real-world relationships through various forms of inheritance such as single, multiple, hierarchical, multilevel, and hybrid. Access specifiers like public, protected, and private control the visibility of inherited members in derived classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 42

 Inheritance is the capability of one class of

things to acquire properties or capabilities


from another class of things.
 For example:-

Vehicles
Vehicles

Pulled Vehicles
Automobiles

Car Bus cart Rickshaw


 The mechanism that allows us to
extend the definition of a class without
making any physical changes to the
existing class is inheritance.
 Inheritance lets you create new classes
from existing class. Any new class that
you create from an existing class is
called derived class; existing class is
calledbase class.
Animal Person

Reptile Person Bird


Lawyer Student Engineer

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

 A derived class automatically includes some


of its parent's members, and can have
additional ones.
Staff
1. Capability to express
the inheritance empcode
relationship which name
makes it ensure the basic
closeness with the real
world models.
For example: when we
want to maintain a Clerk Officer
database of an grade designation
organisation, the
database is divided
into number of classes
whose relationships
are as follows
2. It gives the idea of re-
Student
usability. It allows the
sturoll
addition of additional
stuname
features to an existing stuaddress
class without modifying it.
One can desire a new class
(sub-class) from an existing
Graduatestu
one (super-class) and add stuyear
new features to a sub-
class.
3. It has B
transitive nature .
If a class A inherits A
properties of
another class B , C D E
then all subclasses
of class A will
automatically
inherits properties
of class B.
1. Increase reuse and software
quality
 Programmers reuse the base classes instead

of
writing new classes.
 Using well-tested base classes helps reduce

bugs in applications that use them.


 Reduce object code size.

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

There are ‘x’ and ‘y’ are super classes


and sub class ‘z’ inherits the properties
from both super classes.
 When many sub-classes are inherited from a single super-
class, is known as hierarchical inheritance.

 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()

Inherited from base class


Public section
Public section y display()
y display() b readit()

Protected section Protected section


z getval()
z getval() c writeit()
It means that the derived class can access
the public and protected members of the
base class privately.
With privately derived class , the public
members and the protected members of the
base class become private members of the
derived class.
Class Super Class Sub
Private section

Inherited from base class


Private section
x Check() y Display()
z getval()
Public section
display() a Init()
y

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

Private section Private section


x Check() a Init()

Inhetrited from base class


Public section
b readit()
Public section
y display() Protected section

y display()
z Getval()
Protected section
Check() c Writeit()
z Getval()
 While defining a base class following things
should be kept in mind-

1. Members intended to be inherited and at the


same time intended to be available to every
function , even to non members , should be
declared public members in base class.
2. Members intended to be inherited but not
intended to be public , should be declared as
protected.
3. Members not intended to be inherited should be
declared as private.
Access Accessible Accessible Accessible
Specifiers from own from derived from objects
class class outside class

Public Yes Yes Yes

Protected Yes Yes No

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: ????

class derivedClass : private baseClass { …};


// Effect: ????
When an object of derived class is created , first the base class
constructor is called , followed by the derived class constructor. When
an object of a derived class expires , first the derived class destructor
is invoked , followed by the base class destructor.
For example:
Class x{
};
Class y:public x
{
};
Void main()
{ y ob1; };

Qn : What will be the order of constructor and destructor invocation?


Constructor x
Constructor y
Destructor y
Destructor x
 Class Base
 { int a;
float b;
public:
Base (int i , float j)
{
a=I;
b=j;
}
};
Class Derived : public Base
{
public:
Derived ( int p , float q) : Base (p , q)
{ }
};

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.

You might also like