Constructors and Destructors

 In C++, it is not possible to initialize the data members. To initialize the data members we require to maintain a special method in the class, i.e. called as “constructor”.
 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.

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.
iii) Copy Constructor:
 It's constructor, which can copy the data of an object into another object.

Constructor Implementation:

i) Default constructor:
public classname()
{
---------------;
---------------;
}
ii) Parameterized constructor:
public classname(arguments)
{
---------------;
---------------;
}
iii) Copy constructor:
public classname(classname argobj)
{
---------------;
---------------;
}

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.

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.



~classname()
{
----------;
----------;
----------;
}

A simple demo on Constructors and Destructors

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SimpleConstructorAndDestructorDemo
{
class SimpleDemo
{
public SimpleDemo()
{
Console.WriteLine("This is constructor...");
}
~SimpleDemo()
{
Console.WriteLine("This is destructor...");
}
}
class Program
{
static void Main(string[] args)
{
SimpleDemo sd = new SimpleDemo();
Console.Read();
}
}
}
constructor1

A complex demo on Constructors and Destructors

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ComplexDemoOnConstructorsAndDestructors
{
class Student
{
//private data members
private int StudentID;
private string StudentName;
//default constructor
public Student()
{
StudentID = 101; StudentName = "varshini";
}
//parameterized constructor
public Student(int StudentID, string StudentName)
{
this.StudentID = StudentID;
this.StudentName = StudentName;
}
//copy constructor
public Student(Student s)
{
StudentID = s.StudentID;
StudentName = s.StudentName;
}
//destructor
~Student()
{
Console.WriteLine("This is destructor...");
}
//public method
public void Display()
{
Console.WriteLine(StudentID + ", " + StudentName);
}
}
class Program
{
static void Main(string[] args)
{
//invoke default constructor
Student s1 = new Student();
s1.Display();
//invoke parameterized constructor
Student s2 = new Student(102, "sateesh");
s2.Display();
//invoke copy constructor
Student s3 = new Student(s1);
s3.Display();
Console.Read();
}
}
}
constructor1