Simulating final Class in C++

17 Nov, 2021
Ever wondered how can you design a class in C++ which can’t be inherited. Java and C# programming languages have this feature built-in. You can use final keyword in java, sealed in C# to make a class non-extendable.
Below is a mechanism using which we can achieve the same behavior in C++. It makes use of a private constructor, virtual inheritance, and friend class. 
In the following code, we make the Final class non-inheritable. When a class Derived tries to inherit from it, we get a compilation error.An extra class MakeFinal (whose default constructor is private) is used for our purpose. The constructor of Final can call the private constructor of MakeFinal as Final is a friend of MakeFinal. 
NOTE:  MakeFinal is also a virtual base class. The reason for this is to call the constructor of MakeFinal through the constructor of Derived, not Final (The constructor of a virtual base class is not called by the class that inherits from it, instead the constructor is called by the constructor of the concrete class).
Since in C++ 11 there is support for the final specifier, the third example shows its implementation.
C++
// C++ program with compilation// error to demonstrate that// Final class cannot be inherited#include <iostream>using namespace std; // The class to be made finalclass Final; // used to make the Final class finalclass MakeFinal {private:    MakeFinal() { cout << "MakFinal constructor" << endl; }    friend class Final;}; class Final : virtual MakeFinal {public:    Final() { cout << "Final constructor" << endl; }}; // Compiler errorclass Derived : public Final {public:    Derived() { cout << "Derived constructor" << endl; }}; int main(int argc, char* argv[]){    Derived d;    return 0;}
Output
  In constructor 'Derived::Derived()':
  error: 'MakeFinal::MakeFinal()' is private
In the above example, Derived‘s constructor directly invokes MakeFinal’s constructor, and the constructor of MakeFinal is private, therefore we get the compilation error.
You can create the object of the Final class as it is a friend class of MakeFinal and has access to its constructor. For example, the following program works fine. 
C++
// C++ program without any// compilation error to demonstrate// that instances of the Final// class can be created #include <iostream>using namespace std; class Final; class MakeFinal {private:    MakeFinal() { cout << "MakeFinal constructor" << endl; }    friend class Final;}; class Final : virtual MakeFinal {public:    Final() { cout << "Final constructor" << endl; }}; int main(int argc, char* argv[]){    Final f;    return 0;}
MakeFinal constructor
Final constructor
In C++ 11, we can make the base class non-inheritable by using the final specifier. For eg, the following code gives a compile error as the base class is declared as final.
C++
// C++ Program with compilation error// as the base class is declared as final #include <iostream>using namespace std; class Base final {    // body}; // Compile error because base class is finalclass Derived : public Base {    // body}; int main() {  return 0;}
Output
prog.cpp:8:7: error: cannot derive from ‘final’ base ‘base’ in derived type ‘derive’
 class derive: public base  // compile error because base class is final
This article is compiled by Gopal Gorthi and reviewed by the GeeksforGeeks team. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
vasukesharwani24
anshikajain26
C Language
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Substring in C++
Function Pointer in C
Different Methods to Reverse a String in C++
std::string class in C++
Unordered Sets in C++ Standard Template Library
Vector in C++ STL
Map in C++ Standard Template Library (STL)
Initialize a vector in C++ (7 different ways)
Set in C++ Standard Template Library (STL)
Priority Queue in C++ Standard Template Library (STL)