0

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?

Community
  • 1
  • 1
amoeba
  • 95
  • 4
  • 17
  • 2
    You can't create an instance of an abstract class. Your base class contains a pure virtual function, and it's instance thus can't be created. – Rishit Sanmukhani Nov 17 '15 at 03:11
  • @Rishit take a look at the linked question. How is my implementation different? – amoeba Nov 17 '15 at 03:14
  • 1
    Your implementation of body of pure virtual function is correct. But you can't create instance of your base class, since it contains pure virtual functions. – Rishit Sanmukhani Nov 17 '15 at 03:15
  • Providing a base class implementation is normally done so that the derived classes have an intuitively named support function they can explicitly call into from their own override of the pure virtual function, should they want that behaviour (possibly with their own additional actions beforehand or afterwards). It doesn't mean you can create a base class object directly. – Tony Delroy Nov 17 '15 at 03:19
  • I see that that's the behavior, I guess that just leads me to why am I not able in instantiate an object if I provide an implementation for the virtual function? – amoeba Nov 17 '15 at 03:32
  • 2
    @amoeba You can, unless it's a pure virtual function... the whole point of a pure virtual function (as opposed to a normal virtual function) is that you can't create an instance of a class that contains one. – user253751 Nov 17 '15 at 04:08
  • @amoeba Essentially if you provide a default implementation of an abstract function, **it is not** to be able to use that class **but** to allows derived class the explicitly use that default implementation (it that later case having a different name for the default implementation, say `getVarDefault`, might make sense) – Phil1970 Jul 10 '21 at 13:04

2 Answers2

2

Ther error message tells that you can't allocate object of class with pure virtual functions.

That means your problem is int main function trying to initialize the base class object.

Pauli Nieminen
  • 1,100
  • 8
  • 7
2

Pure virtual functions are the virtual functions whose implementation must be provided by the derived class. This is one of the reason to make your virtual function pure, so that derived class must have it's implementation.

virtual void function()=0 is pure virtual function where =0 indicates it's purity.

When you have a pure virtual function in a class, you are not suppose to create a instance of class. (Otherwise making function pure virtual doesn't make much sense). Such a class is abstract class.

Now, there might be some cases where you want to have your own implementation of pure virtual function so that derived class may directly use it. Hence we provide body of pure virtual function in some cases.

If you want to create an instance of that class, don't make your virtual function pure.

Rishit Sanmukhani
  • 2,159
  • 16
  • 26