1 //友元分为友元类和友元成员函数和友元函数 2 //设计A类是B类的友元类 3 //那么A类可以访问B类的所有成员(公有、私有、保护成员),同理,友元类和友元函数也是一样 4 #include<iostream.h> 5 class B{ 6 friend class A; 7 private: 8 int topSecret; 9 public: 10 B(){} 11 B(int t=0){ 12 this->topSecret=t; 13 } 14 ~B(){} 15 int GetTopSecret(){ 16 return this->topSecret; 17 } 18 }; 19 20 class A{ 21 public: 22 A(){} 23 ~A(){} 24 void change(B &b)const{ 25 b.topSecret++; 26 } 27 }; 28 int main(){ 29 B b(0); 30 A a; 31 a.change(b); 32 cout<<b.GetTopSecret()<<endl; 33 return 0; 34 }
C++学习(9)
猜你喜欢
转载自www.cnblogs.com/Tobi/p/9244990.html
今日推荐
周排行