C++ Constructor Tutorial with Examples – POFTUT

C++ Constructor Tutorial with Examples

A constructor is used in object-oriented programming languages in order to initialize an object which will be created from a class type. In C++ constructor is automatically called in order to initialize an object in different ways.

What Is Constructor?

A constructor is defined inside the class with the same name as the class. The name must be same with the class because it will be marked as a constructor automatically. The constructor should be public which means it should be put public definitions of the class. Constructor names are also case sensitive which should be the same case with the class name.  The constructor also have the same name as the class.

How Does Constructor Work?

The constructor will work like a function that will be called automatically during object initialization. The initialization can be done in different ways like below.

Types Of Constructors

Inside a class definition, we do not have to create a constructor explicitly where the compiler will create a default constructor implicitly which will just initialize the object. But we have different options in order to create constructor like the following:

  • Default Constructor
  • Parameterized Constructor
  • Dynamic Constructor

Default Constructor

We will start with an explicit default constructor. We will put the Point() constructor definition under the public: part and we will also add curly braces { } to create default constructor code block. We will put inside the default constructor code block the code we will run during object initialization or creation. The code is simple where we will set x and y variables to the 0.

Parameterized Constructor

In the previous example, we have just called the constructor to initialize the object. But we do not provide any value for the initialization. We can also use parameterized constructors in order to provide some values for the initialization which will set to the created object. A parameterized constructor is very same with the default constructor where we will add some parameters as arguments to the constructor function. In this following example, we will set the x and y by providing them to the constructor as value.

LEARN MORE  What Is A Function and How To Create Function In PHP, Python, JavaScript, C/C++, C#, Bash, Java , PowerShell Programming Languages?

Constructor Overloading

In the previous examples, we have defined only one constructor for each time but C++ provides the ability to define multiple constructors for different use cases. Defining multiple constructors for a single class is named constructor overloading. The most important restriction about constructor overloading is the same signature can not be used multiple times. This means the same parameter count and types can not be used multiple times. Let’s look to the example where we will define a constructor which do not requires a parameter and the second constructor which accepts two parameters.

We will use the following constructors:

  • Point() constructor will do not get any parameter where during initialization it will set x and y variables to the 0.
  • Point(int new_x, int new_y) will get new_x and new_y arguments and set their values to the x and y.

Copy Constructor

As from a single class, multiple objects can be created and initialized. In some cases, we may need to use already created object values to be used in the newly created object. We can do this by using copy constructors which will get the existing object pointer and copy the existing object values to the newly created object during initialization inside the constructor.

Dynamic Constructor

In general, during the object initialization process, the memory allocation sizes are fixed. Every type like int, char, object etc has specific sizes. But we can also allocate memory dynamically during the contructor call or initialization. In the following example, we will create a char array dynamically allocated. The char array c memory is allocated and then put point to this variable.

LEARN MORE  C++ Hello World Application

Constructor vs Member Function

As constructors act similar to the member function of the class you may ask yourself that what is the difference between constructor and member function or similarities. Here are some of the most important of them.

  • Constructors do not have a return value or type where member functions do have a return value or type even it is void.
  • Constructors are may be called automatically or explicitly according to usage where member functions are called explicitly.
  • Constructors do have the same name with its class where member functions can/should have different names then it’s class
  • Constructors are created automatically if we do not specify one by the compiler where member function does not create implicitly.

Leave a Reply

Your email address will not be published. Required fields are marked *

Enjoy this blog? Please spread the word :)