C++实验题:声明并实现一个Line类,表示线段。Line类包括:

2025-03-21 22:17:05
推荐回答(1个)
回答(1):

#include
using namespace std;

const double ZERO = 0.000001;

class Line
{
public:
Line() {x1 = 0; y1 = 0; x2 = 0; y2 = 0;}
Line(int _x1, int _y1, int _x2, int _y2): x1(_x1), y1(_y1), x2(_x2), y2(_y2){}
~Line() {}
void setStart(int x, int y);
void setEnd(int x, int y);
void getStart(int &x, int &y);
void getEnd(int &x, int &y);
double slope();

private:
int x1;
int y1;
int x2;
int y2;
};

void Line::setStart(int x, int y)
{
x1 = x;
y1 = y;
}

void Line::setEnd(int x, int y)
{
x2 = x;
y2 = y;
}

void Line::getStart(int &x, int &y)
{
x = x1;
y = y1;
}

void Line::getEnd(int &x, int &y)
{
x = x2;
y = y2;
}

double Line::slope()
{
if (-ZERO < x2 - x1 && ZERO > x2 - x1)
{
cout<<"无穷大"< cin.get();
exit(-1);
}

return (y2 - y1) / (x2 - x1);
}

int main()
{
int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
cout<<"请输入点坐标:";
cin>>x1>>y1>>x2>>y2;
cin.get();

Line line;
line.setStart(x1, y1);
line.setEnd(x2, y2);
cout<<"slope = "<
cin.get();
return 0;
}