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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:14:48+00:00 2026-06-12T07:14:48+00:00

Please tell me why the output is as below for the following program. I

  • 0

Please tell me why the output is as below for the following program. I am not getting the virtual classes in c++. observe the below code:

class B
{
public:
    B(char c = 'a') : m_c(c) {}

public:
    char get_c() const { return m_c; }
    void set_c(char c) { m_c = c; }

private:
    char m_c;
};

class C: public B
{ };

class D: public B
{ };

class E
    : public C
    , public D
{ };

int main()
{
    E e;
    C &c = e;
    D &d = e;
    std::cout << c.get_c();
    d.set_c('b');
    std::cout << c.get_c() << std::endl;
    return 0;
}

O/P: aa
I expect output would be ab. What would be the reason for getting “aa”??

If i have c.set_c(‘b’) instead of d.set_c(‘b’) then I will get O/P : “ab”, Here also, I am not getting why is it as such. Both c, d are referring to one object only.

class C:virtual public B{};
class D:virtual public B{};

If the class C, class D are inherited virtually from B, then
O/P would always be “ab”

  • 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-06-12T07:14:49+00:00Added an answer on June 12, 2026 at 7:14 am

    consider class C : public B and C* c = new C then c point to an storage that begin with a B since C* is also B*. and this is true for class D : public B.

    Now for class E : public C, public D and E* e = new E(). memory of e is something like:

    {| B of C | other members of C }{| B of D | other members of D}
    

    as you can see in above case we have 2 instance of B one for C and another for D and now it is obvious when you call ((D*)e)->set_c( 'b' ) you only change B instance of D and B instance of C will remain unchanged.

    now when you say class C : public virtual B, C++ share B instance with any other class that virtually inherit from B. so in this case e is something like:

               | shared B |
        | C members | | D members |
    

    and as you can see we have only one B so calling ((C*)e)->set_c and ((D*)e)->set_c will both act on same B.

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

Sidebar

Related Questions

Can sommebody please tell me what is not right about this code? It compiles
I want to get the following output: my_www.%first_game_start_time%(REN, 233_736) Would you please tell me
Please have a look at the code below (excerpt from a C# book): public
I have the following: http://jsfiddle.net/mVs9T/13/ Could someone please tell me why the output is
Please tell me why iam always getting Server Upload Failed! in the code given
Could you please tell me what the equivalent BASH code for the following C++
I have a question with this below program Please see the program below public
Can you please tell why the the output disappears so quickly? If you want
Please tell, how can i determine, the browser supports canvas (paperjs), or not ?
Please tell me why my window doesn't render. Below is the javascript that i

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.