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
is there any choice in opting subjects like 4 out of 7
What is the real life example of polymorphism?
when to use 'mutable' keyword and when to use 'const cast' in c++
explain sub-type and sub class? atleast u have differ it into 4 points?
What is the point of polymorphism?
What is object and class in oops?
What is protected in oop?
Why is polymorphism needed?
Which type does string inherit from?
What is a superclass in oop?
What is abstraction in oop?
What is advantage of inheritance?
Whats oop mean?
What is the purpose of polymorphism?
What is polymorphism in oop example?