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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:10:53+00:00 2026-05-31T03:10:53+00:00

For example, in this piece of code, if line [a] is commented out, the

  • 0

For example, in this piece of code, if line [a] is commented out, the output is 0.

inh2.cpp

#include<iostream>
using namespace std;

class A {
    public:
        int x;
        A() { x = 10; }
};

class B : public A {
    public:
        int x;  // <--------- [a]
        B() { x = 0; }
};

int main() {
    A* ab = new B;
    cout << ab->x << endl;
}

results from gcc

$ g++ inh2.cpp
$ ./a.out
10
$

I have two questions:

  1. How does ab->x resolve to 10 in the above case? The object is of type class B, and thus should value to 0.
  2. Why does commenting Line [a] change the behaviour of the code? My reasoning is that x would have anyways been inherited, which should result in same behaviour.

My reasoning for Q #1 above:

  • ab points to the memory location of an object of class B. It is a physical object in the sense that all the variables with their values are assigned memory.

  • Variable x within this object stores value 0.

  • When ab->x is done, ab tells us the memory location of the object, and we go look inside it to find that x is 0. So we should print 0.

Where am I wrong here?

  • 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-31T03:10:54+00:00Added an answer on May 31, 2026 at 3:10 am
    1. Yes, it is of type B, but you are assigning it as a pointer to an A, and therefore it is using the x defined on A (as when we’re dealing with a pointer to A, we don’t know that B even exists, even though that’s what you allocated).

    2. When you comment out the line, during the construction phase, As constructor is called first, then Bs constructor, which sets x (in its base class) to 0. There is only one x at this point, and Bs constructor is called last.

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

Sidebar

Related Questions

I have this piece of code (from the Nokia PC connectivity 3.2 example code,
I have this piece of code here: $query = mysql_query(SELECT * FROM example WHERE
for example this code var html = <p>This text is <a href=#> good</a></p>; var
How i can get full right name of generic type? For example: This code
example: at this domain http://www.example.com/234234/go.html is only one iframe-code how can i get the
Consider this code (prestuffed with an example): DateTime dt1 = DateTime.Parse(7/30/2010 9:33:29.1234567 AM); DateTime
Consider the following piece of code: As you can see we are on line
I have piece of code: class example { public function say($x) { if ($x
I have the following piece of code (shortened for the example): while (reader.ready()) {
I’ve had this problem many times: I have a piece of source code, but

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.