Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ : Multiple inheritance with polymorphism
    primarykey
    data
    text
    <p>(pardon the noob question in advance)</p> <p>I have 4 classes:</p> <pre><code>class Person {}; class Student : public Person {}; class Employee : public Person {}; class StudentEmployee : public Student, public Employee {}; </code></pre> <p>Essentially <code>Person</code> is the base class, which are directly subclassed by both <code>Student</code> and <code>Employee</code>. <code>StudentEmployee</code> employs multiple inheritance to subclass both <code>Student</code> and <code>Employee</code>.</p> <pre><code>Person pat = Person("Pat"); Student sam = Student("Sam"); Employee em = Employee("Emily"); StudentEmployee sen = StudentEmployee("Sienna"); Person ppl[3] = {pat, sam, em}; //compile time error: ambiguous base class //Person ppl[4] = {pat, sam, em, sen}; </code></pre> <p>When I use an array of <code>Person</code>, the base class, I can put <code>Person</code> and all of its subclasses inside this array. Except for <code>StudentEmployee</code>, given the reason ambiguous base class.</p> <p>Given that <code>StudentEmployee</code> is guaranteed to have all the methods and attributes of <code>Person</code>, is <code>StudentEmployee</code> considered a subclass of Person? </p> <ul> <li>If so, Why does the compiler not allow me to assign an object to a variable of the type of its superclass?</li> <li>If not, why not; and what would be the proper way to accomplish this?</li> </ul> <p>Cheers</p> <hr> <p>EDIT: Preemptively, this question is NOT the same as either of the following:<br> <a href="https://stackoverflow.com/questions/2423231/polymorphism-relates-inheritance">polymorphism relates inheritance</a><br> <a href="https://stackoverflow.com/questions/1889996/inheritance-mucking-up-polymorphism-in-c">Inheritance mucking up polymorphism in C++?</a> </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload