devxlogo

Abstract Base Class(ABC) Creation

Abstract Base Class(ABC) Creation

Question:
How do I convert a class into an abstract base class without using a pure virtual member function in it?

Answer:
If your goal is to disable instantiation of a given class while allowing instantiation of classes that are derived from it, you can declare the class’s constructor, destructor, copy constructor, and assignment operator protected. This way, derived classes, which define these members public by default, can be instantiated whereas objects of the “abstract” class cannot:

class Abs{protected: Abs(); virtual ~Abs(); //...etc.};Abs a; //compilation error; ctor inaccessibleClass Derived: public Abs{};Derived d; //OK

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist