Advertisement
spider68

singleton class implementation

Aug 18th, 2021
1,230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class parent{
  6.     private:
  7.         int data;
  8.         static parent* obj;
  9.         parent(){
  10.             // data=0;
  11.         }
  12.     public:
  13.         static parent* getinstance(){
  14.             if(obj==NULL){
  15.                 obj=new parent();
  16.             }
  17.             return obj;
  18.         }
  19.         void setvalue(int val){
  20.             data=val;
  21.         }
  22.         void getvalue(){
  23.             cout<<data<<endl;
  24.         }
  25.    
  26. };
  27. parent *parent::obj;
  28.  
  29. int main()
  30. {
  31.   parent* obj=obj->getinstance();
  32.   obj->getvalue();
  33.   obj->setvalue(5);
  34.   obj->getvalue();
  35.   return 0;
  36. }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement