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 3 solutions to prevent a class to be inherited in C++ where object creations will be allowed.
Solution-1: Use the final keyword (from C++11)
In the below example, we’ve made the Unique class final. So, it’ll not be inherited by any class.
If you try to inherit it, the compiler will flash an error that “a final class cannot be used as a base class“.
Note that you can create an object of the final class as show in the main() method here.
#include <iostream>
using namespace std;
class Unique final {
public:
void display() {
cout << "My unique class" << endl;
}
};
//class Derived : Unique {
// YOUR GET ERROR HERE if you inherit the Unique class
// class Unique - a final class cannot be used as
// a base class.
//};
int main() {
// you can create an object of the final class
Unique ob;
ob.display();
return 0;
}
Solution-2: 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, the 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 by any class.
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.
Since, we’ve prevented a class to be inherited by making the class constructor private. So, an object will also not be created of this class.
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 will create the class object, and return to users or main program.
Here is the complete working program that’ll satisfy the above criteria.
C++ Code Example:
“Unique” class cannot be inherited but object of it can be created.
#include<iostream>
using namespace std;
class Unique{
private:
//Make constructor private
Unique(){}
public:
//Create a static fuction that
//returns object of the class
static Unique* GetInstance(){
return new Unique();
}
void Display(){
cout<<" My Unique Class"<<endl; } }; int main(){ Unique *u = Unique::GetInstance(); u->Display();
return 0;
}
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.
class Derived:public Unique{
public:
Derived(){
cout<<"Derived constructor"<<endl;
}
public:
void Display(){
cout<<" My Derived Class"<<endl;
}
};
Solution-3: 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 the Unique class.
When we create the object of the Unique class, it will access and call the private constructor of the dummy class StopInheritance, Because the Unique class is friend of the 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 an object of the Unique class. But, if we derive a class from the Unique class and create an object of derived class, then it will directly try to access the constructor of dummy class because the Unique class has inherited it virtually.
And since the derived class is not a friend of the dummy class, it cannot access the private constructor resulting in a compiler error.
#include<iostream>
using namespace std;
//dummy class
class StopInheritance{
private:
//make constructor private
StopInheritance(){
}
//make Unique class a friend of it
friend class Unique;
};
class Unique:virtual StopInheritance
{
public:
Unique(){
cout<<"Class";
}
void Display(){
cout<<" My Unique Class"<<endl;
}
};
class Derived:public Unique{
public:
void Display(){
cout<<" My derived Class"<<endl;
}
};
int main(){
Derived obj;
obj.Display();
return 0;
}