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
What does and I oop mean in text?
What is the purpose of enum?
Can you inherit a private class?
Whats is abstraction in oops?
What is stream in oop?
What is use of overloading?
any one please tell me the purpose of operator overloading
What is the importance of oop?
What are the two different types of polymorphism?
Give an example where we have to specifically use C programming language and C++ programming language cannot be used?
What is difference between multiple inheritance and multilevel inheritance?
They started with the brief introduction followed by few basic C++ questions on polumorphism, inheritance and then virtual functions. What is polymorphims? How you will access polymorphic functions in C? How virtual function mechanism works?
Can a varargs method be overloaded?
What does enum stand for?
Will I be able to get a picture in D drive to the c++ program? If so, help me out?