求C++一个问题的第二种符合要求的解法

img

img


解答除了这样写,还有其他的写法吗,outx的功能是什么,函数名可以改成什么

outx作用为给xy赋值,但不合题意,代码如下,望采纳

#include<iostream>

using namespace std;

class Point
{
private:
    float x;
    float y;

public:
    float getX()
    {
        return x;
    }
    float getY()
    {
        return y;
    }
    void showPoint()
    {
        cout << "x:" << x << endl << "y:" << y << endl;
    }
    Point()
    {
        cout << "请输入x,y的值" << endl;
        cin >> x >> y;
    }
};

class Line
{
private:
    Point p1;
    Point p2;
    float k;
public:
    void output()
    {
        cout << "p1的坐标为:(" << p1.getX() << "," << p1.getY() << ")" << endl;
        cout << "p2的坐标为:(" << p2.getX() << "," << p2.getY() << ")" << endl;
        k = (p1.getY() - p2.getY()) / (p1.getX() - p2.getX());
        cout << "斜率为:" << k << endl;
    }
};

int main()
{
    Line l;
    l.output();
    system("pause");
    return 0;
}

效果:

img

setx