1.定义一个Point类表示平面上的一个点,再定义一个Rectangle类
表示平面上的矩形,用Point类的对象作为Rec tangle类的数据成员
描述平面上的矩形的顶点坐标。要求类Point中有相应的成员函数可
以读取点的坐标值,类Rectangle中含有一个成员函数,用以计算并
输出矩形的面积及项点坐标。在主函数中对类Rectangle进行测试。
class Point{
};
class Rectangle
{
public:
private:
Point p;
}
这么做应该是可以的
class Point
{
public:
int x;
int y;
void getPoint()
{
cout<<"x = "<<x<<"y = "<<y<<endl;
}
};
class Rectangle
{
public:
Rectangle(int x,int y,int length,int width)
{
point.x=x;
point.y=y;
m_length=length;
m_width=width;
}
void getPosition()
{
point.getPoint();
}
void getArea()
{
cout<<"area = "<<m_length*m_width;
}
int m_length;
int m_width;
Point point;
};
int main()
{
Rectangle rec(2,2,10,20);
rec.getPosition();
rec.getArea();
}