Constructors and Destructors


  • C++ provides a special member function called the constructors which enables an object to initialize itself when it is created.
  • This is known as automatic initialization of objects.
  • It also provides another member function called the DESTRUCTOR that destroys the objects when they are no longer required.

  • Constructors

  • A constructor is a special member function whose task is to initialize the objects of its class.
  • It is special because its name is the same as the class name.
  • The constructor is invoked whenever an object of its associated class is created.
  • It is called constructor because it constructs the values of data members of the class.

  • A constructor is declared and defined as follows


    class integer
      {
        int m,n;
     public:
         integer (void);         // constructor declared
         …………
         ……….
        };
    integer :: integer (void)    // constructor defined
       {
         m=0;
         n=0;  
       }
    
  • When a class contains a constructor , it is guaranteed that an object created by the class will be initialized automatically.
  • integer int1;
    not only creates the object int1 but also initializes its data members m and n to zero.
  • A constructor that accepts no parameters is called the default constructor.
  • For eg: default constructor for class A is A :: A()

  • The constructor functions have some special characteristics

    • Its name is the same as the class name.
    • They should be declared in the public section.
    • They are invoked automatically when the objects are created.
    • They do not have return types, not even void and therefore, and they cannot return values.
    • They cannot be inherited.
    • They can have default arguments.
    • Constructors cannot be virtual.
    • We cannot refer to their addresses.
    • An object with a constructor (or destructor) cannot be used as a member of a union.
    • They make 'implicit calls' to the operators new and delete when memory allocation is required.

    • Parameterized constructors

    • It may be necessary to initialize the various data elements of different objects with different values when they are created.
    • C++ permits us to achieve this objective by passing arguments to the constructor function when the objects are created.
    • The constructors that can take arguments are called parameterized constructors.

    • Example:

      ass integer
        {
          int m,n;
       public:
           integer (int x, int y);         // parameterized constructor
           …………
           ……….
          };
      integer :: integer (int x, int y) 
         {
           m=x;
           n=y;  
         }
      
    • We must pass the initial values as arguments to the constructor function when an object is declared.
    • This can be done in 2 ways:

      1) By calling the constructor explicitly:
      i.e integer int1 = integer(0, 100);
      This statement creates an integer object int1 and passes the values 0 and 100 to it.

      2) By calling the constructor implicitly:
      i.e integer int1(0, 100);
      This method is sometimes called the shorthand method.

    Example

    The constructor function can also be defined within the class

    The parameters of a constructor can be of any type except that of the class to which it belongs.

    Multiple constructors in a class (Overloading of constructors)

    • C++ permits to use more than one constructor in the same class.
    • For example:
                            class integer 
                           {
      
                                  int m,n;
                              public:
                                 integer()
                                    { m=0; n=0; }          //C-1
                                 integer (int a, int b)
                                     {m=a; n=b;  }         //C-2
                                 integer (integer & i)
                                     { m=i.m; n=i.n; }    //C-3
                            };
    • This declares 3 constructors for an integer object.
    • Third receives one integer object as an argument.
    • For ex: The declaration, integer I1;
      would automatically invoke the first constructor and set both m and n to I1 to zero
    • The statement, integer I2(20, 40);
      would call the second constructor
    • The statement, integer I3( I2);
      would invoke the third constructor which copies the values of I2 into I3.
      In other words, it sets the value of every data element of I3 to the value of the corresponding data element of I2. Such a constructor is called copy constructor.
    • When more than one constructor function is defined in a class, we say that constructor is overloaded.

    Example here

    Constructors with default arguments

    • It is possible to define constructors with default arguments.
    • For example:
      complex (float real, float imag=0);
      Then the statement,
      complex C (5.0);
      assigns the value 5.0 to real and zero to imag (by default)
    • The statement, complex C (2.0, 3.0);
      assigns 2.0 to real, 3.0 to imag. Overrides the default value.

    Destructors

    • A destructor, as the name implies, is used to destroy the objects that have been created by a constructor.
    • The destructor is a member function whose name is the same as the class name but is preceded by a tilde.
    • For example, the destructor for the class integer can be defined
      ~integer() { }
    • A destructor never takes any argument nor returns any value.
    • It will be invoked implicitly by the compiler upon exit from the program to clean up storage that is no longer accessible.
    • Usage of destructor , saves the memory.

    Example


    Data Hiding << Previous

    Next >> Static data members

    Our aim is to provide information to the knowledge seekers. 


    comments powered by Disqus




    Footer1