我有一個朋友Point類的Rectangle類。我使用笛卡爾座標,所以我在矩形類中有四個點。點在點類中定義。如果數據初始化如下:如何訪問朋友類中的變量
int main()
{
Point w(1.0, 1.0);
Point x(5.0, 1.0);
Point y(5.0, 3.0);
Point z(1.0, 3.0);
Rectangle r1(x, y, z, w);
}
如何從r1打印所有點?此外,我需要使用4個點作爲矩形,並使用朋友類。
頭:
class Point
{
public:
Point();
Point(int, int);
void printPoint(Rectangle& pt, Point a);
private:
int x;
int y;
};
class Rectangle
{
public:
Rectangle(Point, Point, Point, Point);
friend void Point::printPoint(Rectangle& pt, Point a);
~Rectangle();
private:
Point a;
Point b;
Point c;
Point d;
};
你能具體談談什麼是錯的? –
兩個點定義了一個(直線)矩形,而不是四個... – Potatoswatter
只是爲了使事情變得有趣:a)您不需要四個點來定義矩形,只需要兩個(左上角和右下角)b)Point :: PrintPoint(並不需要任何參數 - 它被稱爲獲得一個點打印本身,c)不需要朋友的東西 –