Can we stop class inheritance in C++ if object creation is allowed ??

We can use 2 easy solutions to prevent a class to be inherited in C++ where object creations will be allowed.

Solution-1: 

Use 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".

#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: 

In below C++ program , 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.
if a class has its constructor private then neither an object of it can be created nor it can be inherited by any class.

#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; 
        
    } 
}; 
/************************************
class Derived:public Unique{
public:
    Derived(){
        cout<<"Derived constructor"<<endl;
    }

public:
    void Display(){
        cout<<" My Derived Class"<<endl;
    }
};
*/************************************
int main(){ 
    Unique *u = Unique::GetInstance();
    u->Display();
    derived *d = new 
    return 0;
}

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. Same concept with little tweak is used in Singleton design pattern.  
Singleton design pattern is a software design principle that is used to restrict the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. For example, if you are using a logger, that writes logs to a file, you can use a singleton class to create such a logger. You can create a singleton class using the following code −

#include <iostream> using namespace std; class Singleton { static Singleton *instance; int data; // Private constructor so that no objects can be created. Singleton() { data = 0; } public: static Singleton *getInstance() { if (!instance) instance = new Singleton; return instance; } int getData() { return this -> data; } void setData(int data) { this -> data = data; } }; //Initialize pointer to zero so that it can be initialized in first call to getInstance Singleton *Singleton::instance = 0; int main(){ Singleton *s = s->getInstance(); cout << s->getData() << endl; s->setData(100); cout << s->getData() << endl; return 0; }

No comments:

Post a Comment

Rendering 3D maps on the web using opesource javascript library Cesium

This is a simple 3D viewer using the Cesium javascript library. The example code can be found here . Click on question-mark symbol on upp...