Constructors

C++ provides mechanisms for ensuring that your objects are initialized properly before they are used. As your objects go in and out of scope, memory is first allotted to them and then initialized. C++ provides a special member function called the constructor for the initialization of an object.

A constructor function is called whenever an object is created. An object can be created as a global variable, as a local variable, through the explicit use of the new operator, though an explicit call of a constructor or as a temporary object. Constructors are called when an object is created as part of another object.

The constructor function is responsible for turning the raw memory allotted to an object into an usable object. Constructors come in many forms. They are default constructors, copy constructors, and other constructors that take different arguments.

A constructor is a special member function whose task is to initialize the objects of its class. Its name is same as the class. A constructor is involved whenever an object of its associated class is created. It is called a constructor because it constructs the value of data members of the class.

A constructor is declared and defiend as follows:

class number

{

int a,b;

public;

number(void); //constructor declared

.........

.........

};

number::number(void) //constructor defined

{

a=0;

b=0;

}

When a class contains a default constructor, it is certain that an object created by a class will be initialized automatically.

For example, the declaration:

number obj1; //object ob1 created

creates the object ob1 of type number and initializes its data members a and b to zero.

Default Constructorthe default constructor is a constructor that takes no arguments. If no such constructor is defined, then the compiler to create the object x

Characteristics of Constructors

The characteristics of constructor functions are:

    1. They should be declared in the public section.

    2. They are invoked automatically when objects are created.

    3. They do not have return types, not even void and therefore cannot return value.

    4. They cannot be inherited, though a derived class can call the base class constructor.

    5. Like other C++ functions, they can have default arguments.

    6. We can not refer to their addresses.

    7. They make implicit calls to the operators nes and delete when memory allocation is required.

Multiple Constructors in a Class (constructor Overloading)

C++ permits us to use more than one constructor in the same class so that even with different numbers and types of initial values, an object may still be initialized. for example, we could define a class as follows:

class number

{

int a, b;

public;

number() //constructor 1

{

a=0; b=0;

}

number(int x, int y) //constructor 2

{

a=x; b=y;

}

number(number &p) // constructor 3

{

a=p.a; b=p.b;

}

};

the first constructoe receives no arguments (default constructor), the second receives two integer arguments ( parameteterized constructor) and the third recieves one integer object as an argument (copy constructor)

HOME LEARN C++ PREVIOUS NEXT