设计并测试一个名为Rectangle的类表示矩形,其属性为矩形的左下角和右上角两个坐标点
该类能够完成边长和面积的计算,并且坐标点也使用一个类来表示首先要谢谢wanfustudio的回答和victor8675309的提醒,谢谢你们!classPoint{pub...
该类能够完成边长和面积的计算,并且坐标点也使用一个类来表示
首先要谢谢 wanfustudio的回答和victor8675309的提醒,谢谢你们!
class Point
{
public:
double GetX();
double GetY();
private:
double x,y;
};
double Point::GetX ()
{
return x;
}
double Point::GetY()
{
return y;
}
class Rectangle
{
public:
Rectangle(Point ld,Point ru);
~Rectangle() {}
double GetArea(Point ld,Point ru);
double GetLength(Point ld,Point ru);
private:
double x1,x2,y1,y2;
};
double Rectangle::GetLength (Point LD,Point RU)
{
return abs(2*(LD.GetX()-RU.GetX()+LD.GetY()-RU.GetY()));
}
double Rectangle::GetArea (Point LD,Point RU)
{
return abs((LD.GetX()-RU.GetX())*(LD.GetY()-RU.GetY()));
}
int main ()
{
Rectangle myRect(Point A,Point B);
cout<<myRect(Point A,Point B).GetArea()<<endl;
cout<<myRect(Point A,Point B).GetLength()<<endl;
}头文件我省略,这是我自己写的不完全程序,你们能帮我看下帮我完善不?我的困惑是怎么把点ld和ru用(x1,y1)(x2,y2)来表示,这样我就可以实现我自己随便输入两个点的坐标来计算矩形的面积和周长 展开
首先要谢谢 wanfustudio的回答和victor8675309的提醒,谢谢你们!
class Point
{
public:
double GetX();
double GetY();
private:
double x,y;
};
double Point::GetX ()
{
return x;
}
double Point::GetY()
{
return y;
}
class Rectangle
{
public:
Rectangle(Point ld,Point ru);
~Rectangle() {}
double GetArea(Point ld,Point ru);
double GetLength(Point ld,Point ru);
private:
double x1,x2,y1,y2;
};
double Rectangle::GetLength (Point LD,Point RU)
{
return abs(2*(LD.GetX()-RU.GetX()+LD.GetY()-RU.GetY()));
}
double Rectangle::GetArea (Point LD,Point RU)
{
return abs((LD.GetX()-RU.GetX())*(LD.GetY()-RU.GetY()));
}
int main ()
{
Rectangle myRect(Point A,Point B);
cout<<myRect(Point A,Point B).GetArea()<<endl;
cout<<myRect(Point A,Point B).GetLength()<<endl;
}头文件我省略,这是我自己写的不完全程序,你们能帮我看下帮我完善不?我的困惑是怎么把点ld和ru用(x1,y1)(x2,y2)来表示,这样我就可以实现我自己随便输入两个点的坐标来计算矩形的面积和周长 展开
2个回答
展开全部
#include<iostream>
#include<cmath>
using namespace std;
class point
{
public:
point(){x= y =0.0;}
point(point &p):x(p.x),y(p.y)
{
}
point(double x, double y)
{
this->x =x;
this->y = y;
}
double x;
double y;
};
class Rectangle
{
public:
Rectangle(){}
Rectangle(Rectangle &r):p1(r.p1),p2(r.p2)
{
}
Rectangle(double x1,double y1,double x2,double y2):p1(x1,y1),p2(x2,y2)
{
}
double GetLength()
{
return fabs(p2.x-p1.x);
}
double GetArea()
{
return fabs((p2.x-p1.x) * (p2.y-p1.y));
}
private:
point p1,p2;
};
void main()
{
Rectangle r(1,2,3,4);
cout <<r.GetLength()<<endl;
cout <<r.GetArea()<<endl;
}
#include<cmath>
using namespace std;
class point
{
public:
point(){x= y =0.0;}
point(point &p):x(p.x),y(p.y)
{
}
point(double x, double y)
{
this->x =x;
this->y = y;
}
double x;
double y;
};
class Rectangle
{
public:
Rectangle(){}
Rectangle(Rectangle &r):p1(r.p1),p2(r.p2)
{
}
Rectangle(double x1,double y1,double x2,double y2):p1(x1,y1),p2(x2,y2)
{
}
double GetLength()
{
return fabs(p2.x-p1.x);
}
double GetArea()
{
return fabs((p2.x-p1.x) * (p2.y-p1.y));
}
private:
point p1,p2;
};
void main()
{
Rectangle r(1,2,3,4);
cout <<r.GetLength()<<endl;
cout <<r.GetArea()<<endl;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询