Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>StudentEmployee</code> certainly is a subclass of <code>Person</code>. The problem is it is so <em>twice</em>: It indirectly inherits <code>Person</code> twice (once through <code>Student</code> and once through <code>Employee</code>) and that's why you get the "ambiguous base class" error. To make sure <code>StudentEmployee</code> only inherits <code>Person</code> once, you have to use <em>virtual inheritance</em>, like so:</p> <pre><code>class Person {}; class Student : public virtual Person {}; class Employee : public virtual Person {}; class StudentEmployee : public Student, public Employee {}; </code></pre> <p>This will fix your error.</p> <p>There is another big problem with your code, though, and it's called <em><a href="https://stackoverflow.com/questions/274626/what-is-the-slicing-problem-in-c">slicing</a></em>.</p> <p>When you do this:</p> <pre><code>Person ppl[3] = {pat, sam, em}; </code></pre> <p>An array of three <code>Person</code> objects will be created but those objects will be copy constructed using the implicitly defined copy constructor of the <code>Person</code> class. Now, the problem with this is that the objects in your array will be just <code>Person</code> objects and not objects of the subclasses you want them to be.</p> <p>To fix this, you'll have to make an array of pointers to <code>Person</code> objects, like this:</p> <pre><code>Person* ppl[] = {new Person("Pat"), new Student("Sam"), new Employee("Emily"), new StudentEmployee("Sienna")}; </code></pre> <p>or</p> <pre><code>Person* ppl[] = {&amp;pat, &amp;sam, &amp;em, &amp;sen}; </code></pre>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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