c++友元类问题(帮忙修改下)

来源:百度知道 编辑:UC知道 时间:2024/06/24 14:11:28
//AB两点是矩阵对点 求面积。
#include<iostream.h>
#include<math.h>
class Point
{
public:
Point(float x1,float y1)
{
x=x1;
y=y1;
}
friend class Rectangle;
private:
float x,y;
};
class Rectangle
{
public:
void Get_s()
{
cout<<"面积:"<<fabs((A.x-B.x)*(A.y-B.y))<<endl;
}
private:
Point A(1,2);
Point B(3,4);
};
void main()
{
Rectangle s;
s.Get_s();
}

#include<iostream.h>
#include<math.h>
class Point
{
public:
Point(float x1,float y1)
{
x=x1;
y=y1;
}
friend class Rectangle;
private:
float x,y;
};
class Rectangle
{
public:
void Get_s()
{
Point A(1,2); //放到里面来,私有字段不能为常量。
Point B(3,4); //放到里面来,私有字段不能为常量。
cout<<"面积:"<<fabs((A.x-B.x)*(A.y-B.y))<<endl;
}

};
void main()
{
Rectangle s;
s.Get_s();
}

开始要声明一下类Rectangle,不然会出现它末定义的.