In C++, there are four types of constructors: default constructors, parameterized constructors, copy constructors, and move constructors. Here’s an overview of each type:
1. Default constructor: A default constructor is a constructor that takes no arguments. It is called when an object of the class is created without any arguments. If a class does not define any constructors, the compiler automatically generates a default constructor. The default constructor initializes all data members to their default values.
2. Parameterized constructor: A parameterized constructor is a constructor that takes one or more arguments. It is called when an object of the class is created with arguments. The arguments are used to initialize the data members of the object.
3. Copy constructor: A copy constructor is a constructor that creates a new object as a copy of an existing object of the same class. It takes a reference to an object of the same class as an argument. The copy constructor is called automatically when an object is created as a copy of another object using the assignment operator (=) or when passing an object as a function argument by value.
class Person { public: string name; int age; double height; Person(const Person& other) { name = other.name; age = other.age; height = other.height; } };
In this example, a copy constructor is defined for the `Person` class. It takes a const reference to another `Person` object as an argument and initializes thedata members of the new object to the same values as the existing object.
4. Move constructor: A move constructor is a constructor that creates a new object by “stealing” the resources (such as memory) from an existing object, leaving the existing object in a valid but unspecified state. It takes an rvalue reference to an object of the same class as an argument. The move constructor is called automatically when an object is created by moving another object using the move constructor or the move assignment operator.
class Person { public: string name; int age; double height; Person(Person&& other) { name = std::move(other.name); age = std::move(other.age); height = std::move(other.height); } };
In this example, a move constructor is defined for the `Person` class. It takes an rvalue reference to another `Person` object as an argument and initializes the data members of the new object by moving the data members from the existing object using the `std::move()` function.
Constructors are used to create and initialize objects of a class. Default constructors are called when an object is created without any arguments, while parameterized constructors are called when an object is created with arguments. Copy constructors are called when an object is created as a copy of an existing object, and move constructors are called when an object is created by moving another object. By defining these different types of constructors, you can provideflexibility and control over how objects are created and initialized in your program.