预测以下程序的输出。
null
#include <iostream> using namespace std; class A { protected : int x; public : A() {x = 0;} friend void show(); }; class B: public A { public : B() : y (0) {} private : int y; }; void show() { A a; B b; cout << "The default value of A::x = " << a.x << " " ; cout << "The default value of B::y = " << b.y; } |
(A) show()中的编译器错误,因为x在类A中受保护 (B) show()中的编译器错误,因为y在类b中是私有的 (C) A::x=0的默认值B::y=0的默认值 (D) 编译器相关 答复: (B) 说明: 请注意,show()是类a的朋友,因此在访问in show()中的任何成员时不应出现任何编译器错误。
B类是从A类继承来的,这里需要注意的一点是友谊不是继承来的。所以show()不会成为B的朋友,因此无法访问B的私人成员。 这个问题的小测验
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END