Home > Back-end >  About C inherit and parents with children in the derived class constructor.
About C inherit and parents with children in the derived class constructor.

Time:11-06

Q: derived subclass why didn't shows the superclass constructor?
Examples are as follows:

CodePudding user response:

The default constructor is compiler implied a a no-parameter constructor, only when the class does not define any constructors is valid, but you customize a constructor, in the circumstance that did not set the default parameters, parents will be looking for children a no-parameter constructor (because of the impact of function overloading overloaded),
Then how to call?
Here are two kinds of schemes
Set the default parameters of the first: is it about
For example the following code can
 # include 
using namespace std;
The class father
{
Private:
Int years;
Int height;
Public:
Father (int years=0, int height=0) : years (years), height (height)
{
Cout<& lt;" Father constructor "& lt; }
Father (const father & amp; F)
{
This - & gt; Years=f.y ears;
This - & gt; Height=f.h eight;
Cout<& lt;" The copy constructor "& lt; }
~ the father ()
{
Cout<& lt;" Destructor "& lt; }
Void init (int & amp; A, int & amp; B)
{
Years=a;
Height=b;
Cout<& lt;" Father init succeed "& lt; }
Void the show ()
{
Cout<& lt;" Father years is "& lt; }
Void the get ()
{
Cout<& lt;" Difference son money "& lt; }
};
The class son: public father
{
Public:
Void the get ()
{
Cout<& lt;" The difference money "& lt; }
Void the sum ()
{
Cout<& lt;" The sum "& lt; }
};
Int main ()
{
Son s1.
//father * f1=& amp; S1.
//s1. The get ();
return 0;
}


this is the figure

The second set to subclass a structure, and in its designated the superclass constructor
And what is the difference between this change
 son (int int a, c, e, d) : father (100200) 
{

Cout<& lt;" Son Constructed \ n ";
}

 int main () 
{
Son s1 (1, 2);
return 0;
}


The results figure

Finally long-winded, I compile environment for GCC

CodePudding user response:

Well, above is, not what, forgive my hand shaking, what's wrong or questions welcome to discuss the

CodePudding user response:

refer to 1st floor @ contradict response:
the default constructor is to provide a no compiler implicit constructor, only when the class does not define any constructors is valid, but you customize a constructor, in the circumstance that does not have to set the default parameters, parents will be looking for children a no-parameter constructor (because of the impact of function overloading overloaded),
Then how to call?
Here are two kinds of schemes
Set the default parameters of the first: is it about
For example the following code can
 # include 
using namespace std;
The class father
{
Private:
Int years;
Int height;
Public:
Father (int years=0, int height=0) : years (years), height (height)
{
Cout<& lt;" Father constructor "& lt; }
Father (const father & amp; F)
{
This - & gt; Years=f.y ears;
This - & gt; Height=f.h eight;
Cout<& lt;" The copy constructor "& lt; }
~ the father ()
{
Cout<& lt;" Destructor "& lt; }
Void init (int & amp; A, int & amp; B)
{
Years=a;
Height=b;
Cout<& lt;" Father init succeed "& lt; }
Void the show ()
{
Cout<& lt;" Father years is "& lt; }
Void the get ()
{
Cout<& lt;" Difference son money "& lt; }
};
The class son: public father
{
Public:
Void the get ()
{
Cout<& lt;" The difference money "& lt; }
Void the sum ()
{
Cout<& lt;" The sum "& lt; }
};
Int main ()
{
Son s1.
//father * f1=& amp; S1.
//s1. The get ();
return 0;
}


this is the figure

The second set to subclass a structure, and in its designated the superclass constructor
And what is the difference between this change
 son (int int a, c, e, d) : father (100200) 
{

Cout<& lt;" Son Constructed \ n ";
}

 int main () 
{
Son s1 (1, 2);
return 0;
}


The results figure

Finally long-winded, I compile environment for GCC

Thank you. Thank you for your enthusiastic help

CodePudding user response:

refer to the second floor @ contradict response:
well, is it, what is not, forgive my hand shaking, what's wrong or questions welcome to discuss the

I added an int behind the object weight=0, the program is ok,
In addition, I still don't understand where I went wrong
Can I ask you for me

CodePudding user response:

reference 4 floor qq_45805535 response:
Quote: refer to the second floor of the @ contradict reply:
well, is it, what is not, forgive my hand shaking, what's wrong or questions welcome to discuss the

I added an int behind the object weight=0, the program is ok,
In addition, I still don't understand where I went wrong
Could I ask you, please


nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related