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
Describe these concepts: Polymorphism, Inheritance and Abstraction.
What is solid in oops?
Is html an oop?
What is difference between data abstraction and encapsulation?
Why interface is used?
What is the highest level of cohesion?
program for insertion ,deletion,sorting in double link list
What is a class and object?
What is an advantage of polymorphism?
Why can't we have instance(stack) of a class as a member of the same class like eg.Class A{A obj;} as we can have self refential pointer
What is stream in oop?
what is the sylabus for priliminaries?
What is the real life example of polymorphism?
How do you answer polymorphism?
what is difference between class template and template class?