Can we have a private constructor ?

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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Write A Program to find the ambiguities in Multiple Inheritance? How are they resolved.(Virtual Functions)

3802


Why do we use inheritance?

805


What is meant by oops concept?

771


What is inheritance in oop?

783


What is polymorphism in oop example?

712


What is polymorphism in oops with example?

746


What is this pointer in oop?

769


What is class and object with example?

786


What is an advantage of polymorphism?

802


What is the main purpose of inheritance law?

916


Is enum a class?

776


Is abstract thinking intelligence?

773


given a set based questions and 5 questions based on it next data sufficiciency questions 2 and 2/3 english sentence completion with options very easy and 2 synononmys paragraph with 10 questions 10 minutes replace =,-,*,% with -,%,+,* type questions 5 3 questions lik following itssickhere itssickthere itssickhere istsickhere which is nt alike the others very easy

2354


what is graphics

2197


What is polymorphism explain its types?

890