I'm getting started with class inheritance in c++ and I'm really stuck.
From the child class I'm trying to call the parent constructor and pass a variable as a parameter. The warning says field 'default_name' is uninitialized when used here [-Wuninitialized].
class ParentClass {
public:
ParentClass(std::string _name): name(_name) {};
std::string name;
void printname() {
std::cout << this->name << std::endl;
}
}
class DerivedClass: public ParentClass {
public:
std::string default_name = "Alex";
DerivedClass(): ParentClass(default_name) {};
}
This obviously does not work because DerivedClass() is going to be called before default_nameis even initialized. Is there a way to construct an object of the parent class from the derived class using a variable?