constructor is a special member function in c++ because its name is the same as classname.
it should be declared in the public section.
Constructors do not have a return type, not even void.
Constructor is called automatically when an object of the class is created.
it is used to initialize the data member of a class.
it may have a default argument.
The syntax to Define Constructor Inside the Class is :
classname (argument_list)
The syntax to Define Constructor Outside the Class is :
classname :: classname(argument_list)
Types of Constructors in C++ :
- Default Constructor
- Parameterized Constructor
- Copy Constructor
- Dynamic Constructor
Also read this :- operator overloading in c++.
1. Default Constructor in c++
A Constructor which contains no parameter is called Default Constructor.
The syntax to Define the Default Constructors in c++ is :
class classname { public : classname() // This Constructor is Default Constructor }
Here’s an example of a Default Constructors declaration in c++
#include<iostream> using namespace std; class item { int no;   int price;   public :   item() // item is a name of Constructor   {    cout<<"Enter the No & Price : n";    cin>>no>>price;   }   void putdata()   {   cout<<"No = "<<no<<endl;    cout<<"Price = "<<price;   } }; int  main() {   item x1;   x1.putdata();   return 0; }
output
Enter the No & Price : 2 43 No = 2 Price = 43
2. Parameterized Constructor in c++
A Constructor which contains an Argument is called Parameterized Constructor.
The syntax to Define Parameterized Constructors in c++ is :
class classname { public : classname(list_of_arguments) // This Constructor is Parameterized Constructor }
3. Copy Constructor in c++
A Constructor which contains a Reference of the Object same Class as an Argument then it is called Copy Constructor.
The syntax to Define Copy Constructor is :
class classname { public : classname(classname con) // This Constructor is Copy Constructor }
4. Dynamic Constructors in c++
it is used to Allocate Memory at the time of Creating an Object
Here’s an example of a Dynamic Constructors declaration in c++
classs classname { public:   classname(char *variable) // This Constructor is Dynamic Constructor   {    //code   } }