c++程序中进行设计

#include
usingnamespacestd;
classB;classA{int i; public:int set(B&b);int get(){returni;}A(intx){i=x;}};classB{intj;public:B(intx){j=x;}friendA;   //行a};
intA::set(B&b){
returni=b.j;}
int main(){Aa(1);Bb(2);
cout<<a.get()<<",";
a.set(b);
cout<<a.get()<<endl;
return0;}
思考题:你能说出程序运行结果的理由吗?如果将”//行a”中的”friendA;”删除,还能够得到所要的结果吗:为什么?

A a(1) a.i被设成1, a.get()返回1
B b(2) b.j被设成2
a.set(b) 就把b.j赋值给a.i 此时a.get()就返回2
如果去掉friend A; 友类关联,调用到下面这个函数时就会出错: b.j 找不到变量
int A::set(B&b)
{
return i=b.j;
}

#include<iostream>
using namespace std;

class B;
class A{
    int i;
public:
    int set(B&b);
    int get(){
        return i;
    }
    A(int x){
        i=x;
    }
    };
class B{
    int j;
    public:
        B(int x){
            j=x;
        };

};

int A::set(B&b)
{
    return i=b.j;
}

int main()
{
    A a(1);
    B b(2);
    cout<<a.get()<<",";
    a.set(b);
    cout<<a.get()<<endl;
    return 0;
}