Thursday, 7 February 2019

Dynamic Constructor In C++

Dynamic constructor is used to allocate the memory to the objects at the run time. Memory is allocated at run time with the help of 'new' operator.
By using this constructor, we can dynamically initialize the objects.

Example:
Class A
{
public:
int *pt;

A()                //Default dynamic constructor
{
pt=new int;
*pt=50;
}

A(int x)        //Parameterized dynamic constructor
{
pt=new int
*pt=x;
}

int show ()
{
return(*pt);
}
};

void main()
{
clrscr();
A obj1;
A obj2(40);
cout<<”The value of 1st object is:”<<”\n”;
obj1.show();
cout<<”The value of 2nd object is:”<<”\n”;
obj2.show();
}

Output:

The value of 1st object is:50
The value of 2nd object is:40

1 comment:

  1. You don't realize how quickly technology is changing. Data science is highly technical and is therefore in high demand. A career in data science will open up many lucrative job opportunities. So, if you have been wanting to start your career in Data Science, now is the best time to enroll in a data science program with one of the best data science training institute in noida.

    ReplyDelete

Intoduction to AWS and Uses.

 AWS stands for Amazon Web Services . Amazon introduced AWS, a platform for cloud computing, so that many organisations might benefit from...