Home > Back-end >  C 'father' : no appropriate default constructor available
C 'father' : no appropriate default constructor available

Time:09-27

# include "iostream"
# include "string"
using namespace std;
The class father
{
Public:

Father (a string s, int a)
{name=s; Tell=a; Print (); cout<" Leave the base class constructor "& lt;
~ the father () {cout<" \ n release base class object \ n "; }
Void the print () {coutProtected:
String name; Int tell;

};



The class son: public father
{
Public:
Son (string a, int b, int c);
~ son () {cout<" Release the subclass object \ n \ n "& lt; Void print1 ();
Private:
Int weight;

};
Son: : son (string a, b int, int) c
{
Name=a;
Tell=b;
cout<" "In the subclass constructor & lt; Weight=c;

}//* *

Void son: : print1 ()//why do you want to add void here
{
Father: : print ();
cout<" Weight: "& lt; }
Int main ()
{
Son a (" Mike ", 180);
//Amy polumbo rint1 ();
cout<" After "& lt; return 0;

}

CodePudding user response:

'father' : no appropriate default constructor available

CodePudding user response:

1. The call to subclass constructor when need to call the superclass constructor
From your parent class is also the father to see father class no default is father (); The implementation of the
So in the subclass constructor needs to explicitly call father's constructor
As follows:
Son: : son (string a, b int, int) c: father (a, b)
{
Name=a;
Tell=b;
Cout & lt; <"In the subclass constructor" & lt; Weight=c;

}/* */
  • Related