在某些情况下需要实现某些功能,但是这个类的自身由于某些原因无法自己实现,需要借助"外力(外部函数)"才可以实现的时候就需要使用友元;
以全局函数作为友元函数:
#include <iostream>
#include <sstream>
using namespace std;
class COMPUTER {
public:
COMPUTER() {
this->cpu = "i7";
this->SSD = "希捷";
this->SIZE = 500;
}
string descounpriter() {
stringstream ret;
ret << "CPU:" << cpu << "\n硬盘型号:" << SSD << "\n硬盘大小:" << SIZE << "G";
return ret.str();
}
private:
string cpu;
string SSD;
int SIZE;
//把这个全局变量放到这个类的内部,在函数前加 friend 就是友元函数
friend void function(COMPUTER&DILL);
};
需要对这个计算机的类进行升级,但是这个类无法自己对自己升级,需要外部的帮助时:
#include "COMPUTER.h"
//需要对这个计算机升级;定义一个全局函数
void function(COMPUTER&DILL) {//先给这个类定义一个类的形参对象
DILL.cpu = "i9";
DILL.SSD = "西部数据";
DILL.SIZE = 1000;
}
int main(void) {
COMPUTER T1;
cout << T1.descounpriter() << endl;
function(T1);
cout << "升级后:\n" << T1.descounpriter() << endl;
system("pause");
return 0;
}
运行结果:
以类内成员函数作为友元函数:
#include <iostream>
#include <sstream>
#include "GAMES.h"
using namespace std;
class COMPUTER {
public:
COMPUTER() {
this->cpu = "i7";
this->SSD = "希捷";
this->SIZE = 500;
}
string descounpriter() {
stringstream ret;
ret << "CPU:" << cpu << "\n硬盘型号:" << SSD << "\n硬盘大小:" << SIZE << "G";
return ret.str();
}
private:
string cpu;
string SSD;
int SIZE;
//用COMPUTER2这个类的成员函数来作为友元函数,实现同样功能
friend void GAMES::kis(COMPUTER* K);
};
注意:在VS2019不可以在类的 .h 头文件里直接实现这个成员函数,不然会报错;
class COMPUTER;//成员函数使用了COMPUTER这个类的指针,所以要包含COMPUTER这个类在头文件里
class GAMES{
public:
void kis(COMPUTER* K);
};
``
在cpp文件中实现就不会报错;
```cpp
#include "GAMES.h"
#include "COMPUTER.h"//在这个文件里需要使用COMPUTER这个类的数据,所以需要这个类的头文件
void GAMES::kis(COMPUTER* K){
K->cpu = "i7-7700HQ";
K->SSD = "三星";
K->SIZE = 2000;
}
测试函数:
#include "COMPUTER.h"
#include "GAMES.h"
int main(void) {
COMPUTER T3;
cout << T3.descounpriter() << endl;
GAMES T1;
T1.kis(&T3);//用的是类的指针,所以传的是这个类的地址
cout << "升级后:\n" << T3.descounpriter() << endl;
system("pause");
return 0;
}
运行结果:
友元类:
把一个类作为友元;
#pragma once
#include <iostream>
#include <sstream>
#include "GAMES.h"
using namespace std;
class COMPUTER {
public:
COMPUTER() {
cout << "初始状态...\n";
this->cpu = "i7";
this->SSD = "希捷";
this->SIZE = 500;
}
string descounpriter() {
stringstream ret;
ret << "CPU:" << cpu << "\n硬盘型号:" << SSD << "\n硬盘大小:" << SIZE << "G";
return ret.str();
}
friend class GAMES;
private:
string cpu;
string SSD;
int SIZE;
};
注意:这里不可以直接用COMPUTER这个类的头文件,而是直接用class COMPUTER;
这样声明,用文件会报错;函数的实现也要在cpp文件中实现,不然也会报错;
IDE:(VS2019)会有这样的情况;
#pragma once
class COMPUTER;
class GAMES{
public:
//升级功能
void kis(COMPUTER* K);
//杀毒功能
void kill(COMPUTER& K);
//系统清理功能
void close(COMPUTER& K);
};
#include "GAMES.h"
#include "COMPUTER.h"
void GAMES::kis(COMPUTER* K) {
K->cpu = "i7-7700HQ";
K->SSD = "三星";
K->SIZE = 2000;
}
void GAMES::close(COMPUTER& K) {
cout << "系统清理中..." << endl;
K.cpu = "NULL";
K.SSD = "NULL";
K.SIZE = NULL;
}
void GAMES::kill(COMPUTER& K) {
cout << "杀毒中..." << endl;
K.cpu = "0";
K.SSD = "0";
K.SIZE = 0;
}
测试函数:
#include "COMPUTER.h"
#include "GAMES.h"
int main(void) {
COMPUTER T1;
cout << T1.descounpriter() << endl;
GAMES T2;
T2.kis(&T1);
cout << "升级后:\n" << T1.descounpriter() << endl;
T2.kill(T1);
cout << "杀毒后:\n" << T1.descounpriter() << endl;
T2.close(T1);
cout << "SYSTEM清理后:\n" << T1.descounpriter() << endl;
system("pause");
return 0;
}
运行结果: