I am attempting to implement a base class that contains a pure virtual function that the base class will provide a default implementation for. Currently, my implementation boils down to this:
class Base {
public:
Base(string bVar);
virtual string getVar() const =0;
private:
string myBVar;
};
Base::Base(string bVar){
this->myBVar = bVar;
}
string Base::getVar() const { //virtual function default implementation
return this->myBVar;
}
class Derived : public Base {
public:
Derived(string dVar);
string getVar() const; // implements pure virtual const base function
private:
string myDVar;
};
Derived::Derived(string dVar) : Base(""){
this->myDVar = dVar;
}
string Derived::getVar() const { //virtual
return this->myDVar;
}
int main(){
Base aBaseObj("param");
return 0;
}
When I compile this code I get this error:
error: cannot declare variable 'aBaseObj' to be of abstract type 'Base' with note: because the following virtual function are pure within 'Base' ...virtual std::string Base::getVar() const
This question has been asked before and answered on. And I can't figure out how my code departs from the accepted answer significantly. When I remove the pure virtual specifier, the code compiles and runs as expected.
Where am I going wrong here with respect to marking my base class' function as pure virtual and providing an implementation for it that derived classes must provide their own implementation for?