C++ Constructors

C++ Constructors: Constructors in C++ can be defined as a special member method which will be called implicitly (automatically) whenever an object of a class is created. In other words, it’s a member function that initializes a class which is called automatically whenever a new instance of a class is created. The main use of the constructor is placing user-defined values in place of default values.

C++ Constructors

Features

  • The constructor will be having the same name as a class name.
  • And it does not have any return type.

Syntax: 

class class_name
{
.........
public:class_name(); //constructor declared or constructor prototype
.........
};
class_name :: class_name() //constructor defined
{//constructor function body
}

Example:

#include<iostream>
using namespace std;
class Line {
   public:
      void setLength( double len );
      double getLength( void );
      Line();//This is the constructor
   private:
      double length;
}; 
//Member functions definitions including constructor
Line::Line(void) {
   cout << "Object is being created" << endl;
}
void Line::setLength( double len) {
   length = len;
}
double Line::getLength(void) {
   return length;
}
//Main function for the program
int main() {
   Line line;
//set line length
   line.setLength(6.0); 
   cout<<"Length of line : "<<line.getLength() <<endl;
   return 0;
}

Output:

The object is being created
Length of line: 6

Types of Constructors in C++

In C++ we are having four different types of constructors. They are as follows:

  • Do nothing constructor
  • Default constructor
  • Parameterized constructor
  • Copy constructor

Do nothing Constructor

The Do nothing constructors are that type of constructor which does not contain any statements. And Do nothing constructor is the one which has no argument in it and also no return type.

Default Constructor

The default constructors can be defined as a constructor that does not take any arguments. It has no parameters. The default constructor is very important for initializing object members, that even if we do not define a constructor explicitly, the compiler automatically provides a default constructor implicitly.

Syntax:

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

Example:

#include <iostream> 
using namespace std;  
class construct { 
public: 
    int a, b; 
  
    // Default Constructor 
    construct() 
    { 
        a = 10; 
        b = 20; 
    } 
}; 
  
int main() 
{ 
    // Default constructor called automatically 
    // when the object is created 
    construct c; 
    cout << "a: " << c.a << endl 
         << "b: " << c.b; 
    return 1; 
}

Output:

a: 10
b: 20

Parameterized Constructor

The parameterized constructor can be used to pass the arguments to the constructor. The arguments will help to initialize an object when it is created. It is used to provide different values to distinct objects. If you want to create a parameterized constructor, simply add parameters to it the way you would to any other function. When you define the constructor’s body, use the parameters to initialize the object.

Example:

#include <iostream>
using namespace std;
class Employee {
   public:
       int id;//data member (also instance variable)    
       string name;//data member(also instance variable)
       float salary;
       Employee(int i, string n, float s)  
        {  
            id = i;  
            name = n;  
            salary = s;
        }  
       void display()  
        {  
            cout<<id<<"  "<<name<<"  "<<salary<<endl;  
        }  
};
int main(void) {
    Employee e1 =Employee(201, "Chinnu", 890000); //creating an object of Employee 
    Employee e2=Employee(202, "Kartheek", 59000); 
    e1.display();  
    e2.display();  
    return 0;
}

Output:

201 Chinnu 890000
202 Kartheek 59000

Copy Constructor

A copy constructor can be defined as a member function that initializes an object using another object of the same class.

Syntax:

class-name (class-name &)
{
. . .
}

copy constructor in C++

Example:

#include <iostream>
using namespace std;
class CopyCon {
 int a, b;
public:
 CopyCon(int x, int y)
 {
 a = x;
 b = y;
 cout << "\nHere is the initialization of Constructor";
 }
 void Display()
 {
 cout << "\nValues : \t" << a << "\t" << b;
 }
};

int main()
{
 CopyCon Object(30, 40);
 //Copy Constructor
 CopyCon Object2 = Object;
 Object.Display();
 Object2.Display();
}

Output:

Here is the initialization of Constructor

Values: 30 40

Values: 30 40