2

Why my code does two instances, one for the parent class and one for the child class. I figure the code in the main() ask for only one. I try to unsterstand why that's happen, but no idea coming to my mind to solve this problem...

#include <iostream>

class Mother {
public:
    static int instance;
    Mother() {
        std::cout << "Mother constructor" << std::endl;
        instance++;
    }

    int get_instance() {
        return instance;
    }
};

class Child : public Mother {
public:
    Child() {
        std::cout << "Child constructor" << std::endl;
        this->instance++;
    }
};

int Mother::instance = 0;

int main() {
    Child child;
    std::cout << "instance: " <<  Mother::instance << std::endl;
}

console return

clang++  -std=c++11 -Wconversion *.cpp && ./a.out
Mother constructor
Child constructor
instance<int>: 2
Knupel
  • 323
  • 2
  • 14
  • Mother's constructor is called implicitly by Child's constructor. – 273K Apr 12 '20 at 16:24
  • 1
    Note that inheritance results in an [is-a relationship](https://en.wikipedia.org/wiki/Is-a), meaning that `Child` is a `Mother`. You might want to think a bit more on whether or not this makes sense in the context of your program/example. See [What is an example of the Liskov Substitution Principle?](https://stackoverflow.com/questions/56860/what-is-an-example-of-the-liskov-substitution-principle) for more ways the OOP notion of inheritance is at odds with what you may expect. – user4581301 Apr 12 '20 at 16:32

2 Answers2

2

The class Child inherits from Mother – i.e., Mother is a base class of Child. Therefore, when you instantiate a Child object, Mother's constructor is also called, and it does before the body of Child's constructor is executed.

In your code, both Mother and Child constructors increase the static data member instance by one, that's why after constructing child, instance value is two instead of one. To obtain the behavior you want, simply don't modify instance in Child::Child():

class Child: public Mother {
public:
    Child() { std::cout << "Child constructor" << std::endl; }
};
JFMR
  • 23,265
  • 4
  • 52
  • 76
  • Thx for your answer. My question user:8012646 how many objects I have at the end one or two ? Because I want only one. – Knupel Apr 12 '20 at 17:33
  • You have one object, `child`, of type `Child` whose base class is `Mother`. Note that a `Child` object can be converted into an object of its base class, `Mother` (i.e., the `Child` object is also a `Mother` object). – JFMR Apr 12 '20 at 17:38
  • Perfect, it's exactly what I want . I was affraid to take too much place in the memory and maybe create a leak with this situation when I read the log in the console and I see there is two constructors pass for one object. Thx a lot for your lights !!! user:8012646 – Knupel Apr 12 '20 at 17:46
  • @StanlePunk Note that something analogous happens at destruction: when destroying a `Child` object, `Mother`'s destructor is also called, and it is done after the body of `Child`'s destructor is executed. – JFMR Apr 12 '20 at 17:53
0

When child is created, the Mother constructor is called first and does instance++, then the Child constructor is called and does instance++ again. That's why you get 2.

Brady Dean
  • 3,378
  • 5
  • 24
  • 50