Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
What is a Constructor of the Class

What is a Constructor of the ClassWhat is a Constructor of the Class

Constructor

Constructor is a special method of a class that is called when an object is created. With it you can initialize the attributes to meaningful values.

By using constructors, you can simplify the creation of objects. Without a constructor, you would have to set each property individually and might need to call multiple methods just to get the object into a usable state.

The primary purpose of a constructor is to initialize the newly created instance. It can set the initial values of the attributes based on the parameters passed to the constructor, ensuring the object starts in a valid state.

Syntax of Constructor

While the process of developing a constructor might appear straightforward, it involves numerous specific considerations. The general approach to creating one is:

code example
  • Name: A constructor has the same name as the class itself.
  • Return Type: Constructors do not have a return type, not even void.
  • Parameters: Constructors can take parameters. They can be overloaded with different sets of parameters to allow different ways of initializing objects.
  • Automatic Invocation: A constructor is called automatically by the compiler when an object is created. This includes creation with the new keyword, direct variable declaration, or through other means like copy construction.

Default Constructor

A default constructor is a constructor that can be invoked without any arguments. Here's how default constructors are defined and used:

cpp

main.cpp

You can experiment with a code above. The logic written in the Example() method is executed every time an instance of the class is created.

Note

Try to add one more object or change the logic inside the constructor.

When you create a class without explicitly defining any constructors, the compiler automatically generates a default constructor for you. Its behavior varies based on the types of member variables in your class, such as whether they are fundamental data types, have default constructors, or are user-defined types.

cpp

main.cpp

In the example above attribute is a fundamental data type. The automatically generated default constructor does not initialize built-in-type member variables. They will have indeterminate values if you create an object and do not explicitly initialize these fields.

Note

You might find that compiler provide a consistent value for value variable, but the standard doesn't require it and it is possible that it will contain a garbage value.

On the other hand, when employing user-defined types such as classes, the default constructor will invoke the default constructor of the member to initialize member object.

cpp

main.cpp

question-icon

When constructor gets called?

Select the correct answer

Everything was clear?

Section 2. Chapter 1
some-alt