类的友元

//友元类
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
class A
{
public:
    A(int x):x(x){}
    friend class B;
    void show(){cout<<x<<endl;}
private:
    int x;
};
class B
{
public:
    B(int y,A &K):y(y),K(K){}
    void show(){cout<<y<<endl;}
    void bianshen();
private:
    A K;
    int y;
};
inline void B::bianshen()
{
    cout<<K.x<<endl;
}
int main()
{
    A K(3);
    K.show();
    B c(2,K);
    c.show();
    c.bianshen();
}
//友元函数
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
class A
{
public:
    A(int x):x(x){}
    friend void showw(A &K);
    void show(){cout<<x<<endl;}
private:
    int x;
};
void showw(A &K)
{
    cout<<K.x<<endl;
}
int main()
{
    A fnq(22);
    fnq.show();
    showw(fnq);
}

猜你喜欢

转载自blog.csdn.net/qq_42576687/article/details/88958639