Abstract Class - RJAE5/2143-OOP GitHub Wiki

Abstract Class

Within C++ OOP, abstract classes are a special type of class that are made by having at least one pure virtual function. That is, where a member function begins with the virtual keyword and has a null function body. This is accomplished by setting the function definition equal to zero. However, not all of the functions can be pure virtual as that is considered an interface.

Example

// Abstract Class Example
class abstractClass
{
public:
    virtual int myFunc() = 0;  // Pure Virtual function
    void otherFunc() 
         {std::cout << "This is a normal member function";}
};
    

Important Notes

Having the pure virtual function stops the class from being instantiated directly, and forces any derived classes to implement the function body if they are to be instantiated. Additionally you cannot use an abstract class as a parameter type, a function return type, or the type of an explicit conversion. You can, however, declare pointers and references to an abstract class.

A derived class would override the function in a similar way to declaring the function to begin with.

Example

class derived public : abstractClass
{
public::
    int myFunc()
        {return 1;}
};

Sources:

  1. https://www.ibm.com/docs/en/zos/2.4.0?topic=only-abstract-classes-c