How to stop class inheritance in C++ with condition that object creation should be allowed

(Last Updated On: February 26, 2019)

Answer includes multiple solutions to stop or prevent class inheritance in C++ with condition that object creation of the class should be allowed with C++ program example and explanation.

Interview Question: I want to stop a class to be inherited and allow to create an object of the class. Design a solution for this problem statement. Give as many solutions as you can.

Answer:

We can apply two solutions to prevent a class to be inherited in C++ where object creations are allowed.

Solution-1: Make the constructor of the class private and write a static function in the class that create object and return it to users.

Here is the C++ program example, in which constructor of the class named Unique is private and have written one static function, that is GetInstance() that create object and returns to user i.e. main() function. So, below program will fulfil the criteria mentioned in the question.

Note that if a class has its constructor private then neither an object of it can be created nor it can be inherited. So, if we try to derive a new class from it then compiler will throw an error stating that private constructor of the class is inaccessible at the compile time itself.

We prevented a class to be inherited by making the class constructor private. But also, object will not be created, but, we want to allow an object of the class to be created. To solve this problem, we can have a static function in the class that is called by class name without creating an object. And this function we create the class object and return to users or main program.

Here is the complete working program that satisfy above criteria.

C++ Code Example: “Unique” class cannot be inherited but object of it can be created.

 

If we try to derive a class from Unique class (example below) compiler will throw an error stating that private constructor of Unique class is inaccessible at compile time itself.

 

 

 

Solution-2: Create a dummy class having a private constructor and make the class friend of dummy class and extend the class virtually from dummy class.

Below is the C++ program example that will not allow Unique class to be inherited. But, allow object creation of Unique class.

When we create the object of Unique class, it will access and call the private constructor of dummy class StopInheritance. Because Unique class is friend of dummy class. Note that if a class is friend of another class then it can access private data of that class. Recommend to read the order of constructor and destructor call in C++ inheritance relationship if you are not familiar with calling orders.

So, we can create the object of Unique class. But, if we derive a class from Unique class and create an object of derived class then it will directly try to access the constructor of dummy class as Unique class has inherited it virtually. And since derived class is not a friend of dummy class, hence it cannot access the private constructor resulting compiler error.