Point类是一个表示二维空间中的点的类,包括点的横纵坐标信息。下面是一个简单的Point类的设计与实现:
// Point.h#ifndef POINT_H#define POINT_Hclass 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; } // 设置横坐标 void setX(double newX) { x = newX; } // 设置纵坐标 void setY(double newY) { y = newY; } // 获取到原点的距离 double distanceToOrigin() const { return sqrt(x*x + y*y); }};#endif// Point.cpp#include "Point.h"#include <cmath>// 可以在这里添加更多函数的实现使用Point类的示例代码:
#include <iostream>#include "Point.h"int main() { Point p1(3, 4); std::cout << "Point p1: (" << p1.getX() << ", " << p1.getY() << ")" << std::endl; std::cout << "Distance to origin: " << p1.distanceToOrigin() << std::endl; p1.setX(6); p1.setY(8); std::cout << "Point p1 after setting new coordinates: (" << p1.getX() << ", " << p1.getY() << ")" << std::endl; return 0;}以上就是一个简单的Point类的设计与实现,可以根据需要添加更多的成员函数或者功能。