建二维坐标系

建二维坐标系类,重载运算符,
实现两坐标点的加,减,
计算两坐标点间的距离,
并重载输入和输出运算符,
使之能够直接输入和输出坐标点的坐标值。

#include "pch.h"
#include 
#include
using namespace std;

class TwoCoord {
private:
	int x, y;
public:
	TwoCoord(int a = 0, int b = 0) :x(a), y(b) {};
	TwoCoord operator+(TwoCoord b);
	TwoCoord operator-(TwoCoord b);
	double operator/(TwoCoord b);
	void display();
	friend ostream &operator<<(ostream &os, TwoCoord &s);
	friend istream &operator>>(istream &is, TwoCoord &s);
};
TwoCoord TwoCoord::operator+(TwoCoord b) { return TwoCoord(x + b.x, y + b.y); }
TwoCoord TwoCoord::operator-(TwoCoord b) { return TwoCoord(x - b.x, y - b.y); }
double TwoCoord::operator/(TwoCoord b) { return double(pow(pow(x-b.x,2)+pow(y-b.y,2),0.5)); }
void TwoCoord::display() {
	cout << "[" << x << "," << y << "]" << "\n";
}
ostream &operator<<(ostream &os, TwoCoord &s) {
	os << "[" << s.x << "," << s.y << "]" << "\n";
	return os;
}
istream &operator>>(istream &is, TwoCoord &s) {
	cout << "输入坐标" << "\n";
	is >> s.x >> s.y;
	return is;
}

int main()
{
	double d;
	TwoCoord A,B,C,D;
	cin >> A;
	cin >> B;
	C = A + B; D = A - B;
	d = A / B;
	cout << "两坐标和="<

你可能感兴趣的:(重载运算符,C++)