标签:cout 它的 src color NPU friend 3.3 www. sed
1、什么是友元函数?
通常一个类的私有函数只能在该类的内部进行访问,但当类允许其他类或者函数访问它的非共有成员时,方法是令其他类或者函数成为友元函数。
eg:友元函数声明定义使用
#include <iostream> using namespace std; class Car { friend void display(Car); private: int speed; char color[20]; public: void input() { cout<<"Enter the speed:"<<endl; cin>>speed; cout<<"Enter the color:"<<endl; cin>>color; } }; void display(Car x)//友元函数的定义 { cout<<"The speed of the car is:"<<x.speed<<endl; cout<<"The color of the car is:"<<x.color<<endl; } int main() { Car mine; mine.input(); display(mine); cout << "Hello World!" << endl; return 0; }
eg:多类友元函数
#include <iostream> using namespace std; class Train;//提前声明 Train 是一个类 class Car { friend void display(Car,Train);//友元函数 private: int speed; char color[20]; public: void input() { cout<<"Enter the speed of the car:"<<endl; cin>>speed; cout<<"Enter the color of the car:"<<endl; cin>>color; } }; class Train { friend void display(Car,Train);//友元函数 private: int speed; char color[20]; public: void input() { cout<<"Enter the speed of the train:"<<endl; cin>>speed; cout<<"Enter the color of the train:"<<endl; cin>>color; } }; void display(Car c,Train t)//友元函数的定义 { if(c.speed>t.speed) { cout<<"The car is faster!!"<<endl; cout<<"The speed of the car is:"<<c.speed<<endl; cout<<"The color of the car is:"<<c.color<<endl; } else { cout<<"The train is faster!!"<<endl; cout<<"The speed of the train is:"<<t.speed<<endl; cout<<"The color of the train is:"<<t.color<<endl; } } int main() { Car mine; mine.input(); Train goverment; goverment.input(); display(mine,goverment); return 0; }
eg:友元类的声明定义使用
#include <iostream> using namespace std; class support { private: int x,y; public: void init_add(int a,int b) { x=a; y=b; } friend class Add; }; class Add { public: void add(support s) { cout<<s.x<<"+"<<s.y<<"="<<s.x+s.y<<endl; } }; int main() { support s; s.init_add(3,4); Add a; a.add(s); return 0; }
2、友元函数重载操作符
①成员函数重载操作符
#include <iostream> #include <cstdlib> using namespace std; class A { private: int a; int b; public: A(int x=0,int y=0):a(x),b(y){} A operator +(A &a1); }; A A::operator +(A &a1) { A a2; a2.a=this->a+a1.a; a2.b=this->b+a1.b; cout<<a2.a<<‘\t‘<<a2.b<<endl; return a2; } int main() { A a1(5,6); A a2(7,8); A a3; a3=a1+a2; return 0; }
②友元函数重载操作符
#include <iostream> #include <cstdlib> using namespace std; class complex { private: double real,imag; public: complex(double,double);//初始化 void show();//输出 friend complex operator +(const complex &c1,const complex &c2);//友元函数运算符重载 }; inline complex::complex(double r1, double r2)//内联函数 { real=r1; imag=r2; } inline void complex::show()//内联函数 { cout<<‘(‘<<real<<‘,‘<<imag<<‘)‘<<endl; } complex operator +(const complex &c1,const complex &c2) { complex c3(0,0); c3.real=c1.real+c2.real; c3.imag=c1.imag+c2.imag; return c3; } int main() { complex c1(1.4,4.5),c2(3.4,5.7); complex c3=c1+c2; c3.show(); return 0; }
3. 使用友元函数前应注意:
3.1 类的友元函数在类作用域之外定义,但可以访问类的私有和保护成员
3.2 尽管类定义中有友元函数原型,友元函数仍然不是成员函数
3.3 由于友元函数不是任何类的成员函数,所以不能用句柄(对象)加点操作符来调用
3.4 public, private, protected成员访问符与友员关系的声明无关,因此友元关系声明可在类定义的任何位置,习惯上在类定义的开始位置
3.5 友元关系是指定的,不是获取的,如果让类B成为类A的友元类,类A必须显式声明类B为自己的友元类
3.6 友元关系不满足对称性和传递性
3.7 如果一个友元函数想与两个或更多类成为友元关系,在每个类中都必须声明为友元函数
参考博客:
https://www.cnblogs.com/Mayfly-nymph/p/9034936.html
https://blog.csdn.net/tobe_numberone/article/details/78137409
https://blog.csdn.net/liubinstud/article/details/6134125
标签:cout 它的 src color NPU friend 3.3 www. sed
原文地址:https://www.cnblogs.com/ybf-yyj/p/9325656.html