Ads block

Banner 728x90px

Constructor in C++

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

A constructor is declared and defined as follows:

//class with a constructor

class integer
{
     int m, n;
  public:
     integer(void);
     ......
     ......
};
integer :: integer(void)
{
    m=0; n=0;
}

When a class contain a constructor means that an object is created by the class will be initialized automatically.


Special Characteristics of constructor functions:



They should be declared in the public section.

They are invoked automatically when the objects are created.
They cannot return any values.

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

Constructors cannot be virtual.

An object with a constructor (or destructor) cannot be used as a member of union.

There are three constructor introduced in C++



Default Constructor
Parameterized Constructors
Copy Constructors

Default Constructor

A constructor that accepts no parameters is called default constructor.
The default constructor of class integer is integer().


Some important points should be remember before creating a constructor


The name of constructor should be match (same) with class name.
A constructor should always be declare in public section.
A default constructor does not contain any argument.
Every program in C++, has a default constructor.
A default constructor can contain block of code but, does not contain parameteres.


If a programmer forget to declare default constructor during the program and creates an object such as
integer int1;
then, a C++ compiler invokes default constructor automatically when the objects are created such as integer() {   }.

The given example will make it clear.

// class with a default constructor
class integer
{
     int m, n;     // member variables
  public:
     integer()       // default constructor declared
     {
       
     }
};
void main()
  {
    clrscr();
    integer int1;       // Object created of class integer
    getch();
  }



Parameterized Constructors

C++ permits us to achieve this objective by passing arguments to the constructor function when the objects are created.

The consteuctors 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)   // constructor defined here
{
      m=x; n=y;
}


Some important points should be remember before creating parameterized constructor


when a 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 ways:

By calling the constructor explicitly.

By calling the constructor implicitly.




The following declaration illustrates the first method:

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

This statement creates an integer object int1 and passes the value 1 and 100 to it.



The second method is implemented as follows:

// implicit call
integer int1(1,100);

The method, sometimes called the shorthand method is used very often as it is shorter, look better and is easy to implement.


#include<iostream.h>
#include<conio.h>

using namespace std;

class integer
{
     int m, n;
  public:
     // Constructor declared
     integer(int x, int y);
     void display( )
     {
        cout << " m = " << m << "\n";
          cout << " n = " << n << "\n";
     }
};

integer :: integer(int x, int y) // Constructor defined
 {
      m = x;   n = y;
 }

int main( )
{
    // Constructor called implicitly
    integer  int1(1, 100);

    // Constructor called explicitly
    integer int2 = integer(25, 50);
    cout << "\nTEST1" << "\n";
    int1.display();
    cout << "\nTEST2" << "\n";
    int2.display();
    return 0;
}

TEST1
m = 1
n = 100
TEST2
m = 25
n = 50



Copy Constructor


In c++, a copy constructor is declared like this
integer(integer &i);
A copy constructor is used to declare and initialize an object from another object. We can initialize an object bu following methods:
integer int1(int2);
and
integer int1 = int2;

The process of initializing through a copy constructor is known as copy initialization.

Point to remember


A constructor takes a reference to an object of the same class as itself as an argument.


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.


#include<iostream.h>
#include<conio.h>

using Namespace std;

class integer
{
    int x;
  public:
    integer()    // default constructor declared
      {   }
    integer(int1) { x = a;}   // parameterized constructor
    integer(integer i)     // copy constructor
    {
      x = i.x;      // copy in the value
    }
    void display()
    {
       cout << x;
    }
};
int main()
{
    integer A(100);      // object A is created and initialized
    integer B(A);        // copy constructor called
    integer C = A;     // copy constructor called again
 
   integer D;      // D is created, 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();

}

id of A: 100
id of B: 100
id of C: 100
id of D: 100



Above in the program, some codes are rounded by red color that indicates wrong way to construct a copy constructor because to construct a copy constructor, we have to declare and initialize an object at the same time and place.

The statement
        D = A;
shows that the object A assign value(100) in object D. Here, the rule to make a copy constructor is not followed by the statement.