友元
类的主要特点之一是数据隐藏,即类的私有成员无法在外部(作用域之外)访问。
友元函数是一种特殊的函数,C++允许这个特权函数访问私有成员。
友元语法
friend关键字只出现在声明处,其他类,类成员函数、全局函数都可声明为友元,友元函数不是类的成员,不带this指针。友元函数可访问对象任意成员属性(包括私有属性)。
全局普通函数作为类的友元
#include <iostream>
#include <string.h>>
using namespace std;
class Room{
friend void visit(Room &room);
private:
string room1;
public:
string room2;
public:
Room(){
room1 = "bedroom";
room2 = "sittingroom";
}
};
void visit(Room &room){
cout << room.room1 << " and " << room.room2;
}
void visit(char* s, Room &room){
if(strcmp(s, "朋友1") == 0){
cout << s << "进入了你的 ";
visit(room);
cout << endl;
}
else if(strcmp(s, "朋友2") == 0){
cout << s << "进入了你的 " << room.room2 << endl;
}
}
int main()
{
Room myroom;
visit("朋友1", myroom);
visit("朋友2", myroom);
return 0;
}
类的成员函数作为另一类的友元
#include <iostream>
#include <string.h>
using namespace std;
class Room;
class GirlFriend{
public:
void visit(Room &room);
};
class Friend{
public:
void visit(Room &room);
};
class Room{
friend void GirlFriend::visit(Room &room);
private:
string room1;
public:
string room2;
public:
Room(){
room1 = "bedroom";
room2 = "sittingroom";
}
};
void GirlFriend::visit(Room &room){
cout << room.room1 << " and " << room.room2 << endl;
}
void Friend::visit(Room &room){
cout << room.room2 << endl;
}
int main()
{
Room myroom;
GirlFriend Lucy;
Friend Rex;
Lucy.visit(myroom);
Rex.visit(myroom);
return 0;
}
一个类整体作为另一个类的友元
一个类的所有函数可以访问另一个类的私有数据
案例
案例1:
#include <iostream>
#include <string.h>
using namespace std;
class TV{
friend class Remote;
private:
int state;
int volume;
int channel;
public:
enum{on, off};
enum{min_v = 0, max_v = 2};
enum{min_c = 0, max_c = 5};
TV(){
this->state = off; //默认关机
this->volume = 1; //初始音量1
this->channel = 3; //默认频道3
}
void TV_state(){
cout << "电视机: " << this->state << endl;
cout << "音量: " << this->volume << endl;
cout << "频道: " << this->channel << endl;
}
private:
void chanage_s(){
this->state = (this->state == on? off:on);
}
void chanage_v_up(){
if(this->volume >= max_v){
cout << "音量已最大" << endl;
return;
}
else{
this->volume ++;
}
}
void chanage_v_down(){
if(this->volume <= min_v){
cout << "音量已最小" << endl;
return;
}
else{
this->volume --;
}
}
void chanage_c_up(){
this->channel ++;
if(this->channel > max_c){
this->channel = min_c;
}
}
void chanage_c_down(){
this->channel --;
if(this->channel < min_c){
this->channel = max_c;
}
}
};
class Remote{
public:
void TV_S(TV &tv){
tv.chanage_s();
cout << "电视机状态为" << tv.state << endl;
}
void TV_V(TV &tv, char* p){
if(strcmp(p,"-") == 0){
tv.chanage_v_down();
cout << "音量状态为" << tv.volume << endl;
}
else if(strcmp(p,"+") == 0){
tv.chanage_v_up();
cout << "音量状态为" << tv.volume << endl;
}
}
void TV_C(TV &tv, char* p){
if(strcmp(p,"-") == 0){
tv.chanage_c_down();
cout << "频道状态为" << tv.channel << endl;
}
else if(strcmp(p,"+") == 0){
tv.chanage_c_up();
cout << "频道状态为" << tv.channel << endl;
}
}
};
int main()
{
TV tv;
Remote r;
tv.TV_state();
r.TV_S(tv);
//调音量
r.TV_V(tv,"+");
r.TV_V(tv,"+");
r.TV_V(tv,"+");
r.TV_V(tv,"-");
r.TV_V(tv,"-");
r.TV_V(tv,"-");
//调频道
r.TV_C(tv,"+");
r.TV_C(tv,"+");
r.TV_C(tv,"+");
r.TV_C(tv,"+");
tv.TV_state();
return 0;
}
小问题技巧:
1
2
3
注:仅用于学习总结