Zhangsan.h
#include "Lisi.h"
#include "Wangwu.h"
class Zhangsan {
//友元类
friend class Wangwu;
//友元成员函数
friend void Lisi::jz();
//友元全局函数
friend void huaqian2();
public:
int age;
private:
int money;
public:
Zhangsan();
Zhangsan(int money);
Zhangsan(int age, int money);
~Zhangsan();
};
Zhangsan.cpp
#include "Zhangsan.h"
Zhangsan::Zhangsan() {}
Zhangsan::Zhangsan(int money) : money(money) {}
Zhangsan::Zhangsan(int age, int money) : age(age), money(money) {}
Zhangsan::~Zhangsan() {}
Lisi.h
class Lisi {
public:
void chwl();
void jz();
};
Lisi.cpp
#include "Lisi.h"
#include "Zhangsan.h"
void Lisi::chwl() {
Zhangsan zs;
zs.age =10;
//zs.money =100;
}
void Lisi::jz() {
Zhangsan zs;
zs.age =10;
zs.money =100;
}
Wangwu.h
class Wangwu {
public:
void htjd();
void hj();
};
Wangwu.cpp
#include "Wangwu.h"
#include "Zhangsan.h"
void Wangwu::hj() {
Zhangsan zs;
zs.age = 20;
zs.money=2000;
}
void Wangwu::htjd() {
Zhangsan zs;
zs.age = 20;
zs.money=2000;
}
Test.cpp
#include <iostream>
#include "Zhangsan.h"
using namespace std;
void huaqian1(){
cout<<"全局函数"<<endl;
Zhangsan zs(19,500);
zs.age-=1;
//zs.money+=50;
}
void huaqian2(){
cout<<"全局函数"<<endl;
Zhangsan zs(19,500);
zs.age-=1;
zs.money+=50;
}
int main() {
huaqian1();
huaqian2();
return 0;
}