0% found this document useful (0 votes)
8 views

Online Class 5-Inheritance

Uploaded by

Soutik Dey
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Online Class 5-Inheritance

Uploaded by

Soutik Dey
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

INHERITANCE

Why Inheritance ?

Inheritance is a mechanism for


building class types from existing class types
defining new class types to be a
specialization

augmentation

of existing types

2
INHERITANCE

It is a concept in which the properties of one class are


available to another
The class that is being inherited is called as superclass
or base class
The class that inherits the properties and functions of
base class is called as subclass or derived class
Derived class inherits all the properties of base class
without making any changes to it. So it facilitates code
reuse, hence reusability is a feature of OOPs.
In principle, a derived class inherits every member of a
base class except:

its constructors and destructors


its assignment operator members (operator=)
its friends
its private members
Figure 12-1 Simple polygons
Figure 12-2 Base and derived classes
Figure 12-3 Inheritance
Note:

Constructors, destructors, nonmember


functions and assignment operators
are not inherited in a derived class. If
needed, they must be created.
Although the constructors and destructors of the base class are
not inherited as constructors and destructors in the derived
class, they are still called by the derived class's constructor.
Unless otherwise specified, the constructors of derived classes
call the default constructors of their base classes (i.e., the
constructor taking no arguments), which must exist.

Calling a different constructor of a base class is possible, using


the same syntax as to initialize member variables in the
initialization list:
Inheritance

 Single Inheritance
 A class inherits from one base class
 Multiple Inheritance
 A class inherits from multiple base classes
Types of Class Members
Accessible to
Public
derived
Protected classes and the
instances
Private
Accessible
?
to derived
not classes
accessible only

Derived
Class
Base-class access specifier determines access status of base class members inside derived class

3 types of base class access specifiers:


Private – all public, protected members of base class become
private in derived class. Inaccessible by derived class objects
Protected – all public, protected members of base class
become protected in derived class. Accessible only by
members and friends of derived class
Public – public members become public in derived class,
hence accessible by derived class objects. Protected remain
protected.
Figure 12-4 Inheritance example
Often an object from a derived class (subclass) “is
an” object of a base class (superclass)
Text Book
 C++ by Balaguruswami
Public inheritance
Points
The data member a is private –and cannot be
inherited
But it can be used in the inherited member
functions.

Check this site
http://www.cplusplus.com/doc/tutorial/in
heritance lusplus.com/doc/tutorial/
p : //w w w. c p
/htt
r/
preprocesso

You might also like