Default construction versus construction with initial values

Discussion in 'C++' started by Ook, Oct 7, 2005.

  1. Ook

    Ook Guest

    I'm having trouble comprehending what exactly "default construction" is. I
    know how to provide a constructor with initial values, so that if I, for
    example, in my code do this:

    MyClass zoot(1,2);

    It would instantiate MyClass, passing 1 and 2 to my constructor which in
    turn would do whatever I want it to. Would a default constructor be as
    follows:

    MyClass zoot;

    With no parameters passed? In my class declaration, would I do this:

    class Zoot
    {
    Zoot(); // default constructor?
    Zoot( int a, int b); // constructor with initial values.
    ....
    }

    Zoot::Zoot()
    {
    // default constructor, no parameters passed, do whatever you want?
    }
    Zoot::Zoot( int a, int b )
    {
    // constructor with initial values
    }

    Am I on the right track? IOW, a default constructor is simply a constructor
    that receives no parameters, but you can still write code to do what you
    want with it?
    Ook, Oct 7, 2005
    #1
    1. Advertising

  2. Ook

    Guest

    Ook wrote:
    > I'm having trouble comprehending what exactly "default construction" is.


    >From the FAQ:


    A "default constructor" is a constructor that can be called with no
    arguments.


    So to stay with your example, this could be a default constructor:

    class Zoot()
    {
    public:
    Zoot(); // Default constructor
    }

    But this could be a default constructor as well:

    class Zoot()
    {
    public:
    Zoot( int a = 0, int b = 5); // Default constructor
    }

    Since it has default values for a and b, you can still create a new
    obect like so:

    Zoot myObj;

    However, this ...

    class Zoot()
    {
    public:
    Zoot( int a, int b); // NOT a default constructor
    }

    .... is NOT a default constructor and the compiler will create one for
    you implicitly.

    > Am I on the right track? IOW, a default constructor is simply a constructor
    > that receives no parameters, but you can still write code to do what you
    > want with it?


    Pretty much.

    Cheers,
    Andre
    , Oct 7, 2005
    #2
    1. Advertising

  3. Ook wrote:
    > I'm having trouble comprehending what exactly "default construction" is. I
    > know how to provide a constructor with initial values, so that if I, for
    > example, in my code do this:
    >
    > MyClass zoot(1,2);
    >
    > It would instantiate MyClass, passing 1 and 2 to my constructor which in
    > turn would do whatever I want it to. Would a default constructor be as
    > follows:
    >
    > MyClass zoot;
    >
    > With no parameters passed?


    Yes, it would.

    > In my class declaration, would I do this:
    >
    > class Zoot
    > {
    > Zoot(); // default constructor?
    > Zoot( int a, int b); // constructor with initial values.
    > ...
    > }

    ;

    >
    > Zoot::Zoot()
    > {
    > // default constructor, no parameters passed, do whatever you want?
    > }


    I am not sure what you mean by "do whatever you want", but yes, this is
    the default c-tor.

    > Zoot::Zoot( int a, int b )
    > {
    > // constructor with initial values
    > }
    >
    > Am I on the right track? IOW, a default constructor is simply a constructor
    > that receives no parameters, but you can still write code to do what you
    > want with it?


    A default constructor is one that can be invoked without specifying any
    arguments. For example,

    class has_def_ctor {
    public:
    has_def_ctor(int a = 42);
    };

    also declares a default c-tor.

    V
    Victor Bazarov, Oct 7, 2005
    #3
  4. Ook

    Mike Wahler Guest

    "Ook" <Don't send me any freakin' spam> wrote in message
    news:...
    > I'm having trouble comprehending what exactly "default construction" is. I
    > know how to provide a constructor with initial values, so that if I, for
    > example, in my code do this:
    >
    > MyClass zoot(1,2);
    >
    > It would instantiate MyClass, passing 1 and 2 to my constructor which in
    > turn would do whatever I want it to. Would a default constructor be as
    > follows:
    >
    > MyClass zoot;
    >
    > With no parameters passed? In my class declaration, would I do this:
    >
    > class Zoot
    > {
    > Zoot(); // default constructor?
    > Zoot( int a, int b); // constructor with initial values.
    > ...
    > }
    >
    > Zoot::Zoot()
    > {
    > // default constructor, no parameters passed, do whatever you want?
    > }
    > Zoot::Zoot( int a, int b )
    > {
    > // constructor with initial values
    > }
    >
    > Am I on the right track? IOW, a default constructor is simply a
    > constructor that receives no parameters, but you can still write code to
    > do what you want with it?


    Almost. A default constructor is a constructor which can be
    invoked with no arguments. It might or might not have formal
    parameters. If it does define parameters, and they all have
    been given default values, then it is considered a default
    constructor (because it can still be called with no arguments).

    class C
    {
    public:
    C() {} // 1) default ctor
    C(int i = 0) {} // 2) default ctor
    C(int i = 0, int j = 42, int x = 99) // 3) default ctor
    C(int i) {} // 4) not a default ctor
    C(int i, int j = 0) {} // 5) not a default ctor
    };

    Note that the above contains errors, because a class can
    only have one default constructor. So all but one of
    1), 2), and 3) would need to be removed.

    -Mike
    Mike Wahler, Oct 7, 2005
    #4
  5. Ook

    Guest

    I have a confusion about this. If you define a ctor with params. Does
    compiler still create a default ctor for you?
    wm
    , Oct 7, 2005
    #5
  6. wrote:
    > I have a confusion about this. If you define a ctor with params. Does
    > compiler still create a default ctor for you?


    The compiler never generates a default c-tor in presence of _any_ other
    user-defined c-tor.

    V
    Victor Bazarov, Oct 7, 2005
    #6
  7. Ook

    Mike Wahler Guest

    <> wrote in message
    news:...
    >I have a confusion about this. If you define a ctor with params. Does
    > compiler still create a default ctor for you?


    No.

    -Mike
    Mike Wahler, Oct 7, 2005
    #7
  8. hrmmm....just wondering. If I made a default constructor

    class Object
    {
    public:
    Object( int a=5, int b = 5 );
    };

    I could still make a call in main like:

    Object myObj( 7 , 38 );

    which would call the default using 7 and 38 instead of default 5,5.
    right?

    Also, one more question. What's the difference between making a call:
    Object myObj;
    and
    Object myObj(); //is this not allowed?
    I had problems doing it, but i dont see why i should
    ctrl4ltdeleteme, Oct 7, 2005
    #8
  9. Ook

    Mike Wahler Guest

    "ctrl4ltdeleteme" <> wrote in message
    news:...
    > hrmmm....just wondering. If I made a default constructor
    >
    > class Object
    > {
    > public:
    > Object( int a=5, int b = 5 );
    > };
    >
    > I could still make a call in main like:
    >
    > Object myObj( 7 , 38 );
    >
    > which would call the default using 7 and 38 instead of default 5,5.
    > right?


    Correct.

    > Also, one more question. What's the difference between making a call:
    > Object myObj;


    This defines an object of type 'Object'.
    This form requires that 'Object' has defined
    a default constructor.

    > and
    > Object myObj(); //is this not allowed?


    Yes, it's 'allowed', but it doesn't do what you perhaps
    believe. It does not define an object. It declares
    (but does not define) a function, named 'myObj', which
    takes no arguments, and returns a value of type 'Object'.
    IOW it's a function prototype (a.k.a. declaration).

    > I had problems doing it,


    What problems specifically. That line should compile just
    fine as long as the definition of type 'Object' is in scope
    at that point. BTW 'Object' is about the worst possible
    name for a type. An object is not a type, and object *has*
    a type.


    > but i dont see why i should


    Obviously because you have limited understanding of C++.
    Which C++ books are you reading? (If none, why not?).
    See the book reviews at www.accu.org for recommendations.

    -Mike
    Mike Wahler, Oct 7, 2005
    #9
  10. hah. I just looked at it again and realized why that wouldn't work.

    Yeah, I am in the process of migrating from java. Though I wouldn't
    consider myself particularly well versed in either. I was currently
    reading a book by deitel on C++, however the peer review verbally hung
    it from the gallows so I just ordered
    The C++ Programming Language Special Edition. Hopefully that will do
    me some good. Thanks for the info.
    ctrl4ltdeleteme, Oct 8, 2005
    #10
  11. Ook

    Ook Guest

    > A default constructor is a constructor which can be invoked with no
    > arguments.


    Ahh - there is the piece I was missing. "no arguments". Thanks, and as
    always thanks to everyone else that pitched in :)
    Ook, Oct 8, 2005
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthew Louden
    Replies:
    1
    Views:
    6,874
    Scott M.
    Oct 11, 2003
  2. Russ

    script versus code versus ?

    Russ, Jun 10, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,478
  3. Sean Dockery
    Replies:
    3
    Views:
    870
    Sean Dockery
    Jun 21, 2009
  4. Morris Neuman

    How to show initial default values in a detailsview form

    Morris Neuman, Jan 20, 2007, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    309
    Steven Cheng[MSFT]
    Feb 15, 2007
  5. Paul Butcher
    Replies:
    12
    Views:
    686
    Gary Wright
    Nov 28, 2007
Loading...

Share This Page