CodeClerks

DYNAMCI COSTRUCTOR IN C++



Write the reason you're deleting this FAQ

DYNAMCI COSTRUCTOR IN C++

The constructors can also be used to allocate memory while creating objects. This will enable the system to allocate the right amount of memory for each object when the objects are not of the same size, thus resulting in the saving of memory. Allocation of memory to objects at the time of their construction is known as dynamic construction of objects. The memory is allocated with the help of the new operator.


#include
#include

using namespace std;

class String
{
   char *name;
   int  length;
    public:
   String()   // constructor-1
   {
      length = 0;
      name = new char[length + 1];
   }
   String(char *s)   //constructor-2
   {
      length = strlen(s);
      name = new char[length + 1];   //one additional
                     //character for
      strcyp(name, s);
   }

   void display(void)
   {cout

Comments

Please login or sign up to leave a comment

Join