#include<stdio.h>
class C;
class B;
class A{
friend class C;
friend bool check(A a,B b);
friend bool operator>(A& a,B& b);
int aNum;
bool flag;
public:
A(bool a,int n){
flag = a;
aNum = n;
}
};
class B{
friend bool check(A a,B b);
friend bool operator>(A& a,B& b);
double bNum;
bool flag;
public:
B(bool a,double b){
flag = a;
bNum = b;
}
};
class C
{
public:
static void print(A a){printf("friend class access class A's private member %d--%d\n",a.aNum,a.flag);}
};
bool check(A a,B b){
if(a.flag && b.flag)
return true;
else
return false;
}
bool operator>(A& a,B& b){
return a.aNum>b.bNum;
}
int main(){
A a(true,9);
B b(true,9.3);
if(check(a,b))
printf("checked!\n");
if(a>b){
printf("a>b\n");
}else{
printf("a<b\n");
}
C::print(a);
return 0;
}
友元函数 友元类
最新推荐文章于 2024-06-20 09:00:44 发布