Download as pdf or txt
Download as pdf or txt
You are on page 1of 46

Inheritance: Extending classes

Contents.
1)Introduction
2)Need for inheritance
3)Types of inheritance
4)Definition of derived and base classes.
5)Visibility modes
6)Inheritance and constructors and destructors.
7)Virtual base Classes
8)Nesting of classes
9)Relation between classes
Introduction.

The capability of one class to inherit the


properties from another class is called
inheritance.
The class whose properties are inherited , is called Base class
or Super class and the class that inherits these properties is
called Derived class or Sub class
The most important advantage of inheritance
is code reusability.
Once a base class is written and debugged, it
can be used in various situations without
having to redefine it or rewrite it. Reusing
existing code saves time, money, and efforts
and increases a program’s reliability.
Inheritance: It is the mechanism of
deriving new class from existing class. It
provides the idea of reusability.

A Base , super,parent class

B derived , sub, child class


=> NEED FOR INHERITANCE:
1. CAPABILITY TO EXPRESS THE INHERITANCE
RELATIONSHIP: This ensures the closeness with the
real-world models i.e. it lets you generates a
model that is closer to the real-world.
• Ex: The class car inherits from another class
automobiles which itself inherits from another
class vehicles.
2. REUSABILITY:
• The most important advantage of inheritance is
reusability.
• Inheritance allows the addition of new features to
an existing class without modifying it.
• A new class (derived class) can be derived from an
existing class and add new features to it.
3. TRANSITIVE NATURE OF INHERITANCE:
• If a class B inherits properties of another
class A, then all subclasses of B will
automatically inherit the properties of A.
• This property is called transitive nature of
inheritance.
• The benefit of this nature is that if any
correction takes place in class A will also be
reflected in all the classes that are inherited
from it.
ADVANTAGES OF REUSABILITY:
• Once a base class is written and debugged, it
can be used in various situations without
having to redefine it or rewrite it. Reusing
existing code saves time , money and efforts
and increases a program’s reliability. The Main
advantages of reusability are
• Faster development time
• Easier maintenance
• Easy to extend
Types of inheritance

Single level Multilevel Multiple


A A B
A
B

B
C
C

Hybrid
Hierarchical
A C
A
B B
A B
B C D
C
C
E F G H I J
Types of inheritance.

1)Single Inheritance: When a subclass


is inherited from only one base class it is known
as Single Inheritance.

X BASE CLASS

Y DERIVED CLASS
2) MULTIPLE INHERITANCE:
When a derived class is inherited from multiple
base classes is known as Multiple Inheritance

BASE CLASS BASE CLASS

X Y

Z DERIVED CLASS
3) HIERARCHICAL INHERITANCE:
When many derived classes are inherited from
a single base class it is called Hierarchical
Inheritance

BASE CLASS
Z

W x Y

DERIVED CLASSES
4) MULTILEVEL INHERITANCE:
When a subclass or derived class is inherited
from another derived class is known as
Multilevel Inheritance

X Base class
of Y
Derived class
Y of X
Base class of Z

Z Derived class of Y
5) HYBRID INHERITANCE:
Hybrid Inheritance combines two or more forms of Inheritance.
i.e. Inheritance-When a subclass inherits
from multiple base classes and all of its
base classes inherit from a single base
class, this form of inheritance is called
hybrid inheritance

W
X Y Z

X Y
A

Z B C
a b
ACCESSIBILITY OF BASE CLASS MEMBERS.

ACCESS Accessible Accessible Accessible


SPECIFIER from own from derived from objects
class class outside
class

PUBLIC Yes Yes Yes

PROTECTED Yes Yes No

PRIVATE Yes No No
Definition of derived and base classes.
The Visibility Mode controls the visibility and
availability of inherited base class members in the
derived class.

1)Single Inheritance: SYNTAX of Defining of


derived class:
class derived-class:<visibility mode> <base-class>
Example: class Sub: public Super
{
};
=>The derived class has access privilege only to the
non private members of the base class.
=>If no visibility mode is specified , then by
default the visibility mode is considered as
Private.
2)Multiple Inheritance:
class derived_class:vis_mode base1,vis_mode base2
{
.
};
Class Sub: public SuperA,private SUperB
{
//Members
};
• 3)Multilevel Inheritance:
SYNTAX
• Class derived1:visibility_mode base
• { .
• .
• }
• Class derived2:visibilty_mode derived1
• { .
• .
• };
Visibility Modes.
The visibility modes basically control the access
specifier to be for inheritable members of base
class, in derived class.

Role of access specifiers in inheritable classes.

Inheritable Inheritable Inheritable


Visibility Mode
public members protected private members
become members become become

Public Public Protected

Protected Protected Protected

Private Private Private


1) Public visibility mode:
The public derivation means that the
derived class can access the public
members of the base class but not the
private members of the class. 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

X private Y public
Y public Z protected
Z protected
2) Private visibility mode:The private derivation
means the derived class can access the public and
protected members of the class privately. i.e public
and protected members of the base class become private
members of the derived class. They cannot be inherited
further if the derived happens to be the base class of
any other class.
Class Super
{
Private: int x;void check(void);
Public: int y;void display(void);
Protected: int z; void getval(void);
};
Class Sub: Public Super
{
private: int s; void init(void);
pubilc:int b; readit(void);
Protected: int c; writeit(void);
};
3) Protected visibility mode: The protected derivation
of a class means that the derived class can access the
public and protected members of the base class
protectedly.i.e the public and protected members of
the base class become protected members of the derived
class.
Class Super
{
};
Class Sub: protected Super
{
};
Deriving publicly is a way of saying “ is a type of”.
=> Inheritance and the Base class
1. Member intended to be inherited and at the same
time intended to be available to every function ,
even to non-members ,should be declared as public
members in the base class.
2. Member intended to be inherited but not intended
to be public, should be declared as protected
members in the base class.
3. Member not intended to be inherited should be
declared as private members in the base class.
=> The significance of visibility mode:
• INHERITANCE AND CONSTRUCTORS AND
DESTRUCTORS:
When an object of a derived class is created,
the program first calls the constructor of base
class, then the constructor for the derived
class. This is because the constructor of derived
class may build upon data members from base
class; hence base class object has to be
constructed first. When an object of a derived
class expires , first the derived class destructor
is invoked , followed by the base class
destructor
.
class Super
{ .
. //same as before
};
Class Sub: public Super
{
};
int main()
{
Sub Ob1;
}
While Ob1 is created, first the constructor
Super:: Super( ) is invoked and then Sub::sub( ) is
invoked. Also when ob1 expires, at the main ( )
,firstly Sub::~Sub() is invoked and then Super::
~Super( ) is invoked.
Sometimes , the base class contractors require
arguments to construct their object.
In such situation, it is the responsibility of the
derived class constructor to pass those arguments
required by the corresponding base class.
Derived:: Derived(type x, type y,….): Base(x,y)
{

} Then the derived


class constructor
invoked base class
derived class constructor by
constructor accepts passing appropriate
argument for itself arguments that it
as well as for its received for base
base class constructor
class Base { int x;
public:
Base(int i)
{ x = i;
cout << "Base Parameterized Constructor"; } };
class Derived : public Base
{ int y;
public:
Derived(int j) : Base(j)
{ y = j;
cout << "Derived Parameterized Constructor"; } };
int main()
{ Derived d(10) ;
cout << d.x ; // Output will be 10
cout << d.y ; // Output will be 10

}
=>Passing Arguments through to a Base class
Constructor
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)
{ }
}; Even if thederived
constructor does not need a
parameter for itself , yet
it accepts parameters for it
base class
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)
{ }
}; the derived constructor is
accepting parameters for
itself as well as for its
base class
Note:
1.The private members of the base class are
visible in the derived class but they are not
directly accessible.
int test;
Class Base {
int test;
public: void getit()
{ cin>>test;
}
};
Class Derived: public Base
{
public: void check()
{
test++;//not allow
};
Abstract class: A class that serves only as
a base class from which other classes
are derived , but no objects of this
base class type exist, is known as
Abstract class.
Making a private Member Inheritable:
(a). By making the visibility mode of
the private member as public.
(b). By making the visibility mode of
the private member as protected.
=>
Not Inheritance Private
Base class
Protected
Public
Private Private
Protected Protected
Public Public
Private
Protected
Public
Constructors in multiple inheritance:
The base classes are constructed in the
order in which they appear in the
declaration of the derived class.
 The base class constructors are called
and executed before executing the
statements in the body of the derived
constructor.
VIRTUAL BASE CLASS: An element of
ambiguity can be introduced into a c++
program when multiple base classes are
inherited.
Base

D1 D2

D3
• #include<iostream.h>
void main()
Class Base{ public:int a
{
};
D3 ob;
Class D1:public Base
ob.a=25; //ambiguous
{
ob.b=50;
public: int b;
};
ob.c=75;
Class D2:public Base ob.total=ob.a+ob.b+ob.c;
{ public:int c; //ambiguous
}; cout<<ob.a<<”\t”<<ob.b<<“\t”<<
Class D3:public D1, public D2 ob.c<<“\t”<<ob.total<<“\n”;
{ }
public: int total;
};
1.Solved the above problem using scope resolution
operator.

void main()
{
D3 ob;
Ob.D1::a=25; //SCOPE resolved , used D1’s a
ob.b=50;
ob.c=75;
ob.total=ob.D1::a+ob.b+ob.c;
cout<<ob.D1::a<<”\t”<<ob.b<<“\t”<<ob.c<<“\t”<<ob.total<<“\n”;
}
2.Solved the above problem using virtual Base class.
The dreaded diamond refers to a class structure in which
a particular class appears more than once in a class’s
inheritance hierarchy.
Base

D1 D2

D3

The use virtual keyword in the classes just below the


top of the dreaded diamond and not in the join class.
Base

virtual D1 virtual D2

D3
#include<iostream.h>
void main()
Class Base{ public:int a
{ clrscr();
};
D3 ob;
Class D1:virtual public Base
ob.a=25; //Now unambiguous
{
ob.b=50;
public: int b;
};
ob.c=75;
Class D2:virtual public Base ob.total=ob.a+ob.b+ob.c;
{ public:int c; //unambiguous
}; cout<<ob.a<<”\t”<<ob.b<<“\t”<
Class D3:public D1, public D2 <ob.c<<“\t”<<ob.total<<“\n”;
{ }
public: int total;
};
OVERRIDING AND HIDING A BASE CLASS
METHOD IN A DERIVED CLASS

•When a method in a derived class has the same


name, arguments in numbers and types and the
same return type the method says to override
that base class method.
=> Shadowing/Overriding Base Class
Functions in Derived Class: When a
derived class function has the same name
as that if its base class member
function(i.e. redefined in derived class),
the derived class member function
shadows/hides the base’s class inherited
function and this situation is known as
function OVERRIDING.
=> Unveiling shadowed Inherited Members:
A using declaration is a way or make
available pre-defined members to the
current scope.
Syntax:
using <parent class name>::<Member name>
Using A::f1;
=>NESTING OF CLASSES: When a class contains
objects of another class type as its member or when
a class contains another class within its memory, it is
known as nesting of classes. it is also known as
containership or containment or aggregation.
For Example:
Class X{…..};
Class Y {….};
Class Z{
X Ob1;//object of X class
Y Ob2; //object of Y class
};
Relationship between classes
1)IS-A Relationship- When a class inherits from
another class it is known as a IS-A relationship.
2)HAS-A Relationship- When a class contains
object of another class type it is known as a
HAS-A relationship.
The class having HAS-A relationship with other
class has the Ownership of the contained object.
Ownership define the responsibility for the
creation and the destruction of an objet.
3)HOLDS-A Relationship- It is similar to HAS-A
relationship but ownership is missing in HOLDS-
A relationship. A class that indirectly contains
another object .i.e. via pointer or reference .
Summary.
1. Inheritance is the capability of one class to
inherit the properties of another class.
2. Inheritance supports reusability of code and is
able to simulate the transitive nature of real life
objects.
3. A class from which another class is inheriting its
properties is called base class and the class
inheriting properties is known as a sub class or
derived class.
4. When a class inherits from a single class it is
known as single inheritance.
5. When a class inherits from multiple base classes it
is known as multiple inheritance.
6. When several classes inherit from the same class it
is hierarchical inheritance.
7. When a subclass is the base class of another
class it is known as multilevel inheritance.
5. When a class inherits from multiple base classes
and all of its base classes are subclasses of the
same class it is hybrid inheritance.
6. A class can derive itself publicly, privately
and protectedly.
7. In the publicly derived class the public and
protected members of the base class remains same.
8. In privately derived class the public and the
protected members of the base class become
private members.
9. In the protectedly derived class the public and
the protected members of the class become
protected members.
10. The derived class constructor is responsible
for invoking (and passing arguments to) the base
class constructor.
11. The derived class can directly access only the
public and protected members of the base class.
12. To make the private member of a class
inheritable declare it under protected section of
the base class.
13. When a class inherits from more than one base
class this is called multiple inheritance.
14. When a derived class and its base class have
common ancestor then ambiguity may arise as the
derived class contains multiple copies of common
ancestor. This can be resolved either by using
scope resolution operator :: or by declaring the
common ancestor as virtual.

You might also like