Thursday, January 3, 2013

Constructor & Destructor – C++.



A constructor is a special member function which is used to initialize the object of its class. Constructors are similar to methods, but they are not methods. Like a method, a constructor has a set of parameters and a body of code. Unlike methods, however, constructors have no return type. In C++, there are  types of constructors, Default constructors can be called with no arguments, parameterize constructors can have argument and Copy constructor

Default constructor -  A default does not have any parameters. It is not necessary to create a default constructor, it is automatically created by the compiler. The default constructor initializes all instance variables to default value. 

Parameterize Constructor - The constructor that can take arguments are called parameterized constructors.

Copy Constructor - A copy constructor is a special constructor in the C++ programming language creating a new object as a copy of an existing object. The first argument of such a constructor is a reference to an object of the same type.

Destructor : Destructor functions are the inverse of constructor functions. Destructors are called when objects are need to be destroyed.


Example : Default Constructor

#include <iostream.h>

#include<iostream.h>
  class D_Constructor
      {
           private:
              int num;
           public:
           //Default Constructor of the class D_Constructor
              D_Constructor()
                   {
                        num=5;
                   }
              void Disp()
                   {
                        cout<<"\nNumber : "<<num;
                   }
      };
  void main()
      {
           //Crerating object and Invoking default constructor
           D_Constructor DC;
           DC.Disp();
      }


Example – Parameterize Constructor

#include <iostream.h>

  class P_Constructor
      {
           private:
              int num_1,num_2;
           public:
              P_Constructor() //Default Constructor
                   {
                        num_1=2;
                        num_2=5;
                   }
              //Parameterise Constructor -  with one parameter
              P_Constructor(int x)
                   {
                        num_1=x;
                        num_2=15;
                   }
              //Parameterise Constructor -  with two parameter
              P_Constructor(int x, int y)
                   {
                        num_1=x;
                        num_2=y;
                   }
              void sum()
                   {
                        cout<<"\nSum of "<<num_1<<" and "<<num_2;
                        cout<<" is = "<<num_1+num_2;
                   }
};

  void main( )
      {
           // object for default constructor
           P_Constructor ob_1;
           // object for parameterise constructor with one parameter
           P_Constructor ob_2(4);
           // object for parameterise constructor with two parameter
           P_Constructor ob_3(4,8);
           ob_1.sum();
           ob_2.sum();
           ob_3.sum();
      }


Example – Copy Constructor

#include <iostream.h>
  class Person
      {
           private:
              int age;
           public:
              Person (int x): age (x)
                   {}
              Person (const Person& copy): age (copy.age) //Copy Constructor
                   {
                   }
              void disp()
                   {
                        cout<<"\nAge : "<<age;
                   }
      };
  void main()
      {
           Person P_1(25);
           Person P_2 (35);
           P_1.disp();
           P_2.disp();
           Person Ob = P_1; //call the Default Copy Constructor
           Ob.disp();
           Ob=P_2;
           Ob.disp();
      }



Example – Destructor



#include <iostream.h>
#include <string.h>

  class Destructor_Ex
      {
           private:
              char *str;
              int n;
           public:
              // A Parameterize Constructor
              Destructor_Ex(const char* S, int n1)
                   {
                        n=n1;
                        str=new char(n+1);
                        strcpy(str,S);
                   }
              // Destructor
               ~Destructor_Ex()
                   {
                        delete[] str;
                   }
              void Disp()
                   {
                        cout<<"\nString is = "<<str;
                   }
      };


  void main ()
      {
           Destructor_Ex ob = Destructor_Ex("This is Demo", 20);
           ob.Disp();
           // Destructor is called & control back to main()
      }



No comments: