一点资料
Parent Class: Vehicle
Child Class: Car & Motorcycle
我有struct
一个
struct Point
{
int x,y;
};
我在&得到了一个setPoint
函数,它执行以下操作Car
Motorcycle
因为Car
有 4 个轮子和Motorcycle
2 个轮子。
Car
会有这个功能
class Car : public Vehicle
{
private:
Point wheelPoint[4]; // if motorcycle then its wheelPoint[2]
public:
Point getPoint();
void setPoint();
}
void Car::setPoint()
{
int xData,yData;
for (int i=0;i<4;i++)
{
cout << "Please enter X :" ;
cin >> xData;
cout << "Please enter Y :" ;
cin >> yData;
}//end for loop
}//end setPoint
所以我也有一个getPoint
功能..
Point Car::getPoint()
{
return wheelPoint;
}
问题出在我的 main.cpp ,我做了以下
int main()
{
VehicleTwoD *vehicletwod[100];
//assuming just car but there motorcycle too
Car *mCar = new Car();
Point myPoint;
vechicletwod[0] = mCar;
//here i did the setpoint, set other variable
//then when i try retrieve
myPoint = Car->getPoint();
//no error till here.
cout << sizeof(myPoint);
}
无论是摩托车还是汽车,结果始终保持在 4,摩托车不会是 2,汽车不会是 4。我不知道怎么了
假设我也为摩托车设定了设定值。两者都返回相同的结果,是我在主类中包含点 myPoint 不适合包含 wheelPoint[array]