永发信息网

大家帮忙看看为什么

答案:1  悬赏:0  手机版
解决时间 2021-04-04 22:24
  • 提问者网友:原来太熟悉了会陌生
  • 2021-04-04 14:35
大家帮忙看看为什么
最佳答案
  • 五星知识达人网友:行雁书
  • 2021-04-04 15:16
帮你改了下,
Rectangle 中width和length是私有的变量,所在在输出时不能直接访问,要用到函数,还有Rectangle你的那个析构函数中的upleft的Location结构体用错了。

#include //这是输入输出流所要包含的头文件
#include //这是要进行数学计算所要包含的头文件

class Location
{
public:
int X,Y; //横坐标和纵坐标:

Location( int a , int b);//构造函数
double distance (Location &);
//求两点距离的函数
~Location();//析构函数
};
//在构造函数中对类的成员进行初始化
Location::Location(int a ,int b)
{
X=a;
Y=b; }
//求两点的距离
//这里我们用的公式是,两点之间的
//距离=
//(x1,y1)和(x2,y2)为两点的坐标

double Location::distance(Location & loc1)
{
double length;
length=sqrt((loc1.X-Location::X)*(loc1.X-Location::X)+
(loc1.Y-Location::Y)*(loc1.Y-Location::Y));
//注意这里的作用域符号::表示X和Y是属//于Location类的
return length; }

Location::~Location()
{ }
class Rectangle
{
private:
friend Location;
//定义矩形长和宽
int length, width;
//定义矩形左上点
Location *upleft;
public:

//定义矩形的构造函数和析构函数
Rectangle(int a,int b,int len,int wid );
~Rectangle();
int GetupleftX();
int GetupleftY();
int Getlength();
int Getwidth();
};
Rectangle::Rectangle(int a,int b,int len,int wid )//:upleft(a,b)
{
//初始化Rectangle类的length和width
length=len;
width=wid;
//这里通过调用Location类的构造函数
//来初始化Rectangle类的对象upleft
upleft=new Location(a,b);
}
Rectangle::~Rectangle()
{
delete upleft;
}
//在构造函数中用new开辟了内存空间upleft,
//所以在析构函数中用delete删除它
int Rectangle::GetupleftX()
{
return upleft->X;
}
int Rectangle::GetupleftY()
{
return upleft->Y;
}
int Rectangle::Getlength()
{
return length;
}
int Rectangle::Getwidth()
{
return width;
}
void main()
{
Rectangle A(-10,-20,60,40);

//定义了一个长为60,宽为40,左上点为(-10,-20)
//的矩形,下面将其输出
cout<<"矩形左上点为:"<<"("< < //A.upleft是Location的成员对象,所以A.upleft.x
//表示输出的是左上点的x坐标,A.upleft.y表示输//出的是左上点的y坐标,即-10和-20
cout<<"矩形的长为:"< //输出为60
cout<<"矩形的宽为:"< //输出为40

}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯