题目图片:
文字形式:
定义平面二维点类Point,(有x,y坐标,构造函数、复制构造函数,输出函数)。线段类Line是由两个Point点组成的组合类(数据成员: 两个端点,线段长度; 函数成员:构造函数、复制构造函数、计算线段长度函数) //主函数 int main() {
int x1,y1,x2,y2; cin>>x1>>y1;
cin>>x2>>y2; Point myp1(x1,y1),myp2(x2,y2); //建立Point类的对象 Line L1(myp1,myp2); //建立Line类的对象 cout<<“Line start Point is:”; L1.GetPstart().print(); cout<<endl; cout<<“Line end Point is:”; L1.GetPend().print(); cout<<endl; cout<<“Length of Line is:”<<L1.GetLen()<<endl;
}
输入格式:
输入两行,第一行为线段的起点坐标(中间已空格隔开),第二行为线段的终点坐标 (中间已空格隔开)
输出格式:
输入各个函数被调用过程主程序,需要填写。
输入样例:
在这里给出一组输入。例如:
0 0
3 4
输出样例:
在这里给出相应的输出。例如:
create Point:P(0,0)
create Point:P(3,4)
Create a new Line:
Line start Point is:P(0,0)
Line end Point is:P(3,4)
Length of Line is:5
本人代码:
#include<iostream>
#include<cmath>
using namespace std;
class Point
{
private:
int x,y;
public:
int getx()
{
return x;
}
int gety()
{
return y;
}
void print()
{
cout<<"P("<<Point::x<<","<<Point::y<<")"<<endl;
}
Point(int a,int b)
{
Point::x=a;
Point::y=b;
cout<<"create Point:";
Point::print();
}
Point(const Point& a)
{
x=a.x;
y=a.y;
}
};
class Line {
private:
Point a,b;
double c;
public:
Line(Point c,Point d):a(c),b(d)
{
cout<<"Create a new Line:"<<endl;
}
void GetPstart()
{
a.print();
}
void GetPend() {
b.print();
}
int GetLen()
{
auto x=static_cast<double>(a.getx()-b.getx());
auto y=static_cast<double>(a.gety()-b.gety());
c=sqrt(x*x+y*y);
return c;
}
};
int main() {
int x1,y1,x2,y2;
cin>>x1>>y1;
cin>>x2>>y2;
Point myp1(x1,y1),myp2(x2,y2); //建立Point类的对象
Line L1(myp1,myp2); //建立Line类的对象
cout<<"Line start Point is:";
L1.GetPstart();
cout<<"Line end Point is:";
L1.GetPend();
cout<<"Length of Line is:"<<L1.GetLen()<<endl;
}