Can we have a private constructor ?

Answers were Sorted based on User's Feedback



Can we have a private constructor ?..

Answer / srikanth

We can make a constructor Private or protected.
1. Private constructor is classic example of implementing a
singleton class( A class with a single instance)
2. Protected constructors can be used when the class cannot
be instantiated but can only be inherited.

Is This Answer Correct ?    108 Yes 2 No

Can we have a private constructor ?..

Answer / prabha govind

yes
and if a constructor is made private/protected...then that
class cannot be inherited

Is This Answer Correct ?    116 Yes 23 No

Can we have a private constructor ?..

Answer / baikunta

yes , we can creat private constructor trrough static
method we can access the class (constructor), for example
singleton, there are a lot of use in design pattern
here is example of single ton
class Singleton {
static Singleton s;
int i;
Singleton(int x) : i(x) { }
void operator=(Singleton&);
Singleton(const Singleton&);
public:
static Singleton& getHandle() {
return s;
}
int getValue() { return i; }
void setValue(int x) { i = x; }
};
Singleton Singleton::s(47);
int main() {
Singleton& s = Singleton::getHandle();
cout << s.getValue() << endl;
Singleton& s2 = Singleton::getHandle();
s2.setValue(9);
cout << s.getValue() << endl;
} ///:~

Is This Answer Correct ?    34 Yes 1 No

Can we have a private constructor ?..

Answer / 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

Can we have a private constructor ?..

Answer / arun

#include<iostream>
using namespace std;

class Singleton
{
public:
static Singleton* Instance();
private:
static Singleton* pinstance;
Singleton();
};

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
}

void main()
{
Singleton *p1 = Singleton::Instance();
cout<<p1<<endl;
Singleton *p2 = p1->Instance();
cout<<p2<<endl;
Singleton & ref = * Singleton::Instance();
}

Is This Answer Correct ?    9 Yes 4 No

Can we have a private constructor ?..

Answer / manjunath

Used mainly to control the object creation...
Ex: the Number of user Login's we can restrict to 15 by
creating 15 objects...

Is This Answer Correct ?    9 Yes 4 No

Can we have a private constructor ?..

Answer / aj

Yes, only if we want to prevent instantiation from other
class, but then the class itself cannot be sub classed.

this the heart of singleton design pattern

Is This Answer Correct ?    3 Yes 1 No

Can we have a private constructor ?..

Answer / manjunath

#include<iostream>
using namespace std;
class A
{
int value;
A* ptr;
A()
{
cout<<"\n\t\tConctructor\n";
}
public:
static A* CreateObject()
{
A* ptr=NULL;
ptr=new A;
return ptr;
}

void getdata()
{
cout<<"\n\tEnter the Value of A class\t:\t";
cin>>value;
}
void putdata()
{
cout<<"\n\t\tThe Value of A Class\t:\t";
cout<<value<<endl;
}
~A()
{
cout<<"\n\t\tDestructor\n";
}
};
int main()
{
A *ptr,*ptr1,*ptr3;
ptr=A::CreateObject();
ptr1=A::CreateObject();
ptr3=A::CreateObject();
ptr->getdata();
ptr1->getdata();
ptr3->getdata();
ptr->putdata();
ptr1->putdata();
ptr3->putdata();
delete ptr;
delete ptr1;
delete ptr3;
return 0;
}


Ref:
Singleton and Factory
Classes...

Is This Answer Correct ?    3 Yes 5 No

Can we have a private constructor ?..

Answer / ashwini

Yes, We can.
By doing so, we can create only single instance of the
class.(singleton)

Is This Answer Correct ?    0 Yes 6 No

Can we have a private constructor ?..

Answer / arpitha

no, its of no use having a private constructor,,, bcoz
declaring constructor as private makes it blocked.

Is This Answer Correct ?    6 Yes 24 No

Post New Answer

More OOPS Interview Questions

Describe what an Interface is and how it?s different from a Class.

7 Answers   Spinsys,


What are the features of oop?

0 Answers  


You have one base class virtual function how will call that function from derived class?

4 Answers  


what is new modifier in C#

8 Answers   HCL,


Contrast OOP and SOA. What are tenets of each?

1 Answers   Siebel Systems, Wipro,






What is the significance of classes in oop?

0 Answers  


what are three tenets of object orinted Systems?Why they call like that ? Please answer me. Advance thanks.

2 Answers   Excel,


what is function over loading?

5 Answers  


define a string class. overload the operator == to compare two strings

2 Answers   Birla, Ericsson, HCL, Infosys, Infotech, MCAS, Satyam,


What is conditional Compilation?

3 Answers   emc2, HCL,


write a c++ code to overload + and - for a stack class such that + provides push and - provides pop operation

1 Answers   College School Exams Tests, HCL, IBM, TCS,


What is oops in simple words?

0 Answers  


Categories