0
点赞
收藏
分享

微信扫一扫

实训四 数据的保护与共享

眼君 2022-04-30 阅读 52

一、实训目的
(1)掌握对象与类的定义与使用;
(2)掌握类的静态成员定义及使用;
(3)掌握友元函数及友元类的用法;

二、实训内容
(1)定义Point类,利用静态整型数据成员count实现对点的个数的统计。
(2)定义Point类的友元函数dist实现对两个点之间距离的计算。
(3)调试运行。
(4)记录结果。
(5)撰写实验报告。

三、实训所实现系统主要功能
(1)定义Point类,利用静态整型数据成员count实现对点的个数的统计。
(2)定义Point类的友元函数dist实现对两个点之间距离的计算。

四、实训系统核心代码及必要说明

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;

class Point {
private:
	double x;
	double y;
	static int count;
public:
		
	Point(double xx, double yy);
	~Point();
	static void showcount()
	{
		cout << "当前类中点的个数为:" << count << endl;
	}
	friend double dist(const Point& p1, const Point& p2);
};

int Point::count = 0;
Point::Point(double xx = 0, double yy = 0)
{
	x = xx;
	y = yy;
	count++;
}
Point:: ~Point(){
	count--;
}
double dist(const Point& p1, const Point& p2)
{
	double d = sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
	return d;

}


int main() {
	int x, y;
	cout << "输入a点坐标:";
	cin >> x >> y;
	Point pa(x, y);
	cout << "输入b点坐标:";
	cin >> x >> y;
	Point pb(x, y);
	cout << "a、b两点的距离为:" << dist(pa, pb) << endl;
	Point::showcount();

	return 0;
}

五、实训结果截图及分析
在这里插入图片描述

举报

相关推荐

0 条评论