In Mother
's declaration you have:
virtual std::string getName() = 0;
This is not just a virtual
, but a pure virtual
. The distinction between a virtual
and a pure virtual
is that the pure variety must have an override implemented in a derived class, even if you have provided an implementation in the base class. For example:
class Foo
{
public:
virtual void DoIt() = 0 {}; // pure virtual. Must be overridden in the derived class even though there is an implementation here
};
class Bar : public Foo
{
public:
void DoIt(); // override of base
};
void Bar::DoIt()
{
// implementation of override
}
You can't instantiate a class with un-implemented pure virtual
methods. If you try, you will get a compiler error:
int main()
{
Foo f; // ERROR
Bar b; // OK
}
And that is exactly what you tried to do. You declared getName()
to be pure virtual
in
Mother
, but you did not override it in Child
. Then you tried to instantiate a Child
int main()
{
Child l("lol");
Which resulted in the compiler error.
To fix it, provide an override of getName()
in the Child
class.