Saturday, March 1, 2014

Constructor & Destructor

Constructor:

The constructor is a special type of method. It will be called automatically, whenever an object is declared for the class. This is used to initialize the fields / properties.

Def:  A constructor is a method of the class, i.e. meant for initializing the data members. The constructor gets called automatically, whenever the object is declared.

Note:  In  C++,  it  is  not  possible  to  initialize  the  fields  directly,  that‘s  why  you  require "Constructor"  compulsory.  But  in  C#.NET  and  VB.NET  it  is  possible  initialize  the  data members  directly,  along  with  the  declaration.  So,  to  initialize  the  simple  values  into  the fields,  you  don‘t  require  the  constructor  particularly  in  C#.NET.  But  if  you  want  to initialize  the  fields  with  some  complex  code,  then  you  require  to  write  the  constructors compulsory in C#.NET and VB.NET also.

Rules of Constructors:
  • Its name must be same as "classname".
  • It must be defined as "public method".
  • It can be defined with/without arguments.
  • It can't return any value. So, no return type specification is required.
  • "Constructor  overloading"  is  possible.  Writing  multiple  constructors  in  the  same  class  is called as "Constructor overloading".
Types of Constructors:
i)  Implicit constructor:
  • A constructor, offered by the compiler implicitly is called as "Implicit constructor". 
  • But the compiler automatically offers an implicit constructor, for "constructor-less class" only.
ii)  Explicit constructor:
  • A constructor, defined by the programmer.
  • It always overrides the implicit constructor.
Models of Constructors:
i)  Default constructor:
  • It's a constructor, with no arguments.
ii)  Parameterized constructor:
  • It's a constructor, with one or more arguments.
Constructor Implementation:
i)  Default constructor:
            public  classname()
                              {  
                   ---------------;
               }


ii)  Parameterized constructor:

            public  classname(arguments)
              {  
                 ---------------;
                 ---------------;
              }

Destructor

It is also a method of a class, which is having some special features just like constructor. But it can be called automatically by the compiler, at "object destruction time". Object  destruction  time  means,  the  time  of  clearing  of  memory  i.e.  allocated  for  the object. The destructor is used to implement any process that is to be performed at the time of object closing.

Rules for destructor:
  • Its name must be defined as class name.
  • It' name must be started with "~" character.
  • Access modifier can‘t be specified for this.
  • No arguments.
  • No return value.
  • Destructor  overloading is not possible. That means multiple  destructors  can't be defined inside of a class.
Destructor Implementation:
         ~classname()
              {   ----------;
                   ----------;
              }
Contructor and Destructor

Download above code for Demo of Contrcutor and Destructor : Download Links

No comments:

Post a Comment

Flag Counter