Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
pm_jat@daiict.ac.in
Public inheritance - Inheritance for extension Private inheritance and protected inheritance - Inheritance for restriction
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
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
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
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