Why can't the derived class in C ++ (OOP) access the constructor with the parameters of the base class?

An important feature of the derived class is that although it can use all the methods and elements of the protected and public fields of the base class, it cannot refer to the constructor with parameters. If the constructors in the derived class are not defined, the constructor without the arguments of the base class will work when the object is created. And if we need to immediately enter data when creating a derived class object, then we need to define our constructors for it.

Why?

  • one
    Apparently this constructor is marked private or not called to initialize the base sub-object of the derived object. - VTT
  • no, it's not about the modifier. but in general, in principle. QUOTE "An important feature of the derived class is that even though it can use all the methods and elements of the protected and public fields of the base class, it cannot access the constructor with parameters. If the constructors are not defined in the derived class, the constructor will work when creating the object without arguments of the base class. And if we need to immediately enter the data when creating a derived class object, then we need to define our constructors for it. " - Maryna Said
  • Ahh, add this quote directly to the question. - VTT

1 answer 1

As I understand it, it’s about the fact that if there is a constructor with parameters in the base class, when constructing an object of a child class, you simply cannot call it:

 class foo { public: foo(int, int, int) {} }; class bar: public foo {}; int main() { bar b(1, 2, 3); // ошибка return(0); } 

Previously, you would have to write a bar class constructor:

 class bar: public foo { public: bar(int x, int y, int z): foo(x, y, z) {} }; int main() { bar b(1, 2, 3); // Ok return(0); } 

But with the advent of C ++ 11, you can instead inherit the constructors of the base class:

 class bar: public foo { public: using foo::foo; }; int main() { bar b{1, 2, 3}; // Ok return(0); }