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;
} 10 7284
"Brian Genisio" <Br**********@yahoo.com> wrote in message news:3f********@10.10.0.241...
| 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
Brian Genisio wrote: 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 <iostream> instead.
// 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)
{ cout << "Child Arg Constructor" << endl;} };
int main(void) { Child xyz(4); return 0; }
Rolf Magnus <ra******@t-online.de> wrote in message
news:bt*************@news.t-online.com... Brian Genisio wrote:
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.
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!
"Brian Genisio" <Br**********@yahoo.com> wrote in message
news:3f********@10.10.0.241... 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?
"al" <al***@168.net> wrote in message
news:1D**********************@bgtnsc05-news.ops.worldnet.att.net... Rolf Magnus <ra******@t-online.de> wrote in message news:bt*************@news.t-online.com... Brian Genisio wrote:
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. 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.
al wrote: Rolf Magnus <ra******@t-online.de> wrote in message news:bt*************@news.t-online.com... Brian Genisio wrote:
> 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. 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;}
jeffc wrote: "Brian Genisio" <Br**********@yahoo.com> wrote in message news:3f********@10.10.0.241...
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?
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
"Brian Genisio" <Br**********@yahoo.com> wrote in message
news:3F**************@yahoo.com... 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.
On Tue, 06 Jan 2004 10:51:28 -0500, jeffc wrote: "Brian Genisio" <Br**********@yahoo.com> wrote in message news:3F**************@yahoo.com... 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.
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
"Martijn Lievaart" <m@remove.this.part.rtij.nl> wrote in message
news:pa****************************@remove.this.pa rt.rtij.nl... On Tue, 06 Jan 2004 10:51:28 -0500, jeffc wrote:
"Brian Genisio" <Br**********@yahoo.com> wrote in message news:3F**************@yahoo.com... 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.
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". This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: SLE |
last post by:
Hi there,
I know constructors are not inherited from the base class, not in VB.NET nor
C# (nor Java I suppose). I never wondered,but reflecting on the reason why,
I cannot find a solid answer.
...
|
by: Justin |
last post by:
Hello, first time posting.
If I have a base class and a derived class, is there only one way to
call the base constructor?
i.e. Is this the only way I can call the base constructor...
|
by: Alexander Muylaert |
last post by:
Hi
Is their a way to keep constructors visible in inherited classes?
public class X{
public x(int A){}
}
public class Y : X{};
|
by: PIEBALD |
last post by:
OK, here's my latest workaround for the lack of inherited constructors...
It requires that the class have a parameterless (default) constructor and a
public virtual "Initialize" method (which...
|
by: Oleg Subachev |
last post by:
I am moving from Delphi to C# and hve encountered the problem:
I have the following classes and form Load event handler:
public class class1
{
public string S;
public class1( string aS )
{...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |