Is a special member function that is called automatically whenever a new object is created?

Published Mar 08, 2022Last updated Sep 03, 2022

Source

Constructor and Destructor are the special member functions of the class which are created by the C++ compiler or can be defined by the user. Constructor is used to initialize the object of the class while destructor is called by the compiler when the object is destroyed.

What is Constructor in C++?

A constructor is a special member function of a class and shares the same name as of class, which means the constructor and class have the same name. Constructor is called by the compiler whenever the object of the class is created, it allocates the memory to the object and initializes class data members by default values or values passed by the user while creating an object. Constructors don’t have any return type because their work is to just create and initialize an object.

What is the basic syntax of Constructor in C++?

The basic syntax of the constructor is given below:

class class_name{ private: // private members public: // declaring constructor class_name[{parameters}] { // constructor body } };

In the above syntax, we can see the class has the name class_name and the constructor have also the same name. A constructor can have any number of parameters as per requirements. Also, there is no return type or return value of the constructor.
Note: In the above syntax we have declared the constructor as a public member but we can declare it private also [we will discuss that later in this article].

Important Points about Constructors

Access specifiers

Constructors can be defined as public, protected, or private as per the requirements. By default or default constructors, which are created by the compiler are declared as the public. If the constructor is created as private, then we are not able to create its object.
When there is no requirement of the object of a class [in a situation when all class members are static] we can define its constructor as private.
Usually, constructors are defined as public because constructors are used to create an object and initialize the class data members for the object. An object is always created from outside of class, which justifies making constructors public.

Inheritance

As a derived class can access all the public properties of the base class, it can call its constructor also if it is not declared as private. Also, the constructor's address cannot be referenced.

Virtual

Constructor in C++ cannot be declared as virtual because when we declare any function of a class as a virtual function, at compile time compiler creates a virtual table to store the address of each function that is declared as virtual. Also, it creates a data member of the class virtual pointer to points towards the virtual table. But as we have discussed we cannot refer to the address of the constructor, which means we are not able to declare the constructor as virtual.

How many types of Constructors are present in C++?

There are four types of constructors in c++

  • Default constructor
  • Parameterized constructor
  • Copy Constructor
  • Dynamic Constructor

Let’s discuss them in detail

Default Constructor

Default constructor is also known as a zero-argument constructor, as it doesn’t take any parameter. It can be defined by the user if not then the compiler creates it on his own. Default constructor always initializes data members of the class with the same value they were defined.

Syntax

class class_name{ private: // private members public: // declaring default constructor class_name[] { // constructor body } };

Code to show the working of default constructor

#include using namespace std; class Person{ // declaring private class data members private: string name; int age; public: // declaring constructor Person[] { cout

Chủ Đề