c++友元函数类的使用
参考:
#include <iostream> using namespace std; class Complex{ friend Complex operator+(Complex &c1, Complex &c2); //声明友元函数 public: Complex(double r=0, double i=0):real(r),image(i){} void display(){ cout<<"("<<real<<","<<image<<")"<<endl; } private: double real; double image; }; Complex operator+(Complex &c1, Complex &c2){ Complex c; c.real = c1.real + c2.real; //访问私有的成员变量,不被允许 //声明友元函数后可以访问私有成员变量 c.image = c1.image + c2.image; //访问私有的成员变量,不被允许 return c; } int main() { Complex c1(2,3), c2(4,5); Complex c3(0,0); c3 = c1 + c2; c3.display(); return 0; }
全局友元:
#include <iostream> #include <cmath> using namespace std; class Point{ //声明友元函数 friend float distance(const Point &p1,const Point &p2); private: float _x, _y; public: Point(float x=0, float y=0):_x(x),_y(y){}; void display(){ cout<<"("<<_x<<","<<_y<<")"<<endl; } }; //距离计算函数 float distance(const Point &p1,const Point &p2){ float dx = p1._x - p2._x; float dy = p1._y - p2._y; //计算两点之间的距离 return sqrt(dx*dx + dy*dy); } int main(){ Point p1(3,4); p1.display(); Point p2(7,8); p2.display(); float d = distance(p1,p2); cout<<"距离 p1 --- p2 "<<d<<endl; return 0; }
成员函数友元:
#include <iostream> #include <cmath> using namespace std; //!前向声明 是一种不完全类型的声明,不能定义对象, //! 但可以定义指针和引用,因为指针和引用都是4字节,都是确定的大小 //!作为参数和返回值,仅用在函数声明中 class Point; //前向声明 //成员函数作友元 class ManagerPoint { public: //距离计算函数 //由于用到的Point,还没定义,结合前向声明的特点, //在此处只能声明函数,,这个函数的实现只能放在Point函数实现之后了 float distance(const Point &p1, const Point &p2); }; class Point{ //声明友元函数 friend float ManagerPoint::distance(const Point &p1,const Point &p2); private: float _x, _y; public: Point(float x=0, float y=0):_x(x),_y(y){}; void display(){ cout<<"("<<_x<<","<<_y<<")"<<endl; } }; //距离计算函数 float ManagerPoint::distance(const Point &p1, const Point &p2) { float dx = p1._x - p2._x; float dy = p1._y - p2._y; //计算两点之间的距离 return sqrt(dx * dx + dy * dy); } int main(){ Point p1(3,4); p1.display(); Point p2(7,8); p2.display(); ManagerPoint mp; float d = mp.distance(p1,p2); cout<<"距离 p1 --- p2 "<<d<<endl; return 0; }
友元类:
#include <iostream> #include <cmath> using namespace std; class Point{ //声明友元类 friend class ManagerPoint; private: float _x, _y; public: Point(float x=0, float y=0):_x(x),_y(y){}; void display(){ cout<<"("<<_x<<","<<_y<<")"<<endl; } }; class ManagerPoint { public: //距离计算函数 //由于用到的Point,还没定义,结合前向声明的特点, //在此处只能声明函数,,这个函数的实现只能放在Point函数实现之后了 float distance(const Point &p1, const Point &p2); }; //距离计算函数 float ManagerPoint::distance(const Point &p1, const Point &p2) { float dx = p1._x - p2._x; float dy = p1._y - p2._y; //计算两点之间的距离 return sqrt(dx * dx + dy * dy); } int main(){ Point p1(3,4); p1.display(); Point p2(7,8); p2.display(); //使用友元类 ManagerPoint mp; float d = mp.distance(p1,p2); cout<<"距离 p1 --- p2 "<<d<<endl; return 0; }
欢迎讨论,相互学习。
cdtxw@foxmail.com
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)