QT学习C++(11)

友元

类的主要特点之一是数据隐藏,即类的私有成员无法在外部(作用域之外)访问。

友元函数是一种特殊的函数,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

注:仅用于学习总结

QT学习C++(10)_爱吃糖葫芦的大熊的博客-CSDN博客

QT学习C++(12)_爱吃糖葫芦的大熊的博客-CSDN博客

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值