预测以下程序的输出。
null
#include <iostream> using namespace std; class B; class A { int a; public : A():a(0) { } void show(A& x, B& y); }; class B { private : int b; public : B():b(0) { } friend void A::show(A& x, B& y); }; void A::show(A& x, B& y) { x.a = 10; cout << "A::a=" << x.a << " B::b=" << y.b; } int main() { A a; B b; a.show(a,b); return 0; } |
(A) 编译错误 (B) A::A=10 B::B=0 (C) A::A=0 B::B=0 答复: (B) 说明: 这是一个简单的程序,其中类a的函数被声明为类B的朋友。
因为show()是friend,所以它可以访问B的私有数据成员。 这个问题的小测验
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END