I have the following code :
#include <iostream>
using namespace std;
class X {
public:
X () {
x = 0;
cout << "1";
}
X (int x) {
this->x = x;
cout << "2";
}
X (double x) {
this->x = x;
cout << "3";
}
X (const X& x) {
this->x = x.x;
cout << "4";
}
protected:
int x;
};
class Y : public X {
public:
Y () : X(10) {
cout << "5";
}
Y (int p) : X(p) {
cout << "6";
}
Y (const X& x) : X(x) {
cout << "7";
}
Y (const X& x1, X x2) : X(x1), x(x2) {
cout << "8";
}
protected:
X x;
};
int main() {
Y y1;
cout << endl;
Y y2(10);
cout << endl;
Y y3(y1);
cout << endl;
Y y4(y2, y3);
cout << endl;
}
The output of this code is :
215
216
44
4448
- In the first two cases, I don't understand why there is the '1'. I agree that the object is built with the second X constructor and the first of Y but why the first constructor of X is called after the second one ?
- In the third case I would like to know why there is no '7' because in my opinion the program declares a Y instance so a Y constructor should be called ?
- In the last case, there is a '8' so that sound good to me but why there is three X constructor call whereas y2 and y3 are already declared ?
PS : I apologize for my English mistakes and I thank the people who correct me