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

What is the problem with multiple inheritance?

582


Which language is pure oop?

545


Why is oop useful?

600


Question: Write a program that prints a paycheck. Ask the program user for the name of the employee, the hourly rate, and the number of hours worked. If the number of hours exceeds 40, the employee is paid “time and a half”, that is, 150 percent of the hourly rate on the hours exceeding 40. Be sure to use stepwi se refine ment and break your solution into several functions. Use the int_name function to print the dollar amount of the check.

693


What is variable example?

593






What causes polymorphism?

569


What do you mean by Encapsulation?

640


given a set based questions and 5 questions based on it next data sufficiciency questions 2 and 2/3 english sentence completion with options very easy and 2 synononmys paragraph with 10 questions 10 minutes replace =,-,*,% with -,%,+,* type questions 5 3 questions lik following itssickhere itssickthere itssickhere istsickhere which is nt alike the others very easy

2145


When not to use object oriented programming?

567


Where You Can Use Interface in your Project

1423


What is encapsulation oop?

574


What are oops methods?

565


If a=5, b=6, c=7, b+=a%c*2. What is the final value of b?

941


What is the example of polymorphism?

554


What is the real time example of inheritance?

636