โ Back to Inheritance
Question 450
Diamond Problem With Virtual Inheritance
Diamond Problem With Virtual Inheritance
When two classes inherit from the same base and a fourth class inherits from both, you get the "diamond problem": two copies of the base and ambiguous member access.virtualinheritance fixes this by keeping a SINGLE shared base subobject.Person(already written) hasstring role() constreturning "person". CreateStudentandEmployeethat each inherit fromPersonusing VIRTUAL inheritance, andTeachingAssistantthat inherits from bothStudentandEmployee. Because of virtual inheritance,role()on aTeachingAssistantis unambiguous. The program prints: person Do NOT write a main function.
Expected Output:
person
Topics:
Inheritance
Code Editor
1
Tab to indent ยท Ctrl+Enter to run ยท Ctrl+Space to expand shortcuts (cout, fori)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.