Search code examples
c++inheritancepolymorphismvirtualabstract

How to declare a class explicity abstract?


I am looking at the following:

http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8l.doc%2Flanguage%2Fref%2Fcplr142.htm

and it says an abstract class in C++ contains a pure virtual function. However, surely this does not mean to create an abstract class all I do is insert a pure virtual function? Couldn't I have the situation where I have a concrete class which doesn't provide an implementation for one particular function and therefore makes it abstract, forcing derived classes to provide the implementation? This wouldn't make the class abstract though?

So how do I differentiate between "this is an abstract class" and "this is a concrete class with one pure virtual function"?


Solution

  • A class is extactly then abstract when it has one or more pure virtual function. If you write a (base) class, wich has all functions implemented, and thus can be instantiated, but it misses a vital function, then it is simply wrong design of your class. Your base class wouldn't be complete in this case and the pure virtual uninmplemented function should be added, which makes it an abstract class then.

    If you have a derived class, which derives from an abstract class, and doesn't implement all functions from the base class, then it is in turn also abstract. In this case, it wouldn't need a pure virtual function itself, because it is abstract by inheritance.

    There is no abstract keyword in C++, like in Java to indicate that a class is abstract.