Updated March 16, 2023
Introduction to Constructor in C++
The constructor may be defined as the special kind of method that has the name same as that of the class. The constructor gets invoked right after the object is initialized and is not required to call the constructor explicitly. Once the constructor is invoked, it assigns memory to the resources.
A constructor is a special class member function of a class that initializes objects i.e. class instance). In C++, Constructor has same name as the class itself. If object is created, Constructor is automatically called. Constructor can be defined either inside the class definition or outside the class definition using class name and scope resolution (::) operator.
It makes the application more efficient and using it can be considered as a good approach to programming. It can be declared as similar to the other methods. The only way it is different from other methods in that class is due to the name of the function. Like other functions, values can be passed to the construction while initialization. It can be of any return type based on the requirement of the program. In this topic, we are going to learn about Constructors in C++.
In order to implement the constructor in the program, one must have yo follow the correct syntax. The syntax has to be taken care of while initializing and declaring. Below is the syntax for declaration then we will look at the syntax for initialization.
1. Constructor Declaration:
Here the return type defines the type of data that has to be returned from this method. If the data type mentioned is integer then the value that the constructor will return will be the integer value and in the same way, there can be any data type used over there. The statements of the default constructor will start executing right after the instance of the class has been created.
2. Calling Constructor:
ClassName objectName = new ClassName();
ClassName objectName = new ClassName(argument1…. Argument n);
While calling the constructor, it might be needed to pass the value from where it has been called or it may not be required. Above are the syntax of both the ways to call the constructor. While bringing the constructor in use, one has to make sure that the syntax has to be followed correctly else it will lead to error.
How does Constructor Work in C++?
Before we can use a constructor in the program, we have to make sure to understand how it works so that we can use it correctly to make the program efficient. The way it works is very simple and contribute to the application for the betterment. The constructor is used in the program where we need to invoke a set of statements whenever the object for the particular class is created. The constructor is never required to be called explicitly.
Once you create the object or instance of the class, it will implicitly call the constructor and will execute a statement under that. In case if certain values have to be passed, you will need to pass the values at the point where the object of that class is being created. In the above section, we have discussed the syntax to pass the values to the constructor. It is the way the constructor works and it assists in making the program simple and efficient.
Types of Constructor in C++
Based on whether the values have to be passed to the constructor, it has been divided into two types: Default constructor and parameterized constructor. Below we will be discussing both the types.
1. Default Constructor
The default constructor may be defined as the type of constructor in which no value has to be passed and can be simply invoked by creating an instance of that class. While declaring the default constructor, we just need to use the return type, the constructor name, and the brackets. There is no need to write and parameter between the brackets that come right after the constructor name. Below is how the default constructor looks like.
In this example, there has been no value accepted and the return type is a string. What all it is required to call such a constructor is just the instance creation of that class.
2. Parameterized Constructor
The parameterized constructor may be defined as the kind of constructor that needs some values to be passed in it so that those could be used by the variables defined in the constructor. The values that have to be passed can be mentioned by the time of the initialization of the variable. When it comes to the declaration of the parameterized constructor, we will need to use the return type, the constructor name, the brackets and the parameters that will be used to bring the values in. It is similar to the default constructor and the only thing it has, in addition, is the values.
Int Employee(int empID, Sting empName);
In this example of parameterized constructor declaration, the return type is an integer and there are two values that the constructor will be accepting. It can be noted that the values of any data type could be passed as a parameter.
Examples of C++ Constructor
In order to make the concept of constructor more precise, we will see an actual example of how constructor can be used in the real program. In the below example, we have used the default constructor that will be called when the object of the class has been created.
In the above example, the class name is FirstExp and the constructor has been declared and defined within it. If the constructor invokes, it will lead to a print “constructor called”. In the main method, the object ‘f’ has been created for class FirstExp which invoked or called the default constructor and the output of the program was “constructor called”.
The constructor may be defined as the special feature of the programming languages which is used to make the program effective and efficient. It can also be considered as a special type of method that has the same name as that of the class and can be invoked whenever the object of that class is created. Based on the requirement of the constructor once can choose between the default and the parameterized constructor. It has to be understood that it can only be used in the case when there is something that has to be called immediately right after the instance of the class has been created.
This is a guide to Constructor in C++. Here we discuss the Constructor Types and How does the Constructor in C++ works. You may also look at the following article to learn more –