Introduction
All built-in data types, with respect to variables with the exception of pointers, offer some form of initialization and cleanup. When a variable is declared, memory is automatically set aside for it, and when the variable goes out of scope, the memory allocated for it is released. In order to simulate this, C++ provides two features called constructors and destructors.
Constructors:
A constructor is basically a function used for initialization – to initialize variables, to allocate memory, and so on. Take the date class for example. Assume that each time an object of the date class is created, it is to be initialize with the date 01/01/1970. The data within the class (any class) can’t be initialized at the time of declaration. This is because the declaration of a class serves only as a template, and no memory is allocated at the time of declaration.
This problem can be solved by writing an initialization function- a constructor. This function will be automatically invoked as soon as an object is created, i.e., the programmer does not have to specifically call the function to initialize the variables. The following example illustrates this:
//this program illustrate the usage of a constructor /* this program initializes a date class object with the date 01/01/1970 */ #include<iostream.h> class date { private: int dd; int mm; int yy; public: date() { dd=01; mm=01; yy=1970; } void display() { cout<<”\n”<<dd<<”/”<<mm<<”/”<<yy; } }; void main() { date date1; date1.display(); }
//example: class constructor #include<iostream.h> class CRectangle { int width, height; public: CRectangle(int, int); int area() { return (width*height) ; } }; CRectangle::CRectangle (int a, int b) { width=a; height=b; } int main() { CRectangle rect(3, 4); CRectangle rectb (5, 6); cout<<”rect area: ”<<rect.area()<<end1; cout<<”rectb area: ”<<rectb.area()<<end1; return 0; }
In the above example, as soon as an object, say date1 is created, the constructor will be invoked and the values dd, mm and yy initialize to the required date - 01/01/1970. Note that the constructor here neither takes any parameters nor does it return values. Constructors are an exception to the general rule in C++ that all functions must specify a return type and also return a value of the specified type. Moreover, since it is to be invoked automatically, it is declared in the public section of the class.
A constructor can also take parameters. A constructor that does not take parameters (like the one in Example) is called the default constructor of a class. A part from this, assume that when an object of the date class is created, the members are to be initialized with values specified by the user. The definition of the class date can be modified as follows:
//this program illustrates the usage of a constructor /* this program initializes a date class object with a date specified by the user */ #include<iostream.h> class date { int dd; int mm; int yy; public: date(int d, int m, int y) //constructors that takes parameters { dd=d; mm=m; yy=y; } void display() { cout<<”\n”<<d<<”/”<<m<<”/”<<yy; } }; int main() { date date1(1, 1, 1998); date1.display(); return 0; }
How is this constructor to be invoked?. It certainly can’t be invoked with the declaration:
date date1;
it depends on how the object is created. If the object definition includes a parameter list
date date1 (14, 7, 1998) ;
as given above, then the constructor with parameters in invoked. Actually speaking, a single class definition could include any number of constructors, which will invoke depending on the type, number and sequence of parameters. This is a special feature of C++, called overloading, and is discussed in detail.
If an object is created without any argument, and no default constructor has defined for the class, then the system is forced to create a default constructor of its own before creating the object. The default constructor, in this case, initializes the data to some predefined value.
Comments/Suggestions are invited. Happy coding......!
Comments Post a Comment