inherited constructors

B

Brian Genisio

Hi all,

Please note the following code... Why is it that when you call the
child with the a constructor that has arguments, the parent constructor
gets called as default? How do I tell the Child(int) constructor to use
the Father(int) constructor?

My results are:
Father default constructor
Child Arg Constructor

//
// Sample Code:
//
#include <iostream.h>

// The parent class
class Father
{
public:
Father(int newX) : x(newX)
{ cout << "Father Arg Constructor" << endl; }

Father() : x(0)
{ cout << "Father default constructor" << endl;}

private:
int x;
};

// The child class, that inherits from the parent class
class Child : public Father
{
public:
Child(int newX)
{ cout << "Child Arg Constructor" << endl;}
};


int main(void)
{
Child xyz(4);
return 0;
}
 
C

Chris \( Val \)

| Hi all,
|
| Please note the following code... Why is it that when you call the
| child with the a constructor that has arguments, the parent constructor
| gets called as default? How do I tell the Child(int) constructor to use
| the Father(int) constructor?

[snip]

You invoke it in exactly the same way you did for 'x':

class Child : public Father
{
public:
Child(int newX) : Father( newX )
{ cout << "Child Arg Constructor" << endl;}
};

Cheers.
Chris Val
 
R

Rolf Magnus

Brian said:
Hi all,

Please note the following code... Why is it that when you call the
child with the a constructor that has arguments, the parent
constructor gets called as default?

That's how the language is defined. If you don't specify the constructor
to be called, the default constructor is used.
How do I tell the Child(int) constructor to use the Father(int)
constructor?

Though the initializer list. See below.
My results are:
Father default constructor
Child Arg Constructor

//
// Sample Code:
//
#include <iostream.h>

This is an ancient pre-standard header. Use said:
// The parent class
class Father
{
public:
Father(int newX) : x(newX)
{ cout << "Father Arg Constructor" << endl; }

Father() : x(0)
{ cout << "Father default constructor" << endl;}

private:
int x;
};

// The child class, that inherits from the parent class

The above is a nice example of a totally useless comment.
class Child : public Father
{
public:
Child(int newX)

: Father(newX)
 
A

al

Rolf Magnus said:
That's how the language is defined. If you don't specify the constructor
to be called, the default constructor is used.
My understanding about default constructor in this situation is that default
constructor is not at your service when a constructor provided. Obviously
this is not true here. Could someone straight this out(This could also be
confusing to OP.)? Thanks!
 
J

jeffc

Brian Genisio said:
Hi all,

Please note the following code... Why is it that when you call the
child with the a constructor that has arguments, the parent constructor
gets called as default? How do I tell the Child(int) constructor to use
the Father(int) constructor?

I don't understand your question. It seems like first you are complaining
that the parent constructor gets called, then you ask how to call the parent
constructor. Do you mean how to use the default parent constructor? In
that case, simply make it explicit in the initializer list.
Child(int newX) : Father(newX)
{ cout << "Child Arg Constructor" << endl;}
};

// The child class, that inherits from the parent class

Is there any other class it can possibly inherit from?
 
J

jeffc

al said:
My understanding about default constructor in this situation is that default
constructor is not at your service when a constructor provided. Obviously
this is not true here. Could someone straight this out(This could also be
confusing to OP.)?

Perhaps you're confused about what "default constructor" means. It is a bit
confusing. It does not mean the constructor that gets created for you by
default. It means the constructor with no parameters. Therefore, the
default constructor can either be created for you, or you can create it
yourself. The OP created it himself, therefore it exists and is available.
The OP created 2 constructors - the default constructor, and another
constructor. If the OP did not create any constructors at all, then the
compiler will create a default constructor. If the OP created any compilers
at all, then the constructor won't create a default constructor.
 
R

Rolf Magnus

al said:
My understanding about default constructor in this situation is that
default constructor is not at your service when a constructor
provided.

If your base class provides a user defined constructor, the compiler
won't generate a default constructor for you, that's right.
Obviously this is not true here.

It is. The compiler doesn't generate a default constructor, but there is
a user-defined one:

        Father() : x(0)
        { cout << "Father default constructor" << endl;}
 
B

Brian Genisio

jeffc said:
I don't understand your question. It seems like first you are complaining
that the parent constructor gets called, then you ask how to call the parent
constructor. Do you mean how to use the default parent constructor? In
that case, simply make it explicit in the initializer list.
Child(int newX) : Father(newX)
{ cout << "Child Arg Constructor" << endl;}
};





Is there any other class it can possibly inherit from?

Nope, you understand my question... Basically, I had a bit of a brain
fart, and was convinced that Child(int) constructor would also call
Parent(int) constructor by default. Calling the parent explicitly is
correct.

Brian
 
J

jeffc

Brian Genisio said:
Nope, you understand my question... Basically, I had a bit of a brain
fart, and was convinced that Child(int) constructor would also call
Parent(int) constructor by default.

To the C++ gurus - would it be a Really Bad Thing to have this as the
default C++ behavior, in theory? As far as I can tell, the only downside
might be a philosophical one.
 
M

Martijn Lievaart

To the C++ gurus - would it be a Really Bad Thing to have this as the
default C++ behavior, in theory? As far as I can tell, the only downside
might be a philosophical one.

It might have been an interesting question once, but nowadays this would
break millions lines of code if changed. So /today/ it /is/ a Bad Thing(tm).

HTH,
M4
 
J

jeffc

Martijn Lievaart said:
It might have been an interesting question once, but nowadays this would
break millions lines of code if changed. So /today/ it /is/ a Bad
Thing(tm).

Like I said, of course, "in theory".
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top