#include<iostream>
#include<cmath>
using namespace std;
class Point { //Point 类定义
public: //外部接口
Point(int x = 0, int y = 0) : x(x), y(y) { //构造函数
//count++; //在构造函数中对count累加,所有对象共同维护同一个count
}
//Point(Point& p) //复制构造函数
//{
// x = p.x;
// y = p.y;
// count++;
//}
//~Point() { count--; }
int getX() { return x; }
int getY() { return y; }
friend float dist(Point& a, Point& b); //友元
//static void showCount() { //输出静态数据成员
// cout << "Object.count=" << count << endl;
//}
private:
int x, y;
static int count;//静态数据成员声明,用于记录点的个数
};
float dist(Point& a, Point& b)
{
double x = a.x - b.x;
double y = a.y - b.y;
return static_cast<float>(sqrt(x * x + y * y));
}
int main()
{
Point p1(1, 1), p2(4, 5);
cout << "The distance is:";
cout << dist(p1, p2) << endl;
return 0;
}
大量调用函数会影响执行效率,故会使用友元。友元可以直接调用类里面的变量