-1

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?

Saeed
  • 7,262
  • 14
  • 43
  • 63
baljeet Singh
  • 317
  • 3
  • 7
  • 11
  • 7
    Perhaps you'd be better off by reading a good book on C++. This is a fairly fundamental concept, and if you're struggling with this, chances are you'll hit another obstacle very soon. (To answer the question: a default constructor is any constructor that can be called with no arguments.) – Kerrek SB Oct 02 '11 at 12:18
  • 2
    Oh, the concept for such classes is called `DefaultConstructible`. ;) – pmr Oct 02 '11 at 12:20
  • can anyone explain it with simple example? – baljeet Singh Oct 02 '11 at 12:22
  • 4
    @baljeetSingh Every C++ book comes with exhaustive examples on this topic. SO isn't a "tutorial on demand' page. – pmr Oct 02 '11 at 12:25
  • @baljeetSingh Please consider choosing your best answer, if you found it useful –  Oct 02 '11 at 12:50

4 Answers4

2

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.

pmr
  • 58,701
  • 10
  • 113
  • 156
1
  • Constructor is a special function, without return type. Its name must be as the class\struct name. It doesn't have an actual name as a function, as Kerrek-SB pointed out.
  • Default constructor is the one that has no parameters, or has parameters all with a default value.
  • Constructor function is being called only once - when an object is instantiated
  • Constructor is called through a new expression or an initialization expression. It cannot be called "manually".
  • Useful for initializing object's fields, usually with a member initializer list.

Check this.

Community
  • 1
  • 1
  • 2
    A constructor with a single argument that has a default value is also a default constructor. – pmr Oct 02 '11 at 12:25
  • Better way to state point 3 is that "constructor is called through a `new` expression or a initialization expression". Arguably, a placement-new is sort of a "manual" invocation. You could also mention that a constructor doesn't have a name. – Kerrek SB Oct 02 '11 at 12:36
  • @Mr.DDD: ad (1), the constructor really doesn't have *any* name. Using the class name is just special syntax for the definition, but as a function, a constructor doesn't have a name. – Kerrek SB Oct 02 '11 at 12:42
  • -1 "Default constructor is the one that has no parameters, or has parameters all with a default value." is wrong. there is also more wrong. but one thing at a time. – Cheers and hth. - Alf Oct 02 '11 at 14:25
  • @Mr.DDD: because it's incompatible with the Holy Standard's definition. – Cheers and hth. - Alf Oct 02 '11 at 15:36
  • @AlfP.Steinbach hahaha. look here my friend to knock ignorance: http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr376.htm –  Oct 02 '11 at 16:43
  • @Mr.DDD: be a good citizen and inform IBM of their error. The Holy Standard is quoted in one of the answers here. That's the final authority. – Cheers and hth. - Alf Oct 02 '11 at 19:46
  • @AlfP.Steinbach Ok, sir :) can you explain what the differance is between `doesn't have args or have with a default value` and `can be called without arguments` :-) –  Oct 02 '11 at 21:30
  • Mr.DDD: for example, the Holy Standard allows `Foo(...)` as default constructor of class `Foo`. – Cheers and hth. - Alf Oct 02 '11 at 21:35
  • @AlfP.Steinbach Didn't you notice that `Foo(...)` is covered with my definition, too ? However, thanks! I have just known that there can be a `Foo(...)` constructor, as an empty `catch` block. This maybe useful for me later! –  Oct 02 '11 at 21:43
1

If you don't need to do anything as your class is instantiated. Use the default constructor, any situation else you will have to use your own constructor as the default constructor basically does nothing. You also don't need to write any "default" constructor.

class abc {
};

int main() {
abc a;  //don't want to do anything on instatiation
system("pause");
return 0;
}

class abc {
private:
int a;
public:
abc(int x) { a = x };
}


int main() {
abc a(1); //setting x to 1 on instantiation
system("pause");
return 0;
}
Sim
  • 4,199
  • 4
  • 39
  • 77
  • so when could be the situation when i need to write abc() {} in the program. i read somewhere that when you explicitly define the variable using constructor you need to define default constructor by yourself. – baljeet Singh Oct 02 '11 at 12:31
  • 1
    yes. You need to define a constructor which will then be the default one as it is then unpossible to call the class without passing the arguments required by that constructor. But this constructor is not called the default constructor anymore. Like on my second example, you will not be able to instantiate abc without pasing an integer to the constructor. – Sim Oct 02 '11 at 12:39
-1

Default constructor is constructor with no argument and will be called on these situations:

  • Instancing or newing an object of a class without any constructor, like:

    abc a;
    abc* aptr=new abc;
    
  • Declaring an array of a class, like:

    abc a_array[10];
    
  • When you have a inherited class which does not call one of base class constructors

  • When you have a feature in your class from another class and you don't call a definite constructor of that feature's class.
  • When you use some containers of standard library such as vector, for example:

    vector <abc> abc_list;
    

In these situations you have to have a default constructor, otherwise if you do not have any constructor, the compiler will make an implicit default constructor with no operation, and if you have some constructors the compiler will show you a compile error. If you want to do one of the above things, use a default constructor to make sure every object is being instantiated correctly.

Saeed
  • 7,262
  • 14
  • 43
  • 63