二级模拟试题:C++习题与解析(友元-02) |
|
www.nanhushi.com 佚名 不详 |
02.分析以下程序的执行结果 #include class B; class A { int i; public: int set(B&); int get(){return i;} A(int x){i=x;} }; class B { int i; public: B(int x){i=x;} friend A; }; int A::set(B &b) // 由于使用了类B的定义,故本函数的定义应放在类B定义之后 { return i=b.i; } void main() { A a(1); B b(2); cout<a.set(b); cout<}
解: 本题说明友元类的使用方法。这里将类A设置为类B的友元类,因此,类A的所有成员函数均为类B的友元函数。通过调用a.set(b)将b对象的i值赋给a对象的i值。 来源:www.examda.com 所以输出为:1,2
|
|
|
文章录入:杜斌 责任编辑:杜斌 |
|
上一篇文章: 二级模拟试题:C++习题与解析(友元-03) 下一篇文章: 二级模拟试题:C++习题与解析(友元-04) |
【字体:小 大】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口】 |
|
|