Introduction
A constructor is a special member function whose task is to initialise the object of its class.it is special because its name is same as the class name.The constructor is invoked whenever an object of its associated class is created.It is called constructor because it construct the values of data member of the class.
A constructor is declared and defined as follows
// class with a constructor
class integer
{
int m,n;
public:
integer(void); // constructor declared
…
…
};
integer :: integer(void) // constructor defined
{
m=0;
n=0;
}
When a class contains a constructor like the one defined above, it is guaranteed that an object created by the class will be initialized automatically. For example, the declaration
integer int1; // object int1 created
not only created the object int1 of type integer but also initializes its data members m and n to zero.There is no need to write any statement to invoke the constructor function.If a normal member function is defined for zero initialization,we would need to invoke this function for each of the objects seperately.This would be very inconvenient,if there are large number of objects.
A constructor that accepts no parameters is called the default constructor.The default constructor for class A is A::A(). If no such constructor is defined then the compiler supplies a default constructor
Therefore, the statement such as
A a;
invokes the default constructor of the compiler to create the object a.
The constructor functions have some special characteristics.These are,
1. They should be declared in the public section.
2. They are invoke automatically when the objects are created.
3. They do not have return type, not even void and therefore they cannot have return values.
4. They cannot be inherited, though derived class can call the base class constructor
5. Like other C++ functions, they can have default arguments.
6. Constructors cannot be virtual.
7.We cannot refer to their addresses.
8. An object with a constructor cannot be used as a member of a union.
9. They make implicit calls to the operators new and delete when memory allocation is required.
10. Constructor cannot be static.
Parameterized Constructor
The constructor integer(), defined above initializes the data members of all the objects to zero.However, in practice it may be necessary to initialize the various data elements of different objects with different values when they are created.C++ permits us to achieve this objective by passing arguments to the constructor function when the objects are created.The constructors that can take arguments are called parameterized constructors.
The constructor integer() may be modified to take arguments as shown below:
class integer
{
int m, n;
public:
integer(int x, int y); // Parameterized Constructor
…
…
}
integer :: integer(int x, int y)
{
m=x;
n=y;
}
When the constructor has been parameterized,the object declaration statement such as
integer int1;
may not work, we must pass the initial values as arguments to the constructor function when an object is declared.
This can be done in two way:
1. By calling the constructor explicitly.
2. By calling the constructor implicitly.
The following declaration illustrates the first method:
integer int1 = integer(0,100); // Explicit call
This statement creates an integer object int1 and passes the values 0 and 100 to it.The second impliments as follows:
integer int1(0,100); // Implicit call
This method sometimes called the shorthand method, it is used very often and easy to implement.
A Simple example which demonstrate Parameterized Constructor:
#include <iostream>
using namespace std;
class integer
{
int m, n;
public:
integer(int, int); // Constructor Declaration
void display()
{
cout << “m=” << m << “n”;
cout << “n=” << n << “n”;
}
};
integer :: integer(int x,int y) // Constructor Defination
{
m=x;
n=y;
}
int main()
{
integer int1(0,100); // Constructor implicit call
integer int2 = integer(25, 75); // Constructor explicit call
cout << “Object1” << “n”;
int1.display();
cout << “Object2” << “n”;
int2.display();
return 0;
}
Output:
Object1
m=0
n=100
Object2
m=25
n=75
Copy Constructor
The parameters of a constructor can be of any type except that of the class to which it belongs.For example,
class A
{
…
…
public:
A(A);
};
is illegal.
However, a constructor can accept a reference to its own class as a parameter.Thus, the statement
class A
{
…
…
public:
A(A&);
};
is valid. In such cases, the constructor is called the copy constructor.
A copy constructor is used to declare and initialize an object from another object.For example,
integer I2(I1);
would define the object I2 and at the same time initialize it to the values of I1.Another form of this statement is,
integer I2 = I1;
The process of initializing through a copy constructor is known as copy initialization. Remember the statement
I2 = I1;
will not invoke the copy constructor.However, if I1 and I2 are objects, this statement is legal and simply assigns the value of I1 to I2, member-by-memeber.This is the task of the overloaded assignment operator (=)
A Simple example which demonstrate Copy Constructor:
#include <iostream>
using namespace std;
Class code
{
private:
int id;
public:
code();{ } //constructor
code(int a) //constructor again
{
id = a;
}
code(code &x)
{
id = x.id; //copy in the value
}
void display()
{
cout << id;
}
};
int main()
{
code A(100); // object A is created and initialized
code B(A); // copy constructor called
code C = A; // copy constructor called again
code D; // object D is created and not initialized
D = A; // copy constructor not called
cout << “n id of A:”;
A.display();
cout << “n id of B:”;
B.display();
cout << “n id of C:”;
C.display();
cout << “n id of D:”;
D.display();
return 0;
}
Output:id of A: 100
id of B: 100
id of C: 100
id of D: 100
A reference variable has been used as an argument to the copy constructor.We cannot pass the argument by value to a copy constructor
When no copy constructor is defined, the compiler supplies its own copy constructor.
Default Constructor
It is defined like other member functions of the class, i.e., either inside the class definition or outside the class definition.
A Simple example which demonstrate Default constructor:
//To demonstrate a constructor
#include <iostream.h>
#include <conio.h>
Class rectangle
{
private:
float length, breadth;
public:
rectangle () //constructor definition
{
//displayed whenever an object is created
cout << “I am in the constructor”;
length=10.0;
breadth=20.5;
}
float area()
{
return (length*breadth);
}
};
void main()
{
rectangle rect; //object declared
cout << “nThe area of the rectangle with default parameters is:” << rect.area() << “sq.unitsn”;
}
Output:
I am in the constructor
The area of the rectangle with default parameters is: 205 sq.units