从point类中派生出一个line类
时间: 2024-06-08 19:09:33 浏览: 5
可以通过继承的方式从point类中派生出一个line类。line类可以包括两个point对象,表示起点和终点,以及一些方法,如计算线段长度、计算线段斜率等。下面是一个示例代码:
```
class point {
protected:
double x, y;
public:
point(double a = 0, double b = 0) : x(a), y(b) {}
};
class line : public point {
private:
point p1, p2;
public:
line(point a, point b) : p1(a), p2(b) {}
double length() {
double dx = p1.x - p2.x;
double dy = p1.y - p2.y;
return sqrt(dx*dx + dy*dy);
}
double slope() {
double dx = p1.x - p2.x;
double dy = p1.y - p2.y;
return dy / dx;
}
};
```
在这个示例中,line类从point类继承了x和y两个数据成员,同时还包含了两个point对象p1和p2。line类中的length()和slope()方法可以计算线段的长度和斜率。注意,在line类中使用了point类的构造函数来初始化x和y数据成员。
相关问题
把定义平面直角坐标系上的一个点的类Point作为基类,派生出描述一条直线的类Line
#include <iostream>
using namespace std;
class Point {
protected:
double x;
double y;
public:
Point(double x = 0, double y = 0) {
this->x = x;
this->y = y;
}
double getX() const {
return x;
}
double getY() const {
return y;
}
void setX(double x) {
this->x = x;
}
void setY(double y) {
this->y = y;
}
void setXY(double x, double y) {
this->x = x;
this->y = y;
}
void print() const {
cout << "(" << x << ", " << y << ")";
}
};
class Line : public Point {
private:
double k;
double b;
public:
Line(double k = 0, double b = 0) {
this->k = k;
this->b = b;
}
Line(Point p1, Point p2) {
if (p1.getX() == p2.getX()) {
k = INFINITY;
b = p1.getX();
} else {
k = (p2.getY() - p1.getY()) / (p2.getX() - p1.getX());
b = p1.getY() - k * p1.getX();
}
}
double getK() const {
return k;
}
double getB() const {
return b;
}
void setK(double k) {
this->k = k;
}
void setB(double b) {
this->b = b;
}
void setKB(double k, double b) {
this->k = k;
this->b = b;
}
void print() const {
cout << "y = " << k << "x + " << b;
}
};
int main() {
Point p1(1, 2);
Point p2(3, 4);
Line l1(p1, p2);
l1.print();
return 0;
}
先定义一个点类 Point ,再由点类 Point 派生出线段类 Line ,在主函数中定义 一个线段类对象,调用成员函数求出改线段的长度和其中点坐标。 (类中的数据成员不能定义为 public 。
#include <iostream>
#include <cmath>
using namespace std;
class Point {
private:
double x, y;
public:
Point(double x = 0, double y = 0) : x(x), y(y) {}
double getX() const { return x; }
double getY() const { return y; }
};
class Line : public Point {
private:
Point p1, p2;
public:
Line(double x1, double y1, double x2, double y2) : p1(x1, y1), p2(x2, y2) {}
double getLength() const { return sqrt(pow(p1.getX() - p2.getX(), 2) + pow(p1.getY() - p2.getY(), 2)); }
Point getMidPoint() const { return Point((p1.getX() + p2.getX()) / 2, (p1.getY() + p2.getY()) / 2); }
};
int main() {
Line line(0, 0, 3, 4);
cout << "Length: " << line.getLength() << endl;
Point midPoint = line.getMidPoint();
cout << "Mid Point: (" << midPoint.getX() << ", " << midPoint.getY() << ")" << endl;
return 0;
}
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)