Posts

Showing posts from May, 2017

Singleton

Image
To ensure only one instance of the class should be created Check list Define a private static variable in the "single instance" class. Define a public static member function in the class. Do "lazy initialization" (creation on first use) in the accessor function. Define all constructors to be protected or private . Clients may only use the accessor function to manipulate the Singleton.   Program class mysingle { private:     mysingle()     {     }     mysingle(const mysingle &obj)     {     }     mysingle operator=(const mysingle &obj)     {     }     static mysingle *instance;   public:     static mysingle* getinstance();     void show()     {         cout<<"in show funtion"<<endl;     } }; mysi...

Casting in c++

In c++ there are 4 types of casting 1. static_cast 2. const_cast 3. dynamic_cast 4. reinterpret cast 1. static cast 1. It is most general purpose cast in c++ 2. It is able to cast basic type of casting i.e. int to double, base class pointer to derive class pointer 3. It can not used to remove constantness or volatileness in object. 4. It cannot be used to cast struct to int or a duble into a pointer.     int x;     double d;    d = static_cast<double>(x); 2. Const_cast 1. This type of casting is used to remove constatness or volatileness. 2. const_cast rejects if request to modify other then constantness or volatileness. #include <iostream> using namespace std; class student { private :      int roll; public :      // constructor      student( int r):roll(r) {}        // A const function that changes ...