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 is the renewal class?
write a program to find 2 power of a 5digit number with out using big int and exponent ?
How Do you Code Composition and Aggregation in C++ ?
Why do pointers exist?
Is html an oop?
Is react oop?
What is a class oop?
Write a C++ program without using any loop (if, for, while etc) to print prime numbers from 1 to 100 and 100 to 1 (Do not use 200 print statements!!!)
What is the highest level of cohesion?
What is overriding in oops?
Where You Can Use Interface in your Project
What is polymorphism used for?
How to call a non virtual function in the derived class by using base class pointer
How is class defined?
What are main features of oop?