C++ constructor is used to initializing the member variables of an instance of a class that is the object of a class.  A constructor is a particular type of member function that initializes an object automatically when it is created. The compiler identifies a given member function is a constructor by its name and the return type.

The  constructor has the same name as that of a class, and it does not have any return type. Also, the constructor is always public.

C++ Constructor Example

A  constructor is the member function of a class. But they do differ from actual function in the following ways:

  1. The constructor has the same name as the class name.
  2. The constructor doesn’t have a return type, unlike other member  functions.
  3. If there is no explicit definition of constructor in a class, a default constructor is automatically called.
  4. If we do not specify a constructor, the C++ compiler generates a default constructor for us (expects no parameters and has a hollow body).

#c++ #c++ constructor

C++ Constructor Example | Constructors in C++ Explained
2.45 GEEK