点类的定义
point.h:
//point.h
#ifndef _POINT_
#define _POINT_
//点类的定义
class Point {
public:
//默认构造
Point();
//构造函数
Point(double, double);
//拷贝构造
Point(const Point&);
//获取点x,y坐标
double getX();
double getY();
//设置点坐标
void setPoint(double, double);
//重载赋值运算
void operator=(const Point&);
//打印点坐标
void show();
private:
double x, y;
};
#endif
point.cpp:
//point.cpp
#include<iostream>
#include "point.h"
using namespace std;
//点类的实现
Point::Point() { //默认构造
this->x = 0;
this->y = 0;
}
Point::Point(double x, double y) { //构造函数
this->x = x;
this->y = y;
}
Point::Point(const Point& p){ //拷贝构造
this->x = p.x;
this->y = p.y;
}
double Point::getX() { return x; } //获取点x坐标
double Point::getY() { return y; } //获取点y坐标
void Point::setPoint(double x, double y){ //设置点坐标
this->x = x;
this->y = y;
}
void Point::operator=(const Point& p) { //重载赋值运算
this->x = p.x;
this->y = p.y;
}
void Point::show(){ //打印点坐标
cout << "(" << x << "," << y << ")"<<endl;
}
主函数:
//main
#include<iostream>
#include "point.h"
using namespace std;
int main() {
Point a;
Point b(1.2, 2.3);
Point c = b;
a = c;
double m = 8;
b.setPoint(5, m);
double x = b.getX(), y = b.getY();
cout << x << " " << y << endl;
a.show();
a.show();
c.show();
return 0;
}