By using public member function of that class

#include<iostream.h>
#include<conio.h>
class demo
{
private:
                demo()
                                {
                                cout<<"Private Default Constructor Is Executed";
                                }
public:
                static void call()
                                {
                                demo();
                                }
};
void main()
{
                clrscr();
                demo::call();
                getch();
}
173 A.PNG
By using new operator

#include<iostream.h>
#include<conio.h>
class demo
{
private:
                int x;
                demo()
                                {
                                cout<<"Private Default Constructor Is Executed"<<endl;
                                x=10;
                                cout<<"X = "<<x<<endl;
                                }
public:
                demo *func()
                                {
                                return (new demo());
                                }
};
void main()
{
                clrscr();
                demo *d=NULL;
                d=d->func();
                getch();
}
173 B.PNG
Advertisements