Upload
suyashi-purwar
View
239
Download
0
Embed Size (px)
Citation preview
8/13/2019 Inheritance in C .1
1/20
Inheritance in C++
8/13/2019 Inheritance in C .1
2/20
Three types of Inheritance in C++
Public inheritance - Inheritance for extension
Private inheritance andprotected inheritance - Inheritance
for restriction
8/13/2019 Inheritance in C .1
3/20
Syntax inheritance in C++
Public Inheritance (Inheritance for Extension)
Equivalent to Manager extends Employee in Java
8/13/2019 Inheritance in C .1
4/20
Public Inheritance in C++ Users View
Same as Java extends
Interface of Derived class objects is super set of baseclass objects
Interface ( Derived ) =Interface (Base) U Interface (added in derived class)
Derived class objects are subtype of base class, i.e., theycan substitute base class objects
8/13/2019 Inheritance in C .1
5/20
Users View of Private Inheritance in C++
(Inheritance for Restriction)
Interface of Derived class objects is subset of base classobjects
IMP: derived class objects are not subtype of base class,therefore, they can not substitute base class objects
Note: java does not support this type of inheritance
8/13/2019 Inheritance in C .1
6/20
Implementation View of various Inheritance types
in C++
8/13/2019 Inheritance in C .1
7/20
Protected Visibility in C++
Protected members are as good as private except thatthese are seen by derived classes, that means
Protected members can not be accessed by methods ofother class, unless other class is derived class (or friend
class)
8/13/2019 Inheritance in C .1
8/20
Implementation View of
Public Inheritance in C++
Same as Java extends
Public members are inherited as public
Private fields are inherited but can not be accessed byderived class methods
Private methods are not inherited
Protected are inherited and have protected visibility inderived class
8/13/2019 Inheritance in C .1
9/20
Public Inheritance If inheritance is public then, class
B inherits as following - x is inaccessible in it
xp, and getXP() as protected
getX() as public
8/13/2019 Inheritance in C .1
10/20
Private Inheritance - Inheritance for restriction
Public members are inherited as private
Private fields are inherited but can not be accessed by
derived class methods
Private methods are not inherited
Protected are inherited as private
8/13/2019 Inheritance in C .1
11/20
Private Inheritance
If inheritance is private then B
inherits x is inaccessible in it
xp, and getXP() as private
getX() as private
8/13/2019 Inheritance in C .1
12/20
Protected Inheritance Inheritance for restriction
Public members are inherited as protected in derived class
Private fields are inherited but can not be accessed by
derived class methods
Private methods are not inherited
Protected are inherited as protected
8/13/2019 Inheritance in C .1
13/20
Protected Inheritance
If inheritance is protected then B
inherits x is inaccessible in it
xp, and getXP() as protected
getX() as protected
8/13/2019 Inheritance in C .1
14/20
Type Substitution in C++
In C++, type substitution has two variations-
Object Substitution: When object of subtype
substitutes objects of super-type
Reference Substitution: When pointer (or reference) of
base type refers to objects of subtype
8/13/2019 Inheritance in C .1
15/20
Consider classes- Manager public inherits Employee
8/13/2019 Inheritance in C .1
16/20
Object Substitution
Considering code below-
Employee e("Sumit Mehta", 45000.00);Manager m("Anil",50000.00,"Marktg");
m.setBonus( 5000 );
Employee x = m;
It is object substitution- however there is loss of data insuch assignments, known as object slicing problem in
C++ ? Also, guess, what will happen if you write e = m ?
Note: Type Substitution works only when you have inheritance forextension, i.e. have public inheritance
8/13/2019 Inheritance in C .1
17/20
Reference Substitution
Consider object m of previous example, following is
example of reference substitution (pointer of base typerefers to objects of subtype) -
Employee* x = &m;
In this case, there is no object slicing, since object is
intact, only the thing, pointer is of super type same as in
Java
8/13/2019 Inheritance in C .1
18/20
Method Binding
In case of Object Substitution, it is always static
In fact object substitution is a copying data from subtype
object to super type object
In the example here, x.getSalary() will be bound to
Employee::getSalary(), where as m.getSalary would bebound to Manager::getSalary(), after all x and m are two
different objects of two different types ?
8/13/2019 Inheritance in C .1
19/20
Questions
8/13/2019 Inheritance in C .1
20/20
Thanks