用c++定义Point类,包含坐标成员,可输出显示坐标值。 在主函数中输入若干点坐标,保存在vector对象中,将点坐标倒置后输出。
时间: 2024-02-12 13:07:49 浏览: 55
好的,我可以帮你解决这个问题。以下是C++定义Point类的代码实现:
```
#include <iostream>
#include <vector>
using namespace std;
class Point {
public:
Point(int x, int y): x_(x), y_(y) {}
void display() {
cout << "(" << x_ << ", " << y_ << ")" << endl;
}
private:
int x_;
int y_;
};
int main() {
vector<Point> points;
int x, y;
while (cin >> x >> y) {
Point point(x, y);
points.push_back(point);
}
for (int i = points.size() - 1; i >= 0; i--) {
points[i].display();
}
return 0;
}
```
首先定义了一个Point类,包含x和y坐标成员变量和一个display()函数用于输出坐标值。然后在主函数中,通过循环和读入操作将输入的点坐标保存在vector对象中。最后,通过循环倒序输出vector中保存的点坐标,调用Point类的display()函数实现输出功能。
相关问题
用C++定义Point类,包含坐标成员,可输出显示坐标值。 在主函数中输入若干点坐标,保存在vector对象中,将点坐标倒置后输出。
好的,这是一个简单的 Point 类的定义,以及倒置坐标的实现:
```c++
#include <iostream>
#include <vector>
class Point {
public:
Point(int x, int y) : x_(x), y_(y) {}
void display() const {
std::cout << "(" << x_ << ", " << y_ << ")" << std::endl;
}
void flip() {
int temp = x_;
x_ = y_;
y_ = temp;
}
private:
int x_;
int y_;
};
int main() {
std::vector<Point> points;
int x, y;
int n;
std::cout << "请输入点的数量:";
std::cin >> n;
for (int i = 0; i < n; ++i) {
std::cout << "请输入第 " << i+1 << " 个点的坐标:";
std::cin >> x >> y;
Point p(x, y);
points.push_back(p);
}
std::cout << "倒置前的坐标:" << std::endl;
for (auto p : points) {
p.display();
}
std::cout << "倒置后的坐标:" << std::endl;
for (auto& p : points) {
p.flip();
p.display();
}
return 0;
}
```
在这个程序中,我们定义了一个 Point 类,有两个私有成员变量 x_ 和 y_,以及两个公有成员函数:display() 和 flip()。display() 函数用于输出坐标,flip() 函数用于倒置坐标。在主函数中,我们使用 vector 容器存储点坐标,然后依次输出点的坐标,再倒置坐标后输出。
定义Point类,包含坐标成员,可输出显示坐标值。 在主函数中输入若干点坐标,保存在vector对象中,将点坐标倒置后输出。 定义Point类,包含坐标成员,可输出显示坐标值。 在主函数中输入若干点坐标,保存在vector对象中,将点坐标倒置后输出。
好的,我可以帮你解答这个问题。下面是代码实现:
```c++
#include <iostream>
#include <vector>
using namespace std;
class Point {
private:
int x;
int y;
public:
Point(int a=0, int b=0) : x(a), y(b) {}
void show() { cout << "x=" << x << ", y=" << y << endl; }
};
int main() {
vector<Point> points;
int a, b;
cout << "请输入点的坐标(x, y),以空格分隔,输入-1 -1结束输入:" << endl;
while(cin >> a >> b) {
if(a==-1 && b==-1) break;
Point p(a, b);
points.push_back(p);
}
cout << "原始点坐标:" << endl;
for(int i=0; i<points.size(); i++) {
points[i].show();
}
cout << "倒置后的点坐标:" << endl;
for(int i=points.size()-1; i>=0; i--) {
points[i].show();
}
return 0;
}
```
首先定义了一个Point类,包括两个成员变量x和y,以及一个显示成员函数show(),用于输出点的坐标值。
主函数中定义了一个vector对象points,用于保存输入的点坐标。然后通过while循环输入若干点坐标,输入-1 -1表示输入结束。输入结束后,首先输出原始点坐标,然后按照倒置的顺序输出点坐标。
以上就是解答,希望能对你有所帮助。