1

I wrote this small program to test my understanding. What I'm having trouble understanding is that constructors aren't inherited, but class B is able to call the constructor of class A!

#include <iostream>
using namespace std;

class A {
public:
    A(int x = 2) {          //Constructor A
        num = x;
    }
    int getNum() {
        return num;
    }

protected:
    int num;
};

class B: public A {         //Constructor B
public:
    B() {
        A(5);
    }
};

int main() {

    A a(3);                     //create obj a with num = 3
    B b;                        //create obj b
    cout << a.getNum() << endl;
    cout << b.getNum() << endl;
    return 0;
}

The output is:

3
2

What did the constructor A's call do exactly? It didn't use the passed argument to initialize object b's number!

Furthermore, if I remove the default value from class A's constructor, I get compilation error:

no matching function for call to 'A::A()'

So what's exactly happening here?

I know that the correct way is to do so:

class B: public A {         //Constructor B
public:
    B() : A(5) {
    }
};

Which gives the output:

3
5

But this is just for the purpose of understanding.

DarkLight
  • 141
  • 1
  • 9

1 Answers1

5

Lets take a look at the B constructor:

B() {
    A(5);
}

Here you actually "call" the A constructor twice. Once as part of the B construction (where the "default" A constructor is called), and once as you create a temporary object inside the B constructor body.

The sequence is as

  1. B constructor called
  2. A default constructor called as part of the initialization of the B object
  3. The body of the B constructor is entered
  4. A non-default constructor called (with argument 5) as part of creation of the temporary object
  5. The temporary object goes out of scope and is destructed
  6. The body of the B constructor exits
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • I added this line inside A's constructor: cout << "A called" << endl; and got this output: A called A called A called 3 2.. the first 2 are for objects a and b, and the 3rd one is for the constructor you're claiming it's not getting called. Well, even if you're correct, then why it did affect object b's number? And why it didn't work the same way when I removed the default value (2) from the function definition? – DarkLight Oct 27 '17 at 04:52
  • @LightXXV I was in a little hurry before and got some things wrong. Updated answer. – Some programmer dude Oct 27 '17 at 05:33
  • I guess I will delete my answer since you updated yours. – MFisherKDX Oct 27 '17 at 05:44
  • Thank you.. now I get it. That means object b's number is set using the default constructor of parent class `A` with the default value, 2, since B() is a constructor with no arguments. And that's why it gave an error when I removed the default value as there's not a constructor `A` with no parameters to get called. – DarkLight Oct 27 '17 at 13:30