Search code examples
c++programming-languages

What is the concept of default constructor?


help me in getting the concept of default constructor with example. i don't know when to use default constructor in the program and when not to. help me coming over this problem.explain it with an example for me. when it is necessary to use it?

#include<iostream>
using namespace std;

class abc
{
public:
    abc()
    {
        cout<<"hello";
    }
};

int main()
{
    abc a;
    system("pause");
    return 0;
}

so actually what is the use of default constructor and when it is necessary to use it?


Solution

  • A class that conforms to the concept DefaultConstrutible allows the following expressions (paragraph 17.6.3.1 of N3242):

    T u; // object is default initialized
    T u{}: // object is value intialized
    T(); T{}; // value initialized temporary
    

    So much for the concept. Paragraph 12.1/5 actually tells us what a default constructor is

    A default constructor for a class X is a constructor of class X that can be called without an argument. If there is no user-declared constructor for class X, a constructor having no parameters is implicitly declared as defaulted (8.4). An implicitly-declared default constructor is an inline public member of its class. ...

    With the introduction of deleted special member functions, the standard also defines a list of cases where no implicit default constructor is available and the distinction of trivial and non-trivial default constructors.