The Wayback Machine - https://web.archive.org/web/20261123035018/https://www.geeksforgeeks.org/cpp/constructors-c/

Constructors in C++

Last Updated : 16 Nov, 2026

Constructors in C++ are special member functions that initialize objects when they are created. They have the same name as the class and do not have a return type.

  • Constructors are called automatically during object creation.
  • C++ supports default, parameterized, copy, and move constructors.
  • Multiple constructors can be defined in the same class using constructor overloading.
C++
#include <iostream>
using namespace std;

class A {
public:
    // Constructor
    A() {
        cout << "Constructor called";
    }
};

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

Output
Constructor called

Explanation: When obj is created, the constructor A() is automatically called.

Types of Constructors

C++ provides different constructors to initialize objects based on the arguments and initialization requirements.

123212
Types of Constructors

1. Default Constructor

A default constructor can be called without passing any arguments. If a class has no user-declared constructors, the compiler generally declares a default constructor automatically.

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

class A {
public:
    int val = 10;
};

int main() {
    A obj;
    cout << obj.val;
    return 0;
}

Explanation: Since no constructor is explicitly declared, the compiler provides a default constructor. The data member val is initialized to 10 using its default member initializer.

Note: A default constructor does not automatically initialize every built-in data member to zero.

2. Parameterized Constructor

A parameterized constructor accepts arguments to initialize an object's data members.

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

class A {
public:
    int val;

    // Parameterized constructor
    A(int x) {
        val = x;
    }
};

int main() {
    A obj(10);
    cout << obj.val;
    return 0;
}

Output
10

Explanation: The argument 10 is passed to the constructor and assigned to val.

Note: When a user-declared parameterized constructor exists, the compiler does not implicitly declare a default constructor. You can explicitly declare one if needed.

3. Copy Constructor

A copy constructor initializes a new object from an existing object of the same class. It commonly accepts a const reference.

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

class A {
public:
    int val;

    A(int x) : val(x) {}

    // Copy constructor
    A(const A& obj) {
        val = obj.val;
    }
};

int main() {
    A obj1(20);

    // Create a copy of obj1
    A obj2(obj1);

    cout << obj2.val;
    return 0;
}

Output
20

Explanation: The copy constructor initializes obj2 using the value stored in obj1.

Note: The compiler can implicitly declare a copy constructor when one is not explicitly declared, although it may be defined as deleted in certain cases.

4. Move Constructor

A move constructor initializes a new object using an rvalue of the same class. It can transfer ownership of resources instead of copying them.

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

class MyClass {
private:
    unique_ptr<int> value;

public:
    // Parameterized constructor
    MyClass(int x) : value(make_unique<int>(x)) {}

    // Move constructor
    MyClass(MyClass&& obj) noexcept
        : value(move(obj.value)) {
        cout << "Move constructor called!" << endl;
    }

    void display() const {
        cout << *value << endl;
    }
};

int main() {
    MyClass obj1(4);

    // Move resources from obj1 to obj2
    MyClass obj2(move(obj1));

    obj2.display();
    return 0;
}

Output
Move constructor called!
4

Explanation: The move constructor transfers ownership of the dynamically allocated integer from obj1 to obj2 using std::move. After the move, obj1.val no longer owns that integer.

Note: Move constructors are supported from C++11 onward. The compiler generates them implicitly only when the required conditions are satisfied.

Delegating Constructors

A delegating constructor allows one constructor to call another constructor of the same class using the class name in the initializer list. Unlike Java's this(), C++ uses syntax such as Student() : Student(18) {}.

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

class Student {
public:
    int age;

    // Parameterized constructor
    Student(int a) : age(a) {}

    // Delegating constructor
    Student() : Student(18) {}
};

int main() {
    Student s;
    cout << s.age;
    return 0;
}

Output
18

Explanation: The constructor Student() delegates initialization to Student(int) by passing the value 18.

Note: In C++, this is a pointer to the current object. The syntax this() cannot be used to invoke another constructor.

Characteristics of Constructors

  • Constructors can have public, private, or protected access.
  • Constructors cannot be declared virtual or static.
  • Member initializer lists can initialize data members and base classes.
  • Constructors can be explicitly declared = default or = delete.
Comment