The Wayback Machine - https://web.archive.org/web/20250309032125/https://www.geeksforgeeks.org/constructors-c/
Open In App

Constructors in C++

Last Updated : 26 Feb, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

In C++, constructor is a special method that is invoked automatically at the time an object of a class is created. It is used to initialize the data members of new objects. The constructor in C++ has the same name as the class or structure.

Example:

C++
#include <iostream>
using namespace std;

class A {
public:
  
    // Constructor of the class without
    // any parameters
    A() {
        cout << "Constructor called" << endl;
    }
};

int main() {
    A obj1;
    return 0;
}

Output
Constructor called

Explanation: In this code, we defined a constructor for class A. In main function, when we create an object of that class, this constructor is called.

Types of Constructors in C++

Constructors can be classified based on in which situations they are being used. There are 4 types of constructors in C++:

  1. Default Constructor
  2. Parameterized Constructor
  3. Copy Constructor
  4. Move Constructor

1. Default Constructor

A default constructor is a constructor that doesn’t take any argument. It has no parameters. It is also called a zero-argument constructor. The compiler automatically creates an implicit default constructor if the programmer does not define one.

Example:

C++
#include <iostream>
using namespace std;

// Class with no explicity defined constructors
class A {
public:
};

int main() {
  
      // Creating object without any parameter
    A a;
      cout << "In Main";
    return 0;
}

Output
In Main

The object of the class A is created without any parameters, hence the default constructor exists.

2. Parameterized Constructor

Parameterized constructors make it possible to pass arguments to constructors. Typically, these arguments help initialize an object when it is created. 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:

C++
#include <iostream>
using namespace std;

class A {
public:
    int val;
  
      // Parameterized Constructor
    A(int x) {
        val = x;
    }
};

int main() {
  
      // Creating object with a parameter
    A a(10);
    cout << a.val;
    return 0;
}

Output
10

In this code, the parameterized constructor is called when we create object a with integer argument 10 . As defined, it initializes the member variable val with the value 10.

Note: If a parameterized constructor is defined, the non-parameterized constructor should also be defined as compiler does not create the default constructor.

3. Copy Constructor

A copy constructor is a member function that initializes an object using another object of the same class. Copy constructor takes a reference to an object of the same class as an argument.

Example:

C++
#include <iostream>
using namespace std;

class A {
public:
    int val;
    
    // Parameterized constructor
    A(int x) {
        val = x;
    }
    
    // Copy constructor
    A(A& a) {
        val = a.val;
    }
};

int main() {
    A a1(10);
    
    // Creating another object from a1
    A a2(a1);
    
      cout << a2.val;
    return 0;
}

Output
10

In this example, the copy constructor is used to create a new object a2 as a copy of the object a1. It is called automatically when the object of class A is passed as constructor argument.

Just like the default constructor, the C++ compiler also provides an implicit copy constructor if the explicit copy constructor definition is not present.

Here, it is to be noted that, unlike the default constructor where the presence of any type of explicit constructor results in the deletion of the implicit default constructor, the implicit copy constructor will always be created by the compiler if there is no explicit copy constructor or explicit move constructor is present.

4. Move Constructor

The move constructor is a recent addition to the family of constructors in C++. It is like a copy constructor that constructs the object from the already existing objects., but instead of copying the object in the new memory, it makes use of move semantics to transfer the ownership of the already created object to the new object without creating extra copies.

It can be seen as stealing the resources from other objects.

Syntax:

className (className&& obj) {
// body of the constructor
}

The move constructor takes the rvalue reference of the object of the same class and transfers the ownership of this object to the newly created object.

Like a copy constructor, the compiler will create a move constructor for each class that does not have any explicit move constructor.

Characteristics of Constructors

  • The name of the constructor is the same as its class name.
  • Constructors are mostly declared as public member of the class though they can be declared as private.
  • Constructors do not return values, hence they do not have a return type.
  • A constructor gets called automatically when we create the object of the class.
  • Multiple constructors can be declared in a single class (it is even recommended – Rule Of Three, The Rule of Five).
  • In case of multiple constructors, the one with matching function signature will be called.

If you are confused about what you should study next on this topic, refer to the article – A Comprehensive Guide to Constructors in C++: Everything You Need to Know

Frequently Asked Questions on C++ Constructors

What Are the Functions That Are Generated by the Compiler by Default, If We Do Not Provide Them Explicitly?

The functions that are generated by the compiler by default if we do not provide them explicitly are:

  1. Default Constructor
  2. Copy Constructor
  3. Move Constructors
  4. Assignment Operator
  5. Destructor

Can We Make the Constructors Private?

Yes, in C++, constructors can be made private. This means that no external code can directly create an object of that class.

How Constructors Are Different from a Normal Member Function?

A constructor is different from normal functions in following ways: 

  • Constructor has same name as the class itself
  • Default Constructors don’t have input argument however, Copy and Parameterized Constructors have input arguments
  • Constructors don’t have return type
  • A constructor is automatically called when an object is created.
  • It must be placed in public section of class.
  • If we do not specify a constructor, C++ compiler generates a default constructor for object (expects no parameters and has an empty body).

Can We Have More Than One Constructor in a Class?

Yes, we can have more than one constructor in a class. It is called Constructor Overloading.



Next Article
Practice Tags :

Similar Reads

three90RightbarBannerImg