看一下这题吧 C++类与对象设计

1. 请先定义一个点类(Point),私有数据成员为点的坐标(X和Y),有参构造函数置X和Y为对应形参的值,另外还包括求两点距离的友元函数(Distance())和显示点的值的公有成员函数(Display()),要求按“(x,y)”的形式显示一个点的信息;然后编写主函数进行测试,要求在测试函数中,输出两点距离的格式为“(x1,y1)---(x2,y2)的距离为:距离”。(Sy3_1.cpp)

在此粘贴程序代码和运行结果的截图。        

效果图和代码如下:

如有帮助,请给个采纳,谢谢。

代码:

#include <iostream>
#include <math.h>
using namespace std;
class Point
{
private:
	int X,Y;
public:
	Point(int x,int y){X = x; Y = y;}
	friend double Distance(Point p1,Point p2);
	void Display(){cout << "(" << X << "," << Y << ")";}
};

double Distance(Point p1,Point p2)
{
	return sqrt(double (p1.X - p2.X)*(p1.X - p2.X) + (p1.Y - p2.Y)*(p1.Y - p2.Y) );
}

int main()
{
	Point p1(2,3);
	Point p2(5,8);
	double dis;
	
	dis = Distance(p1,p2);
	p1.Display();
	cout << "---";
	p2.Display();
	cout << "的距离为:" << dis << endl;
	return 0;
}

 

#pragma warning(disable:4996)
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<stdbool.h>
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
#define mem(a, sum) memset(a, sum, sizeof a)

//点
class Point {
public:
	Point(double a = 0, double b = 0) :x(a), y(b) {}
	void Dispaly();
	double Distance_(Point a);
	friend double Distance(Point a, Point b);
	void set(double a, double b);
	int query(int n);
private:
	double x, y;
protected:

};

//点类函数定义
inline void Point::Dispaly() {
	printf("(%.2lf, %.2lf)", x, y);
}
double Point::Distance_(Point a) {
	double tem = std::pow(a.x - x, 2) + std::pow(y - a.y, 2);
	tem = std::sqrt(tem);
	//printf("到目标点的距离是%lf \n", tem);
	return tem;
}
double Distance(Point a, Point b) {
	return a.Distance_(b);
}
void Point::set(double a, double b) {
	this->x = a;
	this->y = b;
	return;
}
//从该点到结束点一共n个点 寻找x + y 的最大值并返回下标
int Point::query(int n) {
	int sum = this->x + this->y, i = 0, ans = 0;
	if (n <= 1)return ans;
	for (i = 1; i <= n - 1; i++) {
		//printf("%lf %lf\n", (this + i)->x, (this + i)->y);
		if ((this + i)->x + (this + i)->y > sum) {
			sum = (this + i)->x + (this + i)->y;
			ans = i;
		}
	}
	return ans;
}


void show(Point a, Point b) {
	a.Dispaly(); 
	printf("---");
	b.Dispaly();
	printf("的距离为:%.2lf", Distance(a, b));
	return;
}

//主函数
int main() {
	double p1, p2;
	printf("请输入点一的坐标:\n");
	scanf("%lf %lf", &p1, &p2);
	Point a(p1, p2);
	printf("请输入点二的坐标:\n");
	scanf("%lf %lf", &p1, &p2);
	Point b(p1, p2);
	show(a, b);
	return 0;
}