1,设计一个点类point,再设计一个矩形类,矩形类使用point类的两个坐标点作为矩形的对角顶点,并可以输出4

1,设计一个点类point,再设计一个矩形类,矩形类使用point类的两个坐标点作为矩形的对角顶点,并可以输出4

题目
1,设计一个点类point,再设计一个矩形类,矩形类使用point类的两个坐标点作为矩形的对角顶点,并可以输出4
答案
段并输出其属性(斜率、长度、两个端点坐标),
再设计一个矩形类,矩形类使用线段类作为矩形的一条边,并可以输出四个坐标点和面积.最后设计
一个主函数用来测试以上两个类的功能.*/
#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;
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;
}
举一反三
已知函数f(x)=x,g(x)=alnx,a∈R.若曲线y=f(x)与曲线y=g(x)相交,且在交点处有相同的切线,求a的值和该切线方程.
我想写一篇关于奥巴马的演讲的文章,写哪一篇好呢?为什么好
奥巴马演讲不用看稿子.为什么中国领导演讲要看?
想找英语初三上学期的首字母填空练习……
英语翻译
最新试题
热门考点

超级试练试题库

© 2017-2019 超级试练试题库,All Rights Reserved.