Dynamic Constructor
Dynamic Constructor
- The Dynamic Constructor can use to allocate memory while creating objects.
- The Dynamic Constructor will enable the system to allocate the right amount of the memory for each object when objects are not of the same size.
- Thus resulting in the saving of memory.
- Allocation of the memory to objects at the time of their construction is known as dynamic construction of objects.
Let's understand by example:
CODE:
#include
<iostream>
using
namespace std;
class
dyncons
{
int * p;
public:
dyncons()
{
p=new int;
*p=10;
}
dyncons(int v)
{
p=new int;
*p=v;
}
int dis()
{ return(*p);
}
};
int
main()
{
dyncons
o, o1(9);
cout<<"The
value of object o's p is:";
cout<<o.dis();
cout<<"\nThe
value of object 01's p is:"<<o1.dis();
}
OUTPUT:
0 Comments