1. If a class provides a constructor, the class does not provide a default constructor.
2. The derived class calls the parameterless constructor of the base class by default
#include <iostream>
#include <cstdio>
class cpoint{public
:
cpoint (int x) {
printf ( "has synax\n");
}
CPoint () {
printf ("No synax\n");
}
;
Class Point:public cpoint{public
: Point
(int x) {
}
};
Point P (4);
int main () {return
0;
}
3. The Jocky class has only a parameter constructor, and a derived class can have an error if there is no parameter constructor (and no default argument exists).
#include <iostream>
#include <cstdio>
class cpoint{public
:
cpoint (int x) {
printf ( "has synax\n");
}
/*
CPoint () {
printf ("No synax\n");
}
*/
};
Class Point:public cpoint{public
: Point
(int x) {
}
};
Point P;
int main () {return
0;
}
(see 2) because the derived class defaults to calling the parameterless constructor of the base class, does not exist, and the parent class parameter constructor does not get the parameter value, the error is caused.
The following two kinds of methods are handled
1 The parameters of the base class need to be initialized to assign values to the parameters of the base class through the parameter initialization table.
2 Change the constructor of the base class so that it has a default parameter.
Method 1 (assigning an initial value to a base class constructor by initializing the table) —-———— implemented as follows:
#include <iostream>
#include <cstdio>
class cpoint{public
:
cpoint (int x) {
printf ( "has synax\n");
}
CPoint () {
printf ("No synax\n");
}
;
Class Point:public cpoint{public
: Point
(): CPoint (1) {
}
};
Point P;
int main () {return
0;
}
Method 2 (change the constructor of the base class so that it has a default parameter.) )----implemented as follows
#include <iostream>
#include <cstdio>
class cpoint{public
:
cpoint (int x = 0) {
printf ("has synax\n");
}
/*
CPoint () {
printf ("No synax\n");
}
*/
};
Class Point:public cpoint{public
: Point
() {
}
};
Point P;
int main () {return
0;
}
The above code is compiled through VS2013