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.
1 2 3 4 5 6 7 8 |
class myclass { public: myclass() { std::cout << "myclass is constructed!\n"; }; }; |
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 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 the Default Constructor with examples.
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 Default Constructible; There are different methods to use this Default Constructor and one of these is using an Implicitly-Declared Default Constructor. If you are looking for how do we declare a default constructor implicitly, here are details.
Implicitly Declared Default Constructor
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,
1 2 3 4 5 6 7 8 9 10 11 12 |
class my_class { public: my_class() // constructor { }; }; class my_other_class : my_class { // implicitly declared default constructor copied from the my_class }; |
The example above is same as below;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class my_class { public: my_class() // constructor { }; }; class my_other_class : my_class { public: my_class() // constructor { }; }; |
The implicitly-declared default constructor or the defaulted constructor on its first declaration has an exception specification as described in exception specification (C++17). Before C++17 it is described in dynamic exception specification.
Here is a full Implicitly-Declared Default Constructor example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include <iostream> class my_class { public: int start; my_class() { start = 100; }; }; class my_other_class : public my_class { // implicitly declared default constructor copied from the my_class }; int main() { my_other_class test; // This class has Implicitly-Declared Default Constructor std::cout << test.start ; // Let's check if it worked same as the previously decleared one getchar(); return 0; } |
As you see we declared my_other_class with public my_class and our my_class() constructor is automatically generated when compiled.