2次元座標系のクラスTwoCoorを確立し、x、yで座標値を表し、2座標点の加減演算を実現し、2座標点間の距離を計算し、入出力演算子を再ロードし、出力座標点の座標値を直接入力できるようにする.

1163 ワード

ヘッダファイル名:hanshu.h
#include"iostream"
#include"math.h"
using namespace std;
class TwoCoor{
private:
	double x,y;
public:
	TwoCoor* operator+(TwoCoor t)
	{
		TwoCoor *p;
		p=new TwoCoor;
		p->x=this->x+t.x;
		p->y=this->y+t.y;
		return(p);
	}
	TwoCoor* operator-(TwoCoor t)
	{
		TwoCoor *p;
		p=new TwoCoor;
		p->x=this->x-t.x;
		p->y=this->y-t.y;
		return(p);
	}
	double diatance(TwoCoor);
	friend void operator<>(istream &is,TwoCoor &t)
	{
		cout << "      :" << endl;
		is >> t.x ;
		is >> t.y ;
	}
};

double TwoCoor::diatance(TwoCoor b)
{
	double d;
	d=sqrt(pow((this->x-b.x),2)+pow(this->y-b.y,2));
	return d;
}

マザーボードの数:
#include"iostream"
#include"hanshu.h"
using namespace std;
void main()
{
	TwoCoor a,b;
	cin >> a ;
	cin >> b ;
	cout << a ;
	cout << endl;
	cout << b ;
	cout << endl;
	cout << *(a+b) ;
	cout << endl ;
	cout << *(a-b) ;
	cout << endl ;
	cout << "     :  " ;
	cout << a.diatance(b) << endl;
}