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 
2
  • Mother's constructor is called implicitly by Child's constructor. Commented Apr 12, 2020 at 16:24
  • 1
    Note that inheritance results in an is-a relationship, 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? for more ways the OOP notion of inheritance is at odds with what you may expect. Commented Apr 12, 2020 at 16:32

2 Answers 2

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; } }; 
Sign up to request clarification or add additional context in comments.

4 Comments

Thx for your answer. My question user:8012646 how many objects I have at the end one or two ? Because I want only one.
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).
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
@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.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.