Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1013179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:03:39+00:00 2026-05-16T10:03:39+00:00

(pardon the noob question in advance) I have 4 classes: class Person {}; class

  • 0

(pardon the noob question in advance)

I have 4 classes:

class Person {};
class Student : public Person {};
class Employee : public Person {};
class StudentEmployee : public Student, public Employee {};

Essentially Person is the base class, which are directly subclassed by both Student and Employee. StudentEmployee employs multiple inheritance to subclass both Student and Employee.

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}; 

When I use an array of Person, the base class, I can put Person and all of its subclasses inside this array. Except for StudentEmployee, given the reason ambiguous base class.

Given that StudentEmployee is guaranteed to have all the methods and attributes of Person, is StudentEmployee considered a subclass of Person?

  • If so, Why does the compiler not allow me to assign an object to a variable of the type of its superclass?
  • If not, why not; and what would be the proper way to accomplish this?

Cheers


EDIT: Preemptively, this question is NOT the same as either of the following:
polymorphism relates inheritance
Inheritance mucking up polymorphism in C++?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T10:03:40+00:00Added an answer on May 16, 2026 at 10:03 am

    StudentEmployee certainly is a subclass of Person. The problem is it is so twice: It indirectly inherits Person twice (once through Student and once through Employee) and that’s why you get the “ambiguous base class” error. To make sure StudentEmployee only inherits Person once, you have to use virtual inheritance, like so:

    class Person {};
    class Student : public virtual Person {};
    class Employee : public virtual Person {};
    class StudentEmployee : public Student, public Employee {};
    

    This will fix your error.

    There is another big problem with your code, though, and it’s called slicing.

    When you do this:

    Person ppl[3] = {pat, sam, em};
    

    An array of three Person objects will be created but those objects will be copy constructed using the implicitly defined copy constructor of the Person class. Now, the problem with this is that the objects in your array will be just Person objects and not objects of the subclasses you want them to be.

    To fix this, you’ll have to make an array of pointers to Person objects, like this:

    Person* ppl[] = {new Person("Pat"), new Student("Sam"),
                     new Employee("Emily"), new StudentEmployee("Sienna")};
    

    or

    Person* ppl[] = {&pat, &sam, &em, &sen};
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Pardon me if this question sounds trivial, I don't have any experience working with
Please pardon the newbie question, but I'm stumped. I'm a teacher converting a Rails
please pardon the absolutely newbie question but i'm very new to tableau. what I'd
I beg pardon for not doing homework on this issue and directly asking question
Pardon any mistaken terminology; I am relatively new to Scala. I will endeavor to
Pardon me if m asking some discussion based questions. I am completely new to
Pardon me if this is too trivial. I am doing an XSLT by calling
Pardon the length, please. Also, this is a completely hypothetical, off-the-cuff situation and code
Pardon me for posting here instead of serverfault, but NServiceBus seems like the kind
Pardon my newbie-ness to Java as I am not experienced enough to know the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.