33,311
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
using namespace std;
class A
{
int x;
public:
friend class B;
A(int b)
{
x=b;
}
void Print()
{
cout<<x<<endl;
}
};
class B
{
A a;
int y;
public:
void set()
{
y=a.x;
}
void Print1()
{
cout<<"i am called "<<endl;
cout<<y<<endl;
a.Print();
}
};
int main()
{
A b=3;
b.Print();
B x;
x.set();
x.Print1();
return 0;
}
#include<iostream>
using namespace std;
class A
{
int x;
public:
friend class B;
A(int b)
{
x=b;
}
A() // 提供一个A的默认构造就行
{
x = 0;
}
void Print()
{
cout<<x<<endl;
}
};
class B
{
A a;
int y;
public:
void set()
{
y=a.x;
}
void Print1()
{
cout<<"i am called "<<endl;
cout<<y<<endl;
a.Print();
}
};
int main()
{
A b=3;
b.Print();
B x;
x.set();
x.Print1();
return 0;
}