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的值和该切线方程.
我想写一篇关于奥巴马的演讲的文章,写哪一篇好呢?为什么好
最新试题
热门考点
- ★散文跟记叙文有什么区别?
- 奶奶养了公鸡12只,母鸡20只,母鸡比公鸡多百分之几?公鸡比母鸡少百分之几?
- 化学元素的命名是发现者定的吗,比如居里夫人的”镭”
- 40-5t^2=0,
- 色光的混合与颜料的混合有何不同
- 填空
- 第一次工业革命期间,人类历史上的交通运输业首次出现了革命性的变革,大大促进了人类社会的发展;19世纪末20世纪初,交通运输业继承前一时期的成就,再次出现了革命性的变革.
- 仙人掌的品质是什么
- 近代苏联社会主义改革和中国社义改革的不同及其原因 能分点么
- 植树节甲班植树的株数比乙班多20%,乙班植树的株数比甲班的一半多10株,若乙班植树x株