C++实验报告
实验名称
友元函数和友元类的定义及使用
实验目的
理解对象与类的关系,掌握对象的创建和使用
掌握构造函数、析构函数的概念及使用方法
掌握内存的动态分配的概念和使用方法
掌握对象数组和对象指针
掌握函数调用中参数的传递
掌握友元函数和友元类的定义及使用
class 类名 {
private:
数据成员或成员函数
protected:
数据成员或成员函数
public:
数据成员或成员函数
};
实验内容
有 Distance 类和 Point 类,将 Distance 类定义为 Point 类的友元类来实现计算
两点之间距离
实验代码
// point.h
class Distance;
class Point
{
public:
Point(int xx=0,int yy=0)
{
X=xx;
Y=yy;
}
friend class Distance;
private:
int X,Y;
};
class Distance
{
public:
float fDist(Point a,Point b);
};
//point.cpp
#include"iostream"
1