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 6165991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:15:47+00:00 2026-05-23T22:15:47+00:00

I have no experience in C++, and I come from a Java background. Lately,

  • 0

I have no experience in C++, and I come from a Java background. Lately, I was asked in an interview on why Java would not allow multiple inheritence and the answer was pretty easy. However, I am still curious on how C++ deals with that since it allows you to inherit from more than one class.

Specifically, say there is a class called MechanicalEngineer and another called ElectricalEngineer. Both have a method called buildRobot().

What happens if we make a third class RoboticsEngineer, that inherets from both and does not override that method, and you just call:

(some instance of RoboticsEngineer).buildRobot()

Will an exception be thrown, or the method from one of the super-classes will be used? If so, how does the compiler know which class to use?

  • 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-23T22:15:48+00:00Added an answer on May 23, 2026 at 10:15 pm

    The compiler will flag this kind of situation (i.e., attempting to call (some instance of RoboticsEngineer).buildRobot()) as an error.

    This happens because the derived object has got a copy of both base objects (a MechanicalEngineer instance and an ElectricalEngineer instance) inside of itself and the method signature alone is not enough to tell which one to use.

    If you override buildRobot in your RoboticsEngineer, you will be able to say explicitly which inherited method to use by prefixing the class name, e.g.:

    void RoboticsEngineer::buildRobot() {
        ElectricalEngineer::buildRobot()
    }
    

    By the same coin, you can actually “force” the compiler to use one version or another of buildRobot by prefixing it with the class name:

     (some instance of RoboticsEngineer).ElectricalEngineer::buildRobot();
    

    in this case the ElectricalEngineer implementation of the method will be called, no ambiguity.

    A special case is given when you have an Engineer base class to both MechanicalEngineer and ElectricalEngineer and you specify the inheritance to be virtual in both cases. When virtual is used, the derived object does not contain two instances of Engineer, but the compiler makes sure that there is only one of it. This would look like this:

     class Engineer {
          void buildRobot();
     };
    
     class MechanicalEngineer: public virtual Engineer {
    
     };
    
     class ElectricalEngineer: public virtual Engineer {
    
     };
    

    In this case,

    (some instance of RoboticsEngineer).buildRobot();
    

    will be resolved without ambiguities. The same is true if buildRobot is declared virtual and overridden in one of the two derived classes. Anyway, if both derived classes (ElectricalEngineer and MechanicalEngineer) overrides buildRobot, then ambiguity arises once again and the compiler will flag the attempt at calling (some instance of RoboticsEngineer).buildRobot(); as an error.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hallo, I come from a C# background and don't have a lot of C++
I'm a novice programmer with basic Java experience, and currently learning Python. I've stumbled
I have reviewed a few Struts 1 vs 2 questions on SO but none
I need some specific and some general advice. I'm a fairly proficient Java programmer
I have a project that uses an asmx file with [WebMethods] and [SoapHeader(Auth)]. I
I would like to write the following C# function in ColdFusion, but I am
I'll be building a rails app that will basically serve user-uploaded files (not a
I've been spending time to assimilate the methodologies behind writing testable code, and I
I'm interested in helping a friend out this summer with some JavaScript trouble he
I'm doing a some research on the feasiblity of a Sharepoint project that has

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.