Constructor in c++

Constructor

A constructor is a 'special' member function whose task is to initialize the objects 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 calles constructor because it Constructs the value of data members of the class.


Types of constructors

  1. Default constructor
  2. Parametrized construtor
  3. Copy contructor
Constructor in c++
Special characteristics of constructor function:
  1. They should be declared in the public section.
  2. They are invoked automatically when the objects are created.
  3. They do not have return type, not even void and therefore, and they cannot return values.
  4. They cannot be inherited, though a derived class can call the base class constructor.
  5. Like others c++ function, they can have default arguments.
  6. Constructors cannot virtual.(meaning of virtual will be discussed on another page please refer link below)
    Virtual function in c++
  7. we cannot refer to their addresses
  8. An object with a constructor or (distructor) cannot be used as a member of a union.
  9. They make 'implicit calls' to the operators new and deleted when memory allocation required.

Remember, when a constructor is declared for a class, initialzed of the class objects becomes mandatory.
// A construcotr is declared and defined as follows:
class integer
{
int m, n;
public:
integer(void); //Constructor declared
.......
};
integer::integer(void) // constructor deifned
{
m=0; n=0;
}

When a class conatins a costructor like one defined above, it is guranteed that an object created bt the class will be initialized automatically. for example, the declaration

integer int1; // object int1 created

not only creates 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 (as we do with the normal member functions). if a 'normal' member function is defined for zero initializatin, we would need to invoke the function for each function for each object of the object separately. This would be very inconvenient, it there are a large number of onjects.

Parameterized constructor

The constructor integer(), defined above, initialize the data memeber of all the object to zero. However, in practice it may be necessary to initialize the various data element of different object with different values when they are created. C++ permits us to achieve this objective by passing argument to the constructor function when the object are created. The constructor that can take arguments are called parameterized constructor.

The contructor integer() may be modified to take argument 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 a constructor has been parameterized, the object declaration statement such as

integer int1;

may not work. we much pass the initial values as argument to the constructor function when an object is declared. This can be done in two ways

  • By calling the constructor explicitly
  • By calling the constructor implicitly

The following declaration illustrates the first method:

integer int1 = integer (0,100); // explicit call

The statement creates an integer object int1 and passes the value 0 and 100 to it. The second is implemented to as follows:


int1(0,100); //implicit call

This method, someitmes called the shorthand method, is used very often as it is shorter, looks better and is easy to impliment.
Remember, when the construtor is parameterized, we must provide appropriate argument fo the constructor.

Copy constructor

A copy constructor is used to declare and initialize an object from another object for example, the statement

integer I2 (I1);

would define the object I2 and at the same time initialize it to the value of I1. Another form of this statement is

integer I2 = I1;

The process of initialize through a copy construcor is known as copy initialization. Remember, the statement

I2 = I1;

will not invoke the copy condtrucotr. However if 01 and I2 are objects, this statement is legal and simply assigns the value of I1 and I2, member-by-member. This is the task of the overloaded assignment operator(=). We shall see more about this later.

copy construtor takes a reference to an object of the same class as itself as an argument, Let us consider a simple example of constructing and using a copy constructor as shown in below program.

#include <iostream>
using namespace std;

class code
{
int id;
public:
code(){} // constructor
code(int a) //constructor again
{
id=a;
}
code(code & x) //copy constructor
{
id=x.id; // copy in the value
}
void display(void)
{
cout<<id;
}
};

int main()
{
code A(100); //object A is created and initialzed
code B(A); //copy constructor called
code C = A; //copy constructor called again
code D; // D is created, not initilized
D = A; // copy construtor 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

Note:

A reference variable has been used as an argument to th copy constructor, We cannot pass the argument by value to a copy constructor.

Post a Comment

1 Comments