-->
当前位置:首页 > 题库 > 正文内容

Luz4年前 (2021-03-08)题库1812
6-6 Point类的声明和实现 (10 分)

定义一个Point类,代表平面上的一个点,其横坐标和纵坐标分别用x和y表示,设计Point类的成员函数,实现并测试这个类。 主函数中输入两个点的坐标,计算并输出了两点间的距离。请根据主函数实现Point类。

裁判测试程序样例:

  1. #include <iostream>#include <iomanip>#include <cmath>using namespace std;//你的代码将被嵌在这里int main(){
  2.     Point p1, p2;    double x1, y1, x2, y2;    cin >> x1 >> y1 >> x2 >> y2;
  3.     p1.setX(x1);
  4.     p1.setY(y1);
  5.     p2.setX(x2);
  6.     p2.setY(y2);    double x = p1.getX() - p2.getX();    double y = p1.getY() - p2.getY();    double len = sqrt(* x + y * y);    cout << fixed << setprecision(2) << len << endl;    return 0;
  7. }

输入样例:

  1. 0 0 3 3

输出样例:

  1. 4.24
作者
李廷元
单位
民用航空飞行学院
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
  1. class Point{
  2.     double x,y;
  3.     public:
  4.     void setX(double x1){
  5.         x=x1;
  6.     }
  7.     void setY(double y1){
  8.         y=y1;
  9.     }
  10.     double getX(){
  11.         return x;
  12.     }
  13.     double getY(){
  14.         return y;
  15.     }   
  16. };