Sei sulla pagina 1di 20

Inheritance in C++

pm_jat@daiict.ac.in

Three types of Inheritance in C++

Public inheritance - Inheritance for extension Private inheritance and protected inheritance - Inheritance for restriction

Syntax inheritance in C++ Public Inheritance (Inheritance for Extension)


Equivalent to Manager extends Employee in Java

Public Inheritance in C++ Users View


Same as Java extends Interface of Derived class objects is super set of base class objects Interface ( Derived ) = Interface (Base) U Interface (added in derived class) Derived class objects are subtype of base class, i.e., they can substitute base class objects

Users View of Private Inheritance in C++ (Inheritance for Restriction)


Interface of Derived class objects is subset of base class objects 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

Implementation View of various Inheritance types in C++

Protected Visibility in C++


Protected members are as good as private except that these are seen by derived classes, that means Protected members can not be accessed by methods of other class, unless other class is derived class (or friend class)

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 by derived class methods Private methods are not inherited Protected are inherited and have protected visibility in derived class

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

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

Private Inheritance
If inheritance is private then B inherits x is inaccessible in it xp, and getXP() as private getX() as private

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

Protected Inheritance
If inheritance is protected then B inherits x is inaccessible in it xp, and getXP() as protected getX() as protected

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

Consider classes- Manager public inherits Employee

Object Substitution
Considering code belowEmployee 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 in such 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 for extension, i.e. have public inheritance

Reference Substitution
Consider object m of previous example, following is example of reference substitution (pointer of base type refers 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

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 be bound to Manager::getSalary(), after all x and m are two different objects of two different types ?

Questions

Thanks

Potrebbero piacerti anche