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 | Constructors and Destructors
C++ OOP
course content

Зміст курсу

C++ OOP

C++ OOP

1. Fundamentals of OOP
2. Constructors and Destructors
3. Encapsulation Overview
4. Inheritance Overview
5. Polymorphism Overview

bookWhat is a Constructor of the Class

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:

  • 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

copy
1234567891011121314
#include <iostream> class Example { public: Example() { std::cout << "Object was created!"; } }; int main() { Example obj; }

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

copy
123456789101112
#include <iostream> class Example { public: int value; }; int main() { Example obj; // Default constructor std::cout << obj.value; }

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

copy
123456789101112131415161718
#include <iostream> class MemberExample { public: MemberExample() { std::cout << "MemberExample: Constructor was called!" << std::endl; } }; class Example { public: MemberExample obj; }; int main() { Example obj; }
When constructor gets called?

When constructor gets called?

Виберіть правильну відповідь

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 1
some-alt