This program shows how to create the private constructor in C++.
Code: Cpp
#include<iostream.h>
#include<conio.h>
class Test
{
int Data; //Stores the Data
Test(int Val):Data(Val) //Private Constructor
{
}
public:
static Test Initialize(int Num) //Static Member Function generally called Factory Method.
{
return Test(Num); //Return the Object.
}
void Display() //Used to display the result.
{
cout<<"Value of Data is "<<Data<<endl;
}
};
void main()
{
clrscr();
Test Obj = Test::Initialize(10); //Static Member function called to return the object.
Obj.Display();
getch();
}

