C++C++11C++14C++17Introduction to C++Learn C++

Learn Implicitly Defined Default Constructor in C++


The Constructor
 in C++ is a function, a method in the class, but it is a ‘special method’ that is automatically called when an object of a class is created. We don’t need to call this function. Whenever a new object of a class is created, the Constructor allows the class to initialize member variables or allocate storage. This is why the name Constructor is given to this special method.

There are different contractor types and the Default Constructor in classes is one of these. This method not only used in classes but also used with struct and union data types.

Default Constructor is a constructor type in Classes that is called when class is defined with no arguments, or it is defined with an empty parameter list, or with default arguments provided for every parameter. A type with a public default constructor is Default Constructible; There are different methods to use this Default Constructor and one of these is using an Implicitly-Defined Default Constructor. If you are looking for how do we define a default constructor implicitly, here are details.

Implicitly Defined Default Constructor

In C++ programming, If there is no constructor in the class (in the struct or the union), the C++ compiler will always declare a default constructor as an inline public member of that class. If there is a declared default constructor, we can force the automatic generation of a default constructor in a new class by the compiler that would be implicitly declared otherwise with the keyword default. We can also implicitly define (not ‘declare’) a default constructor in another class. If we define a constructor in a class, we can also use this class and its constructor in another class. The difference between the Implicitly Defined Default Constructor and Implicitly Declared Default Constructor is here we defined a new class member and still, our class has its default constructor while it can use the default constructor from its new class member.

Here is an Implicitly-Defined Default Constructor example,

The example above is same like as below;

Here is a full Implicitly-Defined Default Constructor example,

As you see we defined test object with my_other_class and our my_class() constructor is automatically generated in this object when compiled. Please check our “Learn Implicitly Declared Default Constructor in C++” article to understand difference between implicitly declared and implicitly defined default constructors.

Get started building powerful apps with C++Builder!

close

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++Introduction to C++Language FeatureLearn C++

How To Get A Directory Name From A Path String

C++Introduction to C++Language FeatureLearn C++

How To Delete A File On Windows In C++ Builder

C++Introduction to C++Language FeatureLearn C++

How To Delete A Directory In C++ Builder On Windows

C++C++11C++14C++17Introduction to C++Learn C++

How to Delete, Clear, Erase A C++ Vector Member On Windows

Worth reading...
Learn Defaulted Default Constructor in C++
en_USEnglish