Classes in C++ are the main building blocks of Object Oriented Programming Tools. They have a special function known as a constructor which is automatically called when an object of a class is created. There is a default constructor type in classes that is called when a class is defined with no arguments, or it is defined with an empty parameter list, or with default arguments provided for every parameter. In this post, we are listing here all default constructor types in C++ with detailed posts and examples for each of them.
Table of Contents
What is a constructor in a modern C++ class?
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. To create a Constructor, we should use the same name of the class, followed by ( and ) parentheses as below,
There are different constructor 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 Do you want to learn what is default constructor or what kind of methods we have that we can declare and use default constructors ? In this post, we will try to explain Default Constructor with examples.
What is a default constructor in a modern C++ class?
A 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 DefaultConstructible;
In definition of a default Constructor, class_name must name the current class or current instantiation of a class template. It must be a qualified class name when declared at namespace scope or in a friend declaration.
How can we define and declare default constructors in modern C++?
Now, let’s see default constructor types in C++ with detailed posts and examples for each of them.
Here’s how to declare a Default Constructor inside of a C++ class definition
We can declare a default constructor inside of a class. Just add class name with ( and ) inside that class as below; create a method
Syntax,
An example with a class;
For more details and examples please see the following.
How to define the Default Constructor outside of a C++ Class Definition
We can define a default constructor outside of a class. Add a class name and ( ); inside the class also add class name and :: operator with class name and ( ) outside of that class as below,
Syntax,
An example with a class;
For more details and examples please check this below,
What is a Trivial Default Constructor in C++?
A Trivial Default Constructor is a constructor that does not act. All data types compatible with the C are trivially default-constructible. Here is a simple class that includes trivial default constructor which is not seen inside.
For more details and examples please check this below,
What is an Eligible Default Constructor in C++?
A default constructor is eligible if it is either user-declared or both implicitly-declared and definable
What is a Defaulted Default Constructor in C++?
We can define a defaulted default constructor in a class with = operator and default statement. Thus, the compiler will define the implicit default constructor even if other constructors are present.
Syntax,
An example with a class;
For more details and examples please check this below,
How do I use a Defaulted Default Constructor outside of a C++ Class definition?
We can easily default the Default Constructor by using = default; after class name with (); We can also do this outside of a Class Definition. The default statement means that you want to use the compiler-generated version of that function, so you don’t need to specify a body. We can also use = delete to specify that you don’t want the compiler to generate that function automatically.
Syntax,
An example with a class;
For more details and examples please check this below,
What is a Deleted Default Constructor in C++?
We can easily default the Default Constructor by using = default; after class name with (); We can do this outside of a Class Definition. The default statement means that you want to use the compiler-generated version of that function, so you don’t need to specify a body.
We can also use = delete to specify that you don’t want the compiler to generate that function automatically. In another way, = delete means that the compiler will not generate those constructors when declared and this is only allowed on copy constructor and assignment operator.
There is also = 0 usage; it means that a function is purely virtual and you cannot instantiate an object from this class. You need to derive from it and implement this method. Thus, the term = 0 is not the same as the term = delete.
Syntax,
An example with a class;
For more details and examples please check this below,
What is an Implicitly Declared Default Constructor in C++?
In C++ programming, If there is no constructor in the class (in the struct or in 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.
Here is an Implicitly-Declared Default Constructor example,
For more details and examples please check this below,
What is an Implicitly Defined Default Constructor in C++?
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,
For more details and examples please check this below,
What is a Deleted Implicitly-Declared Default Constructor in C++?
In C++ programming, 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. If we don’t define a constructor in a class and the implicitly-declared default constructor is not trivial, we can inhibit the automatic generation of an implicitly-defined default constructor by the compiler with the keyword delete.
Here is an Implicitly-Declared Default Constructor example that gives Error,
For more details and examples please check this below,
You can use the search function to find more examples about constructors in C++ on LearnCPlusPlus.org
C++ Builder is the easiest and fastest C and C++ compiler and IDE for building simple or professional applications on the Windows operating system. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from h