派生类 构造函数 初始化列表问题 求助!
include <iostream>
using namespace std;
class Point
{
public:
Point(float xx=0,float yy=0)
{
X=xx;
Y=yy;
}
void Move(float xOff,float yOff)
{
X+=xOff;
Y+=yOff;
}
float GetX()
{
return X;
}
float GetY()
{
return Y;
}
private:
float X;
float Y;
};
class Rectangle:public Point
{
public:
Rectangle(float x,float y,float w,float h):Point(x,y)
{
W=w;
H=h;
}
float GetH()
{
return H;
}
float GetW()
{
return W;
}
private:
float H,W;
};
int main()
{
Rectangle rect(2,3,20,10);
rect.Move(2,3);
cout<<rect.GetX()<<endl;
cout<<rect.GetY()<<endl;
cout<<rect.GetW()<<endl;
cout<<rect.GetH()<<endl;
system("pause");
return 0;
}
输出结果为 4,6,20,10
但是当把rectangle类的构造函数改为
Rectangle(float x,float y,float w,float h):
{
Point(x,y);
W=w;
H=h;
}
把Point类的构造函数放在函数体内,而不是初始化列表
结果就是2,3,20,10;
能不能解释一下是为什么