Home > Software engineering >  Why is a hidden function is using derived class variable?
Why is a hidden function is using derived class variable?

Time:01-30

This is a very basic program but I thought it is necessary to get my doubt cleared.

The output of the below program is disp in base, x = 100.

In the below program, I am only setting the value of x in derived class object, which in turn is inheriting it from the base class. In base class x is being intialized with the value 10. What I failed to understand is that when I call the function disp using the syntax d1.base::disp() how does it takes the value of x from the derived class into the function of base class? I am expecting it to display 10 in place of x.

Is there some concept that I am missing here?

#include<iostream>
using namespace std;
class base {
public:
    int x = 10;
    void disp() { 
        cout<< "disp in base, x = " << x << endl;
    }
};

class derived : public base {
public:
    void disp() {
        cout<< "disp in derived, x = " << x << endl;
    }
};

int main() {
    derived d1;
    d1.x = 100;
    d1.base::disp();
    return 0;
}

CodePudding user response:

Is there some concept that I am missing here?

Yes. Although your derived class disp() function hides the function with the same signature in the base class, it uses the same x member variable as the base class. This is the way inheritance works: functions and data that are not overridden (or hidden) by a derived class are inherited from the base class; internally, a derived object will have, as part of its memory layout, an actual base object, and the class will have access to the members of that base class, if the relevant access attribute (i.e. protected or public, but not private) is given to those members.

In your code, the derived class doesn't also hide/shadow that base class x member, so it uses the same variable. For the derived and base classes to have separate (i.e. different) x members, you would need to also declare that member in the derived class, like below:

class derived : public base {
public:
    int x = 10; // Gives "derived" its own "x", which hides the base class member
    void disp() {
        cout << "disp in derived, x = " << x << endl;
    }
};

With that modification, your d1.base::disp(); call will display the 10, which is the (unmodified) value of the base class member variable, because the d1.x = 100; line now modifies the entirely different x member of the derived class.

Note that hiding such data members is considered bad practice by many C programmers.

CodePudding user response:

From what I can figure out if you were to add the following line in your main method,

d1.disp();

You will get the following output,

disp in base, x = 100
disp in derived, x = 100

From what I can figure out is because the variable x in your base class is public, the derived class inherits that variable. Therefor, when you .setX to a value, you change that variable in the derived class which is the same as within the base class. Which is what I could figure out.

  • Related