Q:

Confused with C++ Inheritance

I'm so confused with the output of the following code:
#include <iostream>

using namespace std;

class Parent
{
public:
    Parent() : x(2) { }
    virtual ~Parent() { }

    void NonVirtual() { cout << "Parent::NonVirtual() x = " << x << endl; }

private:
    int x;
};

class Child : public Parent
{
public:
    Child() : x(1) { }
    virtual ~Child() { }

    void NonVirtual() { cout << "Child::NonVirtual() x = " << x << endl; }

private:
    int x;
};

int main()
{
    Child c;
    Parent* p = &c;

    c.NonVirtual();  // Output: Child::NonVirtual() x = 1
    p->NonVirtual(); // Output: Parent::NonVirtual() x = 2
    // Question: are there two x'es in the Child object c?
    //           where is x = 2 from (we have not defined any Parent object)?

    cout << sizeof(c) << endl;
    cout << sizeof(*p) << endl;

    return 0;
}

A:

The code above illustrates the fact that, in C++, only functions marked virtual are overriden. What you have here is overshadowing, not overriding. In overriding and inheritance, the behavior is based on runtime type which is the normal inheritance behavior you expect, but if you don't declare it virtual, then the behavior is based purely on compile-time type (i.e. declared type). Since p is declared to be of type Parent* it uses the implementation in Parent, while c is declared to be of type Child, and so it uses the version given by type Child. If you declared the method virtual, then in both cases, it would lookup the appropriate version of the function at runtime and invoke the version given in the  Child class.
I should also add that you have two different x variables... if you want to share variables between a base class and a derived class, you should mark it "protected" in the base class (although I would argue that it is generally poor design to do so). The x variable that you declare in Child is a different variable from the one in Parent. Remember that x is private in Parent, and so the name x didn't have any meaning in Child until you created a second variable named x in Child.

