Rules for calling base class constructor in C++
Hello everyone! I just started learning C++ and I can't figure out the rules for calling the base class constructor during inheritance. Let's say I have a simple class `Animal` and a class `Dog` that inherits from it:\n\n```cpp\nclass Animal {\npublic:\n Animal() { cout << "Animal created"; }\n Animal(string name) : name_(name) { cout << "Animal " << name << " created"; }\nprivate:\n string name_;\n};\n\nclass Dog : public Animal {\npublic:\n Dog() { cout << "Dog created"; }\n Dog(string name) { cout << "Dog " << name << " created"; }\n};\n```\n\nWhen I create a `Dog` object without parameters, the default `Animal()` constructor is called, and that makes sense. But when I write `Dog d("Buddy")`, first `Animal()` is called, and then my `Dog(string
No answers yet.
Log in to leave an answer