初学者寒假自学c++

在平面上作两个点,连一直线,求直线的长度和直线中点的坐标。
具体要求:
1、基类为Dot,有两个公有数据成员,即平面上的坐标(x,y),同时有构造函数及输出数据函数。
2、派生类为Line,有两个基类Dot对象,分别存放两点的坐标,同时,从基类继承了一个Dot数据,存放直线中点的坐标。

运行示例:

img

代码:

#include <iostream>
#include <math.h>
using namespace std;
class Dot
{
public:
    double mx, my;
    Dot(double x =0, double y=0) :mx(x), my(y) {}
    void display()
    {
        cout << "(" << mx << "," << my << ")";
    }
};

class Line :public Dot
{
public:
    Dot md1, md2;
    Line(double x1, double y1, double x2, double y2)
    {
        md1.mx = x1;
        md1.my = y1;
        md2.mx = x2;
        md2.my = y2;
        //计算中点
        mx = (x1 + x2) / 2;
        my = (y1 + y2) / 2;
    }
    void show()
    {
        cout << "起点";
        md1.display();
        cout << ",终点";
        md2.display();
        cout << ",中点";
        display();
        cout << endl;
    }
    double length()
    {
        double len = (md1.my - md2.my) * (md1.my - md2.my) + (md1.mx - md2.mx) * (md1.mx - md2.mx);
        return sqrt(len);
    }
};

int main()
{
    Line ln(0,0,5,5);
    ln.show();
    cout << "长度:" << ln.length() << endl;
    return 0;
}


double distance ( int x1 , int y1 , int x2 , int y2 ) {

int x = , y = ;

double res = 0.0;

x = abs( x2 - x1 ); // 直角三角形的 勾

y = abs( y2 - y1 ); // 直角三角形的 股

res = sqrt( x * x + y * y ) ; // 勾股定理 求 斜线

return res;

}

又有问题了么,一个坐标点类,一个直线类,稍等

#include <iostream>
using namespace std;
class Dot
{
    protected:
        int x,y;
    public:
        Dot() {}
        Dot(int x,int y):x(x),y(y) {}
        void SetData(int x,int y) {this->x = x;this->y=y;}
        void SetData(Dot pt1,Dot pt2) {x = (pt1.x+pt2.x)/2;y=(pt1.y+pt2.y)/2;}
};
 
class Line : public Dot
{
    protected:
        Dot pt1,pt2;
    public:
        Line() {}
        Line(Dot d1,Dot d2) : pt1(d1),pt2(d2) {SetData(pt1,pt2);}
        Line(int x1,int y1,int x2,int y2)
        {
            pt1.SetData(x1,y1);
            pt2.SetData(x2,y2);
            SetData((x1+x2)/2,(y1+y2)/2);
        }
        void showCenter() {cout<<"x="<<x<<",y="<<y<<endl;}
};
 
int main()
{
    Line line1(10,20,30,50);
    line1.showCenter();
    Dot pt1(10,15),pt2(18,25);
    Line line2(pt1,pt2);
    line2.showCenter();
    return 0;
}

下面是用 C++ 实现上述类的代码:


#include <iostream>
#include <cmath>

using namespace std;

// 定义基类Dot
class Dot {
public:
    // 数据成员:平面上的坐标(x,y)
    int x;
    int y;
    // 构造函数
    Dot(int x=0, int y=0) : x(x), y(y) {}
    // 输出数据函数
    void print() {
        cout << "(" << x << "," << y << ")" << endl;
    }
};

// 定义派生类Line
class Line : public Dot {
public:
    // 数据成员:两个基类Dot对象,分别存放两点的坐标
    Dot p1;
    Dot p2;
    // 构造函数
    Line(Dot p1, Dot p2) : p1(p1), p2(p2) {}
    // 计算直线的长度
    double length() {
        return sqrt(pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2));
    }
    // 计算直线中点的坐标
    void midpoint() {
        x = (p1.x + p2.x) / 2;
        y = (p1.y + p2.y) / 2;
    }
};

int main() {
    // 创建两个Dot对象
    Dot p1(1, 2);
    Dot p2(3, 4);
    // 创建Line对象
    Line line(p1, p2);
    // 输出直线的长度
    cout << "直线的长度为:" << line.length() << endl;
    // 计算直线中点的坐标
    line.midpoint();
    // 输出直线中点的坐标
    cout << "直线中点的坐标为:";
    line.print();
    return 0;
}


class Dot {
    double x;
    double y;
    Dot(){
        double x,y;
        scanf("%lf %lf",&x,&y);
        this.x=x;
        this.y=y;
    }
public void out(){
        //输出数据
printf("%lf %lf",x,y);
}
    public double getX() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        this.y = y;
    }
}
class Line:public Dot{
    Dot d1;
    Dot d2;
    Line(){
        d1=new Dot();
        d2=new Dot();
        }
        public double getLength(){
        return sqrt((d1.getX()-d2.getX())*(d1.getX()-d2.getX())+(d1.getY()-d2.getY())*(d1.getY()-d2.getY()));
        }
        public void mid(){
        //设置中点坐标
        setX((d1.getX()+d2.getX())/2);
        setX((d1.getY()+d2.getY())/2);
        }

};
int main(){
    Line l=new Line();
    
    l.mid();
    printf("%lf %lf %lf",l.getLength(),l.getX(),l.getY());
    
        }

麻烦采纳下

你好!这是一个使用 C++ 实现的可以计算平面上两点间直线长度和中点坐标的程序的示例:

#include <iostream>
#include <cmath>  // 使用数学函数 sqrt 需要包含 cmath 头文件

// 定义基类 Dot
class Dot {
  public:
    // 构造函数
    Dot(double x = 0, double y = 0) : x_(x), y_(y) {}

    // 输出函数
    void Print() {
      std::cout << "(" << x_ << ", " << y_ << ")" << std::endl;
    }

  private:
    double x_, y_;  // 平面上的坐标 (x, y)
};

// 定义派生类 Line
class Line : public Dot {
  public:
    // 构造函数
    Line(const Dot& p1, const Dot& p2) : p1_(p1), p2_(p2) {
      // 计算直线的长度
      length_ = std::sqrt((p1.x_ - p2.x_) * (p1.x_ - p2.x_) +
                          (p1.y_ - p2.y_) * (p1.y_ - p2.y_));
      // 计算直线的中点坐标
      x_ = (p1.x_ + p2.x_) / 2;
      y_ = (p1.y_ + p2.y_) / 2;
    }

    // 输出函数
    void Print() {
      std::cout << "Length: " << length_ << std::endl;
      std::cout << "Center: ";
      Dot::Print();  // 调用基类的输出函数
    }

  private:
    Dot p1_, p2_;  // 两个点的坐标
    double length_;  // 直线的长度
};

int main() {
  Dot p1(1, 2), p2(4, 5);  // 定义两个点的坐标
  Line line(p1, p2);  // 定义直线
  line.Print();  // 输出直线的长度和中点坐标

  return 0;
}