The code below shows 2 classes person and trouble_maker. In this example i'm a bit confused why the trouble_maker class does not have access to set the variable name that it inherited from person directly in the instantiating list. Why does the trouble_maker object have to be created before i'm able to set the variable name?
#include <string>
class person {
int age;
public:
person(int age);
protected:
std::string name;
};
person::person(int age) : age(age) {
}
class trouble_maker : person {
public:
trouble_maker(std::string name, int age);
};
trouble_maker::trouble_maker(std::string name, int age) : person(age), name(name) //name is not a static memeber
{
this->name = name; // works fine
}
int main() {
person a_person();
return 0;
}