C++實(shí)例代碼詳解友元函數(shù)
友元函數(shù) 可以直接操作類的私有數(shù)據(jù)。
friend關(guān)鍵字在聲明處修飾函數(shù) 那么該函數(shù)就是類的友元。
友元 不是類的一部分。
友元概述
c++允許 友元 訪問 私有數(shù)據(jù)。
友元的語法:
friend關(guān)鍵字只出現(xiàn)在聲明處 其他類、類成員函數(shù)、全局函數(shù)都可聲明為友元 友元函數(shù)不是類的成員,不帶this指針 友元函數(shù)可訪問對象任意成員屬性,包括私有屬性。
普通全局函數(shù)作為類的友元
//房間類 class Room { //將goodGayVisit作為類的友元函數(shù) //goodGayVisit 訪問 類中所有數(shù)據(jù) 但是 它不是類的成員 friend void goodGayVisit(Room &room); private: string bedRoom;//臥室 public: string sittingRoom;//客廳 public: Room() { this->bedRoom = "臥室"; this->sittingRoom="客廳"; } }; // 普通全局函數(shù) 作為 類的友元 //好基友 訪問 我的房間 void goodGayVisit(Room &room) { cout<<"好基友訪問了你的"<<room.sittingRoom<<endl; cout<<"好基友訪問了你的"<<room.bedRoom<<endl;//ok } void test01() { Room myRoom; goodGayVisit(myRoom); }
運(yùn)行結(jié)果:
類的某個(gè)成員函數(shù)作為另一個(gè)類的友元
問題1:
問題2:
成員函數(shù)內(nèi) 不能訪問 Room的私有數(shù)據(jù)
最終代碼:
#include <iostream> using namespace std; class Room;//Room向前聲明 class GoodGay { public: void visit1(Room &room);//此處的Room 被上方 class Room void visit2(Room &room); }; class Room { //如果想方 visit2作為Room類的友元 那么Visit2就可以訪問 Room的私有數(shù)據(jù) //一定要記得 加類作用域 friend void GoodGay::visit2(Room &room); private: string bedRoom;//臥室 public: string sittingRoom;//客廳 public: Room() { this->bedRoom = "臥室"; this->sittingRoom="客廳"; } }; void GoodGay::visit1(Room &room) { cout<<"好基友visit1訪問了你的"<<room.sittingRoom<<endl; //cout<<"好基友visit1訪問了你的"<<room.bedRoom<<endl;//不能訪問 Room私有數(shù)據(jù) } void GoodGay::visit2(Room &room) { cout<<"好基友visit2訪問了你的"<<room.sittingRoom<<endl; cout<<"好基友visit2訪問了你的"<<room.bedRoom<<endl; } int main(int argc, char *argv[]) { Room myRoom; GoodGay goodGay; goodGay.visit1(myRoom);//只能訪問客廳 goodGay.visit2(myRoom);//客廳 臥室 都可以訪問 return 0; }
運(yùn)行結(jié)果:
一個(gè)類整體作為另一個(gè)類的友元
一個(gè)類的所有成員函數(shù) 訪問 另一個(gè)類的私有數(shù)據(jù)
#include <iostream> using namespace std; class Room;//Room向前聲明 class GoodGay { public: void visit1(Room &room);//此處的Room 被上方 class Room void visit2(Room &room); }; class Room { //將GoodGay作為Room的友元 //GoodGay 所有成員函數(shù) 都可以訪問 Room私有數(shù)據(jù) friend class GoodGay; private: string bedRoom;//臥室 public: string sittingRoom;//客廳 public: Room() { this->bedRoom = "臥室"; this->sittingRoom="客廳"; } }; void GoodGay::visit1(Room &room) { cout<<"好基友visit1訪問了你的"<<room.sittingRoom<<endl; cout<<"好基友visit1訪問了你的"<<room.bedRoom<<endl; } void GoodGay::visit2(Room &room) { cout<<"好基友visit2訪問了你的"<<room.sittingRoom<<endl; cout<<"好基友visit2訪問了你的"<<room.bedRoom<<endl; } int main(int argc, char *argv[]) { Room myRoom; GoodGay goodGay; goodGay.visit1(myRoom); goodGay.visit2(myRoom); return 0; }
運(yùn)行結(jié)果:
友元的注意事項(xiàng)
1.友元關(guān)系不能被繼承。
2.友元關(guān)系是單向的,類A是類B的朋友,但類B不一定是類A 的朋友。
3.友元關(guān)系不具有傳遞性。類B是類A的朋友,類C是類B的朋友,但類C不一定是類A的朋 友
封裝電視機(jī) 和遙控器的類
1、封裝電視機(jī)的類
class TV { enum{ On,Off }; //電視狀態(tài) enum{ minVol,maxVol = 100 }; //音量從0到100 enum{ minChannel = 1,maxChannel = 255 }; //頻道從1到255 private: int mState; //電視狀態(tài),開機(jī),還是關(guān)機(jī) int mVolume; //電視機(jī)音量 int mChannel; //電視頻道 public: TV() { this->mState = Off;//默認(rèn)關(guān)機(jī) this->mVolume = minVol; this->mChannel = minChannel; } void onOrOff(void) { this->mState = (this->mState == On ? Off:On); } //加大音量 void volumeUp(void) { if(this->mVolume >= maxVol) return; this->mVolume++; } //減小音量 void volumeDown(void) { if(this->mVolume <= minVol) return; this->mVolume--; } //增加頻道 void channelUp(void) { if(this->mChannel >= maxChannel) return; this->mChannel++; } //減小頻道 void channelDown(void) { if(this->mChannel <= minChannel) return; this->mChannel--; } //顯示電視機(jī)的狀態(tài) void showTVState(void) { cout<<"電視機(jī)的狀態(tài)為:"<< (this->mState==On ? "開機(jī)":"關(guān)機(jī)") <<endl; cout<<"電視機(jī)的音量:"<<this->mVolume<<endl; cout<<"電視機(jī)的頻道:"<<this->mChannel<<endl; } }; void test01() { TV tv; tv.onOrOff();//開機(jī) tv.volumeUp();//調(diào)四次音量 tv.volumeUp(); tv.volumeUp(); tv.volumeUp(); tv.channelUp();//調(diào)三次頻道 tv.channelUp(); tv.showTVState(); }
運(yùn)行結(jié)果:
2、設(shè)置遙控器的類2-1
class TV { friend class Remote; //默認(rèn)為私有 enum{ On,Off }; //電視狀態(tài) enum{ minVol,maxVol = 100 }; //音量從0到100 enum{ minChannel = 1,maxChannel = 255 }; //頻道從1到255 private: int mState; //電視狀態(tài),開機(jī),還是關(guān)機(jī) int mVolume; //電視機(jī)音量 int mChannel; //電視頻道 public: TV() { this->mState = Off;//默認(rèn)關(guān)機(jī) this->mVolume = minVol; this->mChannel = minChannel; } void onOrOff(void) { this->mState = (this->mState == On ? Off:On); } //加大音量 void volumeUp(void) { if(this->mVolume >= maxVol) return; this->mVolume++; } //減小音量 void volumeDown(void) { if(this->mVolume <= minVol) return; this->mVolume--; } //增加頻道 void channelUp(void) { if(this->mChannel >= maxChannel) return; this->mChannel++; } //減小頻道 void channelDown(void) { if(this->mChannel <= minChannel) return; this->mChannel--; } //顯示電視機(jī)的狀態(tài) void showTVState(void) { cout<<"電視機(jī)的狀態(tài)為:"<< (this->mState==On ? "開機(jī)":"關(guān)機(jī)") <<endl; cout<<"電視機(jī)的音量:"<<this->mVolume<<endl; cout<<"電視機(jī)的頻道:"<<this->mChannel<<endl; } }; //遙控器類 class Remote { private: TV *pTv; public: Remote(TV *pTv) { this->pTv = pTv; } //音量的加減 void volumeUp(void) { //調(diào)節(jié)的電視機(jī)的音量 this->pTv->volumeUp(); } void volumeDown(void) { this->pTv->volumeDown(); } //頻道的加減 void channelUp(void) { this->pTv->channelUp(); } void channelDown(void) { this->pTv->channelDown(); } //電視開關(guān) void onOrOff(void) { this->pTv->onOrOff(); } //遙控器設(shè)置頻道設(shè)置 void setChannel(int num) { //判斷 頻道 是否有效 if(num >= TV::minChannel && num<= TV::maxChannel ) { this->pTv->mChannel = num; } } void showTVState(void) { this->pTv->showTVState(); } }; void test02() { TV tv; Remote remote(&tv); remote.onOrOff(); remote.volumeUp(); remote.volumeUp(); remote.volumeUp(); remote.volumeUp(); remote.channelUp(); remote.channelUp(); remote.showTVState(); remote.setChannel(75); remote.showTVState(); }
運(yùn)行結(jié)果:
到此這篇關(guān)于C++實(shí)例代碼詳解友元函數(shù)的文章就介紹到這了,更多相關(guān)C++友元函數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言實(shí)現(xiàn)個(gè)人財(cái)務(wù)管理軟件
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)個(gè)人財(cái)務(wù)管理軟件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05C語言判斷一個(gè)數(shù)是否是2的冪次方或4的冪次方
本文中我們來看一下如何用C語言判斷一個(gè)數(shù)是否是2的冪次方或4的冪次方的方法,并且判斷出來是多少次方,需要的朋友可以參考下2016-06-06Qt地圖自適應(yīng)拉伸的實(shí)現(xiàn)示例
最近需要寫一個(gè)程序,要是讓qt到程序自適應(yīng),本文主要介紹了Qt地圖自適應(yīng)拉伸的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12c++ 如何在libuv中實(shí)現(xiàn)tcp服務(wù)器
這篇文章主要介紹了c++ 如何在libuv中實(shí)現(xiàn)tcp服務(wù)器,幫助大家更好的理解和使用libuv,感興趣的朋友可以了解下2021-02-02Qt自定義Widget實(shí)現(xiàn)互斥效果詳解
在使用Qt時(shí),可能會遇到這種問題:多個(gè)控件互斥,類似于QRadiButton控件,但又不是單純的QRadioButton控件,互斥的可能是一個(gè)窗口,也可能是幾個(gè)按鈕,等等多種情況。本文將介紹利用Qt自定義Widget實(shí)現(xiàn)的互斥效果,需要的可以參考一下2022-01-01