定义一个点类Point,包含两个保护成员(x,y)以及3个公有成员函数,要求构造函数带两个形参,2个普通成员函数(设置坐标以及显示坐标);
时间: 2023-06-15 08:06:57 浏览: 204
以下是一个简单的点类Point的实现:
```
class Point {
protected:
int x;
int y;
public:
Point(int xCoord, int yCoord) : x(xCoord), y(yCoord) {}
void setPoint(int xCoord, int yCoord) {
x = xCoord;
y = yCoord;
}
void showPoint() {
cout << "(" << x << ", " << y << ")" << endl;
}
};
```
上述代码中,Point类包含两个保护成员变量x和y,以及三个公有成员函数:一个构造函数,一个设置坐标的函数setPoint()和一个显示坐标的函数showPoint()。构造函数接受两个形参,用于初始化x和y的值。setPoint()函数用于设置x和y的值,showPoint()函数用于显示点的坐标。
相关问题
2. 定义一个点类Point, 包含两个保护成员(x, y)以及3个公有成员函数,要求构造函数带两个形参,2个普通成员函数(设置坐标以及显示坐标); 再定义一个圆类(Circle),公有继承Point类,新增私有数据成员半径以及4个成员函数,要求构造函数实现数据的初始化,2个普通成员的数分别能设置和获取圆的信息,1个普通成员函数计算圆的面积; 然后在主函数中定义2个Circle类对象并调用相应的函数进行测试。
```C++
#include <iostream>
#include <cmath>
using namespace std;
class Point {
protected:
int x, y;
public:
Point(int a, int b) {
x = a;
y = b;
}
void setPoint(int a, int b) {
x = a;
y = b;
}
void showPoint() {
cout << "(" << x << ", " << y << ")" << endl;
}
};
class Circle : public Point {
private:
double radius;
public:
Circle(int a, int b, double r) : Point(a, b) {
radius = r;
}
void setCircle(int a, int b, double r) {
setPoint(a, b);
radius = r;
}
double getRadius() {
return radius;
}
double getArea() {
return M_PI * radius * radius;
}
};
int main() {
Circle c1(0, 0, 5); // 创建一个圆心为(0, 0),半径为5的圆
Circle c2(3, 4, 2.5); // 创建一个圆心为(3, 4),半径为2.5的圆
// 显示两个圆的信息
cout << "c1的圆心坐标为:";
c1.showPoint();
cout << "c1的半径为:" << c1.getRadius() << endl;
cout << "c1的面积为:" << c1.getArea() << endl;
cout << "c2的圆心坐标为:";
c2.showPoint();
cout << "c2的半径为:" << c2.getRadius() << endl;
cout << "c2的面积为:" << c2.getArea() << endl;
return 0;
}
```
定义坐标类point,私有数据成员x和y,公有数据成员构造函数,析构函数,显示坐标函数,利用友元函数distance访问类的成员计算距离,主函数中创建两点间距离(要求构造函数带有默认函数)这样的c++代码怎么写?
以下是符合您要求的 C++ 代码:
```c++
#include <iostream>
#include <cmath>
using namespace std;
class point {
private:
double x;
double y;
public:
// 构造函数
point(double x = 0, double y = 0) {
this->x = x;
this->y = y;
}
// 析构函数
~point() {}
// 显示坐标函数
void display() const {
cout << "(" << x << ", " << y << ")" << endl;
}
// 友元函数,计算两点间距离
friend double distance(const point& p1, const point& p2) {
double dx = p1.x - p2.x;
double dy = p1.y - p2.y;
return sqrt(dx * dx + dy * dy);
}
};
int main() {
point p1(0, 0);
point p2(3, 4);
cout << "p1: ";
p1.display();
cout << "p2: ";
p2.display();
cout << "Distance between p1 and p2: " << distance(p1, p2) << endl;
return 0;
}
```
在上面的代码中,我们定义了一个 `point` 类,该类具有 `x` 和 `y` 两个私有数据成员,和一些公有成员函数和友元函数。其中,构造函数带有默认参数,析构函数为空。`display` 函数用于显示坐标信息,`distance` 函数是一个友元函数,用于计算两点间距离。
在 `main` 函数中,我们创建了两个 `point` 类型的对象 `p1` 和 `p2`,并分别初始化它们的坐标。然后调用 `display` 函数显示它们的坐标,并调用 `distance` 函数计算两点间距离并输出。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)