Pages

Friday, March 4, 2011

Destructors

What is the use of Destructors

Just like constructors are used to allocate memory to an object, a destructor is used to deallocate or delete the objects. A destructor, like a constructor is also a special function whose primary function is to deallocte the memory occupied by an object. Destructors are used to free memory, release resources and to perform other clean up. Destructors are automatically named when an object is destroyed. Like constructors, destructors also take the same name as that of the class name but are prefixed with the "~" sign called the "tilda" operator or simply the "wave" sign. A destructor is called just before the closure of a program.

Normally, you don't need to program or define a constructor since operating system now a days are capable enough to delete the objects.


General Syntax of Destructors

~ classname();


The above is the general syntax of a destructor. In the above, the symbol tilda ~ represents a destructor which precedes the name of the class.

Properties
  1. Destructors take the same name as the class name.
  2. Like the constructor, the destructor must also be defined in the public. The destructor must be a public member.
  3. The Destructor does not take any argument which means that destructors cannot be overloaded.
  4. No return type is specified for destructors.

Thursday, March 3, 2011

Constructor

A class contains two types of members - variables and functions. A variable is used to hold the data while functions or methods are meant to provide access to variables. The functions are used to initialise the member variables, perform some kind of calculation on them and later print them. Out of these the main task of a function is to initialise the variables, because if the variables are not initialised how will the function printing them will work?

In a large program which consists of hundreds of lines of code, it is a common mistake if we forget to make a call to the function which is initialising the variables. It can also happen that the call to the function has been made but it is inside a condition which is false. Consider the following code for a better understanding.

class sample
{
int a,b;
public:
void get()
{
a=10;
b=20;
}
void display()
{
cout<
}
void somecalc()
{
cout<
}
}

void main()
{
sample s1;
s1.display();
s1.somecalc();
}

In the above code note that the function which was to initialise the value of a by 10 and b by 20 (get()) hasn't been called. Now if the variables have no value how will the functions "display" and "somecalc" will work, since both of them are dependent on the values.

Enter the constructor.

A constructor is a "special" function which is called implicitly each time an object of a class is declared. Since it is called automatically and the user is not required to call it manually the risk of its not "getting called" is zero. A constructor is mainly used to initialise the variables of a class. By declaring a constructor the programmer can shift the responsibility of initialising the variables to the system.

Properties of a constructor

1. A constructor should have the SAME NAME as of the class. For example, if the name of the class is 'SAMPLE', the constructor should also have the name 'SAMPLE'.
2. A constructor is called automatically when an object of the class is declared.
3. A constructor can not have a return value. Generally, when a function does not return a value, the keyword "void" is written before it,but in case of a constructor we can not even mention the term "void".
4. A constructor is always declared as "public", since it is going to be called as soon as an object is declared. If it is declared as "private" the compiler will not be able to access it outside the class.
5. A constructor is called only once per object. A normal (non constructor) function can be called as many times as required but a constructor is called once and just once per object.
6. A class can have more than one constructor. If a class has multiple constructors it is called "constructor overloading".
7. The programmer cannot declare a constructor as virtual or static, nor can the programmer declare a constructor as const, volatile, or const volatile.


The syntax generally is as given below:
{ arguments};


Types of Constructors:

1. Default constructor : A constructor which has no arguments is called a default constructor. Default Constructor is also called as no argument constructor. A class can not have more than one default constructor.
2. Parametrized Constructor : A constructor with arguments is called a parametrized constructor. A class can have more than one parametrized constructor.

3.Copy constructor: This constructor takes one argument. Also called one argument constructor. The main use of copy constructor is to initialize the objects while in creation, also used to copy an object. The copy constructor allows the programmer to create a new object from an existing one by initialization.