Benefit of Abstract Class with Pure Virtual Function???

I'm just now learning about C++ classes, and could use some help understanding the benefit of using an abstract class with a pure virtual function versus just using inheritance where that new function exists in the derived class(es).

What is the benefit of having a pure virtual function in a base class? Why use that instead of just having a non-abstract base class without that function, then defining the function in whatever derived class you plan to use it from? Is there something special happening in memory that makes this advantageous?

Thanks!
What is the benefit of having a pure virtual function in a base class?

One of the reasons that you would use a pure virtual function in an abstract base is to require its implementation in a derived class.


Why use that instead of just having a non-abstract base class without that function, then defining the function in whatever derived class you plan to use it from?

Because then the implementation of said function wouldn't be required, and you could not invoke that function on an object with a pointer or reference to the base class type.
abstract classes represent abstract concepts, concrete classes represents concrete types.

By writing a non-abstract base, you're telling the readers of your code that it's okay to create and manipulate a Vehicle or a Shape or a Widget that isn't actually anything you can meaningfully drive or measure or draw.
You might want to read about the separation of interface from implementation. Abstract base classes play a big part in this.

In the extreme case you have classes which are only pure virtual functions; this is the C++ way of implementing an interface (something languages like C# and Java have a specific keyword for). Obviously you can never instantiate an interface. Abstract base classes with some implementation act similarly, in that they define an interface; but in addition provide some base functionality for their derived classes to use.

Using an interface allows you to define the behaviour of the class (what it can do) without dictating how it's implemented (how it's done.) They also allow you to more effectively decouple components (and ensure they stay nicely decoupled!)

To take a simple example; if you have a class which uses a std::map and std::vector internally but not for any of the parameters in its public methods, client code will still have to include <map> and <vector> (or have them included for them) to be able to compile their code.

If the public methods are factored out into an abtract base class which doesn't include the parts which uses std::map, then it will be possible to compile the client code without these dependencies.

While it's no big deal for two header files (esp. such common ones), when you apply this approach to lots of headers in more complicated projects it can reduce interdependencies considerably.

Andy

Separating Interface and Implementation in C++
http://accu.org/index.php/journals/269
Last edited on
Thanks for the great feedback guys! I have a much clearer understanding of how the abstract class could be useful now.
Topic archived. No new replies allowed.