Download presentation
Presentation is loading. Please wait.
Published byΠλειόνη Γιαννόπουλος Modified over 6 years ago
1
The Diamond Problem Its Solution Copyright © 2017 Curt Hill
2
Multiple Inheritance Again
We see that in multiple inheritance we get all the properties and methods of two or more ancestral classes This turns out to be a problem when multiple ancestors have the same property or method The worst case of this is two derivations from the same hierarchy Copyright © 2017 Curt Hill
3
Recall this Hierarchy person set_name employee student set_wage
set_major grad set_degree Copyright © Curt Hill
4
The Diamond Problem person set_name employee student set_wage
set_major Student_worker Copyright © Curt Hill
5
The Issues The Student Worker class now has two copies of name, birthdate and gender It also has multiple copies of the methods How is this solved? In one sense this is a design problem The wage part should come from elsewhere Let’s ignore that and look at another way Copyright © 2017 Curt Hill
6
Virtual Inheritance In a recent version of C++ standards virtual inheritance was introduced for just this problem This changes the student and employee classes Student was: class student: | public person { becomes: class student: | public virtual person { Copyright © 2017 Curt Hill
7
Commentary We would also need a virtual after employee
However, student_worker would look like normal multiple inheritance Copyright © 2017 Curt Hill
8
Implementation If the student and employee do not use virtual inheritance then the student worker has two copies of the person With virtual inheritance in student worker we only get one of these We also get the extras from student and employee Copyright © 2017 Curt Hill
9
Fix without need? The diamond problem is largely used to show the problems of multiple inheritance Virtual inheritance fixes this As mentioned before is this example really a design problem? Moreover, virtual inheritance does nothing about name clashes not caused by problems similar to the diamond problem Copyright © 2017 Curt Hill
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.