Can we have a private constructor ?
Answer Posted / arun
1. Yes we can make a constructor private. By implementing
this concept we can create a singleTon class.
2. Suppose we have a static method is a class that is used
to create the object of the class by using private
constructor then that member function is named as "Named
Constructor".
3. Using this named constructor concept we can create
SingleTon class as well as normal class.
Example:
class Singleton
{
public:
static Singleton* Instance();
protected:
Singleton();
Singleton(const Singleton&);
Singleton& operator= (const Singleton&);
private:
static Singleton* pinstance;
};
Singleton* Singleton::pinstance = 0;// initialize pointer
Singleton* Singleton::Instance ()
{
if (pinstance == 0) // is it the first call?
{
pinstance = new Singleton; // create sole instance
}
return pinstance; // address of sole instance
}
Singleton::Singleton()
{
//... perform necessary instance initializations
}
Singleton *p1 = Singleton::Instance();
Singleton *p2 = p1->Instance();
Singleton & ref = * Singleton::Instance();
| Is This Answer Correct ? | 25 Yes | 0 No |
Post New Answer View All Answers
class CTest { public: void someMethod() { int nCount = 0; cout << "This is some method --> " << nCount; } }; int main() { CTest *pctest; pctest->someMethod(); return 0; } It will executes the someMethod() and displays the value too. how is it possible with our creating memory for the class . i think iam not creating object for the class. Thanks in Advance... Prakash
write string class as your own class in java without using any built-in function
What is encapsulation in oops?
i=20;k=0;
for(j=1;k-i;k+=j<10?4:3)
{
cout< What is polymorphism explain its types? Explain the advantages of inheritance. Can abstract class have normal methods? assume the program must insert 4 elements from the key board
and then do the following programs.sequential search(search
one of the elements),using insertion sort(sort the element)
and using selection sort(sort the element). What do you mean by Encapsulation? What is balance factor? What is encapsulation in simple terms? What causes polymorphism? How do you explain polymorphism? Why do while loop is used? Why do we need oop?