Constructors in C++

In this tutorial, we will learn about the constructors in C++ and its types with example in detail.

constructor in c++

Constructor is the member function that is executed automatically when and object is created. Constructors is used for initializing objects as it will be called once the object is created without need of calling separate member function to initialize object.

A constructor is define by giving the same name as the class. It doesn’t have any return type as it is called by the system and there is no any program to return to.

Structure of constructor

class class_name  //name of class
{
    ...
public:
    class_name(); // constructor (same name as class_name)
    ...
};
class_name :: class_name() //constructor defined
{
      //constructor body
}

Example : program to make simple calculator

#include<iostream>
using namespace std;

class Calculator
{
private:
  int val1, val2, x;
public:
    Calculator (int a, int b)	//parameterized constructor to initialize a and b
  {
    val1 = a;
    val2 = b;
  }
  int sum ()			//function to find sum
  {
    x = val1 + val2;
    return x;
  }
  int mul ()			//function to find sum
  {
    x = val1 * val2;
    return x;
  }
  int div ()			//function to find sum
  {
    x = val1 / val2;
    return x;
  }
  int diff ()			//function to find sum
  {
    x = val1 - val2;
    return x;
  }

  void display (string type)		//function to display the total
  {
    cout << val1 << type << val2 << " = " << x << endl;
  }
};

int
main ()
{
  Calculator c (8, 2);		//initializing object implicitly
  c.sum ();
  c.display ("+");
  c.mul ();
  c.display ("*");
  c.div ();
  c.display ("/");
  c.diff ();
  c.display ("-");

  Calculator c1 = Calculator (6, 3);	// initializing object 'c' explicitly
   c1.sum ();
  c1.display ("+");
  c1.mul ();
  c1.display ("*");
  c1.div ();
  c1.display ("/");
  c1.diff ();
  c1.display ("-");
  return 0;
}				//end of program

Output

8+2 = 10
8*2 = 16
8/2 = 4
8-2 = 6
6+3 = 9
6*3 = 18
6/3 = 2
6-3 = 3

Types of Constructor

1. Default Constructor

A constructor without parameters is called default constructor.  If no any constructor definition, the compiler will define default constructor with public access specifier.

Structure of default constructor

 class class_name
 {
 	...
 	 public:
 	     class_name() { }; //default constructor
 	     ...
 };

2. Parameterized constructor

Constructors that take arguments are called parameterized constructor.

In above example,

Calculator (int a, int b)
  {
    val1 = a;
    val2 = b;
  }

Here, Calculator (int a, int b) is the constructor that takes parameter a  and b .

3. Copy Constructor

Copy constructor is used to create copy the existing Object of class type.

Syntax of copy constructor

class_name(const class-name &object_name)
{
     //constructor body
}

The copy constructor for class must have one of the following signatures:

  class( const class &other );
  class( class &other );
  class( volatile const class &other );
  class( volatile class &other );

Example of copy constructor

/*.....A program to highlight the concept of copy constructor.......... */
#include<iostream>
using namespace std;
class demo
{
private:
  int x;
public:
    demo (int a)
  {
    x = a;
  }
  demo (demo & b)		//copy constructor with reference object argument
  {
    x = b.x;
  }
  int display ()		//function to display
  {
    return x;
  }
};

int
main ()
{
  demo c1 (7);			//initializing object 'c' implicitly
  demo c2 (c1);			//copy constructor called
  demo c3 = c1;
  demo c4 = c2;
  cout << "value of c1 = " << c1.display () << endl;
  cout << "value of c2 = " << c2.display () << endl;
  cout << "value of c3 = " << c3.display () << endl;
  cout << "value of c4 = " << c4.display () << endl;
  return 0;
}				//end of program

Output

value of c1 = 7
value of c2 = 7
value of c3 = 7
value of c4 = 7