In C++, constructors are special methods that are automatically called whenever an object of a class is created. The constructor in C++ has the same name as the class or structure.
A constructor is different from normal functions in following ways:
- 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).
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;
}
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 generated by the compiler if the programmer does not define one. This constructor doesn't take any argument as it is parameter less and initializes object members using default values. It is also called a zero-argument constructor. However, the default constructor is not generated by the compiler if the programmer has explicitly defined a constructor.
C++
#include <iostream>
using namespace std;
// Class with no explicity defined constructors
class A {
public:
};
int main() {
// Creating object
A a;
return 0;
}
In the above program the class A does not contains any explicitly defined constructor. Hence, The object of the class A is created without any parameters, As the class will use the default constructor generated by the compiler.
2. Parameterized Constructor
Parameterized constructor allow us to pass arguments to constructors. Typically, these arguments help initialize an object's members. 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's members.
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;
}
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.
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(20);
// Creating another object from a1
A a2(a1);
cout << a2.val;
return 0;
}
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.
Note: 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.
The move constructor uses std::move() to transfer ownership of resources and it is called when a temporary object is passed or returned by value.
C++
#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;
}
OutputMove 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.
Explore
C++ Basics
Core Concepts
OOP in C++
Standard Template Library(STL)
Practice & Problems