Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
133 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...