13

The benefit of defining common virtual functions in the base class is that we don't have to redefine them in the derived classes then.

Even if we define pure virtual functions in the base class itself, we'll still have to define them in the derived classes too.

#include <iostream>
using namespace std;

class speciesFamily
{
    public:
        virtual void numberOfLegs () = 0;
};

void speciesFamily :: numberOfLegs ()
{
    cout << "\nFour";
}

class catFamily : public speciesFamily
{
    public:
        void numberOfLegs ()
        {
            speciesFamily :: numberOfLegs ();
        }
};

This may look fancy for sure, but are there any situations when it is beneficial to define a pure virtual function in the base class itself?

Kara
  • 6,115
  • 16
  • 50
  • 57
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
  • possible duplicate of [pure virtual function with implementation](http://stackoverflow.com/questions/2089083/pure-virtual-function-with-implementation) – Steve Jessop Nov 09 '11 at 10:59
  • And note that the answers over there have something in common. Other than the special case of destructors, the implementation in the base class will only ever be called explicitly by derived classes. It could just as well have had a different name, so the "benefit" is just that it has the same name as the override expected to call it, saving you from warts like `_default` or `_base`. – Steve Jessop Nov 09 '11 at 11:02

4 Answers4

14

Two things:

First off, there's one border-line scenario which is commonly cited: Suppose you want an abstract base class, but you have no virtual functions to put into it. That means you have no functions to make pure-virtual. Now there's one way around: Since you always need a virtual destructor, you can make that one pure. But you also need an implementation, so that's your canditate:

struct EmptyAbstract
{
  virtual ~EmptyAbstract() = 0; // force class to be abstract
};
EmptyAbstract::~EmptyAbstract() { } // but still make d'tor callable

This may help you minimize the implementation size of the abstract class. It's a micro-opimization somehow, but if it fits semantically, then it's good to have this option.

The second point is that you can always call base class functions from derived classes, so you may just want to have a "common" feature set, despite not wanting any abstract instances. Again, in come pure-virtual defined functions:

struct Base
{
  virtual void foo() = 0;
};

struct Derived : Base
{
  virtual void foo()
  {
    Base::foo();  // call common features
    // do other stuff
  }
};

void Base::foo() { /* common features here */ }
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • I don't understand the sense of this `suppose you want an abstract base class, but you have no virtual functions to put into it.` Abstract class means its objects can't be created. Fine. And now if we don't have even virtual functions to put into it, then what sense does it make to create that abstract base class? Any example situations in which this scenario will make sense? – Aquarius_Girl Nov 09 '11 at 11:35
  • 1
    @AnishaKaul: I never said it made sense! C++ is a rich, complex language that allows you to do all sorts of things that are logically possible on paper. Not all of them are practically useful (like protected inheritance, or function-try-blocks). Who knows, maybe you just want a blank type-erasing base class with no features? Some people who love dynamic casts might have a use for an empty common base class... Actually, the fact that those babies are even hard just to *write* might indicate that this is not a feature that's expected to be used a lot. – Kerrek SB Nov 09 '11 at 11:37
  • Alright, but I was looking for a real life situation in which defining pure virtual functions in base class would have made actual sense. :) – Aquarius_Girl Nov 09 '11 at 11:42
  • If you want an abstract base class it is likely simpler to make the constructors protected. – Angel O'Sphere Nov 09 '11 at 11:54
  • 1
    @AngelO'Sphere: That doesn't make it abstract; it just makes it harder (but not impossible) to instantiate. It also doesn't make it polymorphic, which is (more or less) the only reason you'd want an abstract class that doesn't declare virtual functions. – Mike Seymour Nov 09 '11 at 13:28
  • @Mike Seymour then your definition of "abstract" must be pretty strange. Ofc you can make tricks in C++ with friends and instanciate a class with a protected or private constructor. Nevertheless literature considers such a class abstract. If you don't have virtual functions, the class is not polymorphic anyway. But I assume in your case you mean assignable-polymorphism, the only thing all derived objects have in common is that you can "delete" them. – Angel O'Sphere Nov 29 '11 at 13:22
  • 1
    @AngelO'Sphere: There's nothing to be confused about. In C++ a class is "abstract" if it has pure-virtual functions or unoverridden pure-virtual base functions, period. – Kerrek SB Nov 29 '11 at 13:42
  • According to whose definition? A pure virtual function is only *one* way to make a class abstract. Abstractness is an OO term, not an C++ term. The class is also abstract if it has protected ctors. The more confusing thing or question towards Mike was: why does he think a class with a pure virtual destructor is polymorph? To be polymorph imho a class needs to define at least one virtual function, regardless whether pure or not. Strictly speaking the function does not even need to be virtual (e.g. if the class is mainly used as template argument). – Angel O'Sphere Nov 29 '11 at 14:06
  • 2
    @AngelO'Sphere: This question is about C++, where the meanings of "abstract class" and "polymorphic class" are defined by the language standard as, respectively, "a class with at least one pure virtual function" and "a class with at least one virtual function". Other meanings of those words might be useful in other contexts, but just cause confusion when used to describe other C++ concepts. – Mike Seymour Nov 29 '11 at 15:17
  • Ah, I had expected those where explanaitions and not definitions. Thanx for the clarification. – Angel O'Sphere Nov 30 '11 at 15:42
11

are there any situations when it is beneficial to define a pure virtual function in the base class itself?

Yes - if the function in question is the pure virtual destructor, it must also be defined by the base class.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

A base-class with only pure virtual functions are what languages like Java would call an interface. It simply describes what functions are available, nothing else.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 1
    Except, in C++, pure virtual functions *can* have implementations. Hence the question, when would you want to give them implementations? – Mike Seymour Nov 09 '11 at 11:34
  • @MikeSeymour If a virtual function has an implementation, it is no longer pure. The base (_interface_) class have _pure_ virtual functions, while the derived classes have normal virtual function. – Some programmer dude Nov 09 '11 at 11:44
  • 1
    A virtual function is pure if it is declared so (i.e. followed by `=0`). This makes the class abstract (preventing instantiation except as a base class), but does not say anything about whether or not the function has an implementation. As I said, pure virtual functions *can* have implementations. – Mike Seymour Nov 09 '11 at 11:45
0

It can be beneficial when there is no reasonable implementation of pure virtual function can be in base class. In this case pure virtual functions are implemented in derived classes.

ks1322
  • 33,961
  • 14
  • 109
  • 164
  • Except the question is, when would it be beneficial to implement them in the *base* class. – Mike Seymour Nov 09 '11 at 11:35
  • @Mike Seymour: it is beneficial to implement some default behavior in base class to avoid code duplication for example – ks1322 Nov 09 '11 at 11:47