C ++ Inheritance: calling the base class constructor in the header

Suppose a class Childis a derived class of a class Parent. In the five file program, how would I indicate in Child.hthat I want to call the constructor Parent? I don't think there is anything like the following in the header:

Child(int Param, int ParamTwo) : Parent(Param);

In this situation, what should the constructor syntax look like Child.cpp?

+4
source share
2 answers

In Child.h, you simply declare:

Child(int Param, int ParamTwo);

In Child.cpp you will have:

Child::Child(int Param, int ParamTwo) : Parent(Param) {
    //rest of constructor here
}
+11
source

.

class Child : public Parent {
    // ...
    Child(int Param, int ParamTwo) : Parent(Param)
    { /* Note the body */ }
};

class Child : public Parent {
    // ...
    Child(int Param, int ParamTwo);
};

(Child.cpp)

Child::Child(int Param, int ParamTwo) : Parent(Param) {
}
+7

Source: https://habr.com/ru/post/1543293/


All Articles