What are Constructors in C++

  • In C++ programming language When a class object is created, its members can be initialized by a constructor function of that class
  • A constructor is a special member function with the same name as the class and no return data type
  • The programmer provides the constructor, which is then invoked each time an object of that class is created
  • Constructors may be overloaded to provide a variety of means for initializing objects of a class
  • Data members can be initialized in a constructor of the class or their values may be set later after the object is created

What is the Task of Constructor in C++?

Traditional programming languages only allocate memory for a variable being defined. The programmer must ensure that the variable is initialized with suitable values.

We know that Non-initialized objects can lead to serious runtime errors in your programs. To avoid errors of this type, C++ performs implicit initialization when an object is defined. This ensures that objects will always have valid data to work on. Initialization is performed by special methods known as constructors

Rules for declaration Constructor:

  • The name of the constructor is also the class name
  • A constructor does not possess a return type not even void.

The constructor is declared in the public class. It allows you to create objects of the class whenever the class definition is available.

Constructors can also perform more complex initialization tasks, such as opening files, allocating memory, and configuring interfaces.

Default Arguments:

A constructor without an argument(parameter) is known as a default argument. The default constructor is only called if an object definition does not explicitly initialize the object. If a class does not contain a constructor definition, the compiler will create a minimal version of the default constructor as a public member. But default constructor does not perform initialization

If a member function of a class already provides all or part of the functionality required by a constructor of the class, call that member function from the constructor. This simplifies the maintenance of the code and reduces the likelihood of an error if the implementation of the code is modified. As a general rule: Avoid repeating code.

Declare default function argument values only in the function prototype within the class definition in the header file.

Any change to the default argument values of a function requires the client code to be recompiled. If it is likely that the default argument values will change, use overloaded functions instead. Thus, if the implementation of a member function changes, the client code need not be recompiled.

Program for default Constructor:

#include<iostream> 
using namespace std; 
class Employee 
{ 
public: 
Employee() 
{ 
cout<<"Default Constructor Invoked"<<endl; 
} 
}; 
int main(void) 
{ 
Employee e1; //creating an object of Employee 
Employee e2; 
return 0; 
} 

OUTPUT:

 Default Constructor Invoked 
 Default Constructor Invoked 

Translate »