Bg1987
Bg1987

Reputation: 1169

c++ abstract class with some implementation

I want to make an abstract class in c++ with a single, but with some default implementation. so that every class that inherits it will have default behavior but you cant create an instance of the base class. but if i mark foo as pure virtual, I can't add an implementation to it.

class Base
{
public:
    virtual void foo() =0; //Now I can't add foo implementation
};

My solution was to not have it as a pure virtual, and just hide the constructor. I'm wondering if its possible to mark the class as pure, but still have some implementation?

Upvotes: 5

Views: 6693

Answers (1)

Björn Pollex
Björn Pollex

Reputation: 76886

You can add an implementation to a pure virtual function. Classes that derive can use the default implementation by explicitly invoking the base-class-method.

Upvotes: 10

Related Questions