Constructors are special methods that are automatically called whenever an object of a class is created. A constructor is different from normal functions in following ways:
- A constructor has same name as the class itself
- Don't have return type
- Automatically called when an object is created.
- If we do not specify a constructor, C++ compiler generates a default constructor for us (expects no parameters and has an empty body).
Example:
#include <iostream>
using namespace std;
class A {
public:
// Constructor without any parameters
A() {
cout << "Constructor called" << endl;
}
};
int main() {
A obj1;
return 0;
}
Output
Constructor called
Explanation: In the above program, we have defined a constructor for class A. In the main function, when we create an object of that class, this constructor is called, which prints "Constructor called".
Types of Constructors in C++
Constructors can be classified based on the situations they are being used in. There are 4 types of constructors in C++:
- Default Constructor
- Parameterized Constructor
- Copy Constructor
- Move Constructor
1. Default Constructor
A default constructor is automatically created by the compiler if no constructor is defined. It takes no arguments and initializes members with default values, and it is not generated if the programmer defines any constructor.
#include <iostream>
using namespace std;
// Class with no explicity defined constructors
class A {
public:
};
int main() {
// Creating object
A a;
return 0;
}
Explanation: The class A does not contain any explicitly defined constructor, so its object is created without passing any parameters. In this case, the compiler automatically provides and uses the default constructor.
2. Parameterized Constructor
A parameterized constructor lets us pass arguments to initialize an object's members. It is created by adding parameters to the constructor and using them to set the values of the data members.
#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
Explanation: 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.
#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(20);
// Creating another object from a1
A a2(a1);
cout << a2.val;
return 0;
}
Output
20
Explanation: 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.
Note: If no copy or move constructor is defined, the compiler automatically creates an implicit copy constructor, unlike the default constructor which is removed when any constructor is defined.
4. Move Constructor
A move constructor is a special constructor in C++ that creates an object by transferring resources from another object instead of copying them. It uses move semantics (often with std::move) to take ownership of memory or handles from a temporary object, avoiding extra copies and improving performance. It is typically called when objects are returned by value or when temporaries are used.
#include <iostream>
#include <vector>
using namespace std;
class MyClass {
private:
int b;
public:
// Constructor
MyClass(int &&a) : b(move(a)) {
cout << "Move constructor called!" << endl;
}
void display() {
cout << b <<endl;
}
};
int main() {
int a = 4;
MyClass obj1(move(a)); // Move constructor is called
obj1.display();
return 0;
}
Output
Move constructor called! 4
Explanation: In the above program, MyClass uses a constructor with an rvalue reference (int&&) to move a value into its member using std::move. When an object is created with std::move, the move constructor transfers ownership of resources instead of copying them. If you don’t define one, the compiler automatically generates a move constructor (just like it does for copy constructors).
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.