Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Base class constructor automatic call

I have troubles understanding the output of the following code when it comes to the first printed number.

#include <iostream>
#include <string>

using namespace std;

class A{
public:
    int x;
    A(){
        x = 5;
    }
};
class B: public A{
public:
    static int x;
    B(){
        x++;
    }
    B(int i){
        x = x+i;
    }
    B(string s){
        x--;
    }
};

int B::x=10;

int main()
{
B b1;
B b2(2008);
B b3("Random string");
cout << b1.x << " : " << b2.x << " : " << b3.x << endl;
return 0;
}

output (the first "2018" is the one I’m having troubles with)

2018 : 2018 : 2018

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The first thing to note is that A::x is different from B::x. B does inherit A::x, but it introduces a new B::x which is static. Hence x in the scope of Brefers to B::x (not to A::x). Ergo, you can remove the base class without changing the output:

#include <iostream>
#include <string>

using namespace std;

class B {
public:
    static int x;
    B(){
        x++;
    }
    B(int i){
        x = x+i;
    }
    B(string s){
        x--;
    }
};

int B::x=10;

int main()
{
    B b1;
    B b2(2008);
    B b3("Random string");
    cout << b1.x << " : " << b2.x << " : " << b3.x << endl;
}

same output::

2018 : 2018 : 2018

Now, B::x starts out as 10 because thats what you initialize it to. Then B b1; increments it (in the default constructor), then B b2(2008); adds 2008 to B::x, it is 2019 now. Eventually B b3("Random string"); decrements it to arrive at the value 2018. Because B::x is static you see 3 times the same output.


The confusion seems to be caused by B::x being a static member and A::x being hidden by B::x. To reiterate the above: static int x; declares x to be a static member. There is only 1 for all instances. When one instance increments it then any other instance will see the incremented value as well. B::x hides A::x because they have the same name. I suppose this is not intentional. If your intention was to let B inherit x from B and use that, then you should remvoe the delcaration and definition of B::x.


It’s not that I don’t understand a static variable, it’s more like me not getting why b1 that (seemingly) call b(),outputs 2018 instead of incrementing the static x to 11

The line B b1; does increment A::x from 10 to 11. Though the constructor does not produce any output. The values are only printed after all constructors finished.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading