Does C++ have a keyword for “abstract”?

No, C++ does not use the abstract keyword to describe abstract classes. But, just because C++ does not use the abstract keyword, that does not mean that it doesn’t have abstract classes. In C++, abstract classes are created through the use of pure virtual functions.

A C++ class is abstract if it has a pure virtual function

Any C++ class with at least one pure virtual function is considered to be an abstract class. This means that there can be no object created for that class (since it is abstract). This also means that any class that derives from that abstract class must implement the pure virtual function, otherwise that derived class will also be considered to be an abstract class as well.

How are abstract classes in C++ different from abstract classes in Java?

Java uses the keyword abstract in order to declare abstract classes. An abstract class may or may not have abstract methods. But if there are any abstract methods in a class, then that class must absolutely be declared to be abstract. So an abstract class in Java could look like this:

public abstract class SomeClass {
   // declare fields and methods

   abstract void someMethod() ;

An abstract class in C++ just needs to have a pure virtual function inside it. This means that abstract classes in C++ are not explicitly declared to be abstract – having a pure virtual function implicitly makes a class abstract in C++. So, an abstract class in C++ could look like this – note that there is no abstract keyword being used, since none exists in C++:

//this is an abstract class:
class SomeClass {
   virtual void pure_virtual() = 0;  // a pure virtual function

Are abstract methods the same thing as pure virtual functions?

Yes, they are both essentially the same thing. However, there is one small difference that is worth noting: in C++, a pure virtual function can actually have an implementation in the base class, as you can read more about here: pure virtual functions.

Abstract methods in Java are basically the same thing as pure virtual functions in C++. Because both abstract methods and pure virtual functions must be overridden in derived classes in order to be able to instantiate those derived classes. Otherwise, those derived classes become abstract classes themselves.

Hiring? Job Hunting? Post a JOB or your RESUME on our JOB BOARD >>

Subscribe to our newsletter for more free interview questions.

Leave a Reply

Your email address will not be published.