0
votes

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"?

2
Question is a little unclear/confusing. You state quite clearly that you've understood that an abstract class must contain a pure virtual function. That is the requirement. If you don't have a pure virtual function, your class is not abstract. So are you asking if that's correct?kfsone
Yes, having at least one pure virtual function makes the class abstract. Reading this might help you.Alexandru Barbarosie
@kfsone are you saying a concrete class cannot contain a pure virtual function?user997112
@BobBlogge setting a virtual function to be 0, is making it pure virtual.Alexandru Barbarosie
@user997112 I'm saying - as you already have - that adding a pure virtual function to a class makes the class an abstract class. Once you provide an implementation for all your pvf's your class is concrete.kfsone

2 Answers

2
votes

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.

1
votes

As already stated above, an abstract class in C++ is, by definition, a class which has at least one pure virtual function. The class being abstract means that you can't make instances of it (only of "concrete" classes derived from it), which protects you from calling the "unexistent" pure virtual functions (although technically you can still call them from base class constructor/destructor and get a nasty runtime crash).

There are no explicit "interface" kind of classes in C++, so you are free to provide implementation for some functions in any classes as you wish, regardless of whether the class is already abstract due to some other function being pure virtual.

On a side note, I'd still like to point out one way to make your class abstract without actually making any "real" methods pure virtual. It is enough to make the class destructor pure virtual (note that it is generally a good idea for the destructor of any polymorphic class to be virtual anyway). A minor gotcha here is that in this particular case (only for destructor), you will still have to provide an implementation for it for the linker to be happy. It may look like this:

class A // abstract class
{
public:
    virtual ~A() = 0 {} // destructor is pure virtual, but still needs a body
};

class B : public A {}; // concrete class deriving from an abstract class

Also note that in this particular case, class B does not have to explicitly implement its own destructor to become concrete, so, if you'd like it to be abstract too, you'll have to repeat the same trick again (or add some other pure virtual methods to B).