1,设计一个点类point,再设计一个矩形类,矩形类使用point类的两个坐标点作为矩形的对角顶点,并可以输出4
1个回答
展开全部
段并输出其属性(斜率、长度、两个端点坐标),
再设计一个矩形类,矩形类使用线段类作为矩形的一条边,并可以输出四个坐标点和面积。最后设计
一个主函数用来测试以上两个类的功能。*/
#include <iostream>
#include <math.h>
using namespace std;
class POINT
{
public:
int x;
int y;
};
class line
{
public:
float getLen();
float getSlope();
void setline(POINT a, POINT b);
void display();
line()
line(int a, int b, int c, int d)
~line(){};
public:
POINT one, two;
};
class figure
{
public:
float getArea();
void disPoit();
public:
line top, left, buttom, right;
};
//设置线段
void line::setline(POINT a, POINT b)
{
one = a; two = b;
}
//求线段的长度
float line::getLen()
{
float len;
len = sqrt((two.y-one.y)*(two.y-one.y) + (two.x-one.x)*(two.x-one.x));
return len;
}
//求线段的斜率
float line::getSlope()
{
float slope;
slope = (two.y-one.y) / (two.x-one.x);
return slope;
}
//输出线段的斜率、长度、
void line::display()
{
cout << "斜率:" << getSlope() << endl;
cout << "长度:" << getLen() << endl;
cout << "两个端点坐标分别为:" << "(" << one.x << "," << one.y <<")"
<< "(" << two.x << "," << two.y <<")" << endl;
}
//求矩形的面积
float figure::getArea()
{
float area;
area = top.getLen() * left.getLen();
return area;
}
//输出四个端点
void figure::disPoit()
{
cout << "四个端点坐标分别为:"
<< "(" << top.one.x << "," << top.one.y <<")"
<< "(" << left.one.x << "," << left.one.y <<")"
<< "(" << buttom.one.x << "," << buttom.one.y <<")"
<< "(" << right.one.x << "," << right.one.y <<")"
<< endl;
}
int main()
{
cout << "*******线段测试*******" <<endl;
line l(0, 0, 1, 2);
l.display();
cout << endl;
cout << "*******矩形测试*******" << endl;
figure f;
POINT a, b, c, d;
a.x = 1;
a.y = 1;
b.x = 3;
b.y = 1;
c.x = 3;
c.y = 3;
d.x = 1;
d.y = 3;
f.top.setline(a, b);
f.left.setline(b, c);
f.buttom.setline(c, d);
f.right.setline(d, a);
f.disPoit();
cout << "矩形的面积:" << f.getArea() << endl;
return 0;
}
再设计一个矩形类,矩形类使用线段类作为矩形的一条边,并可以输出四个坐标点和面积。最后设计
一个主函数用来测试以上两个类的功能。*/
#include <iostream>
#include <math.h>
using namespace std;
class POINT
{
public:
int x;
int y;
};
class line
{
public:
float getLen();
float getSlope();
void setline(POINT a, POINT b);
void display();
line()
line(int a, int b, int c, int d)
~line(){};
public:
POINT one, two;
};
class figure
{
public:
float getArea();
void disPoit();
public:
line top, left, buttom, right;
};
//设置线段
void line::setline(POINT a, POINT b)
{
one = a; two = b;
}
//求线段的长度
float line::getLen()
{
float len;
len = sqrt((two.y-one.y)*(two.y-one.y) + (two.x-one.x)*(two.x-one.x));
return len;
}
//求线段的斜率
float line::getSlope()
{
float slope;
slope = (two.y-one.y) / (two.x-one.x);
return slope;
}
//输出线段的斜率、长度、
void line::display()
{
cout << "斜率:" << getSlope() << endl;
cout << "长度:" << getLen() << endl;
cout << "两个端点坐标分别为:" << "(" << one.x << "," << one.y <<")"
<< "(" << two.x << "," << two.y <<")" << endl;
}
//求矩形的面积
float figure::getArea()
{
float area;
area = top.getLen() * left.getLen();
return area;
}
//输出四个端点
void figure::disPoit()
{
cout << "四个端点坐标分别为:"
<< "(" << top.one.x << "," << top.one.y <<")"
<< "(" << left.one.x << "," << left.one.y <<")"
<< "(" << buttom.one.x << "," << buttom.one.y <<")"
<< "(" << right.one.x << "," << right.one.y <<")"
<< endl;
}
int main()
{
cout << "*******线段测试*******" <<endl;
line l(0, 0, 1, 2);
l.display();
cout << endl;
cout << "*******矩形测试*******" << endl;
figure f;
POINT a, b, c, d;
a.x = 1;
a.y = 1;
b.x = 3;
b.y = 1;
c.x = 3;
c.y = 3;
d.x = 1;
d.y = 3;
f.top.setline(a, b);
f.left.setline(b, c);
f.buttom.setline(c, d);
f.right.setline(d, a);
f.disPoit();
cout << "矩形的面积:" << f.getArea() << endl;
return 0;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询