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

i am getting an of the type can not convert int to int *. to overcome this problem what we should do?

1835


What is object and example?

602


What polymorphism means?

620


Can private class be inherited?

617


What is use of overloading?

606






How do you answer polymorphism?

577


What is interface in oop?

660


What are oops functions?

583


when to use 'mutable' keyword and when to use 'const cast' in c++

1642


What are oops methods?

566


explain sub-type and sub class? atleast u have differ it into 4 points?

1832


What does no cap mean?

590


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

1696


How do you achieve polymorphism?

614


What is oops and its features?

587