版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
第C++深入探究類與對(duì)象之友元與運(yùn)算符重載目錄友元1全局函數(shù)做友元2類做友元3成員函數(shù)做友元運(yùn)算符重載1加號(hào)運(yùn)算符重載2左移運(yùn)算符重載3遞增運(yùn)算符重載4賦值運(yùn)算符重載5關(guān)系運(yùn)算符重載6函數(shù)調(diào)用運(yùn)算符重載
友元
生活中你的家有客廳(Public),有你的臥室(Private),客廳所有來的客人都可以進(jìn)去,但是你的臥室是私有的,也就是說只有你能進(jìn)去,但是呢,你也可以允許你的好閨蜜好基友進(jìn)去。
在程序里,有些私有屬性也想讓類外特殊的一些函數(shù)或者類進(jìn)行訪問,就需要用到友元的技術(shù)。
友元的目的就是讓一個(gè)函數(shù)或者類訪問另一個(gè)類中私有成員。
友元的關(guān)鍵字為:friend
友元的三種實(shí)現(xiàn):
全局函數(shù)做友元類做友元成員函數(shù)做友元
1全局函數(shù)做友元
classBuilding
//告訴編譯器goodGay全局函數(shù)是Building類的好朋友,可以訪問類中的私有內(nèi)容
friendvoidgoodGay(Building*building);
public:
Building()
this-m_SittingRoom="客廳";
this-m_BedRoom="臥室";
public:
stringm_SittingRoom;//客廳
private:
stringm_BedRoom;//臥室
voidgoodGay(Building*building)
cout"好基友正在訪問:"building-m_SittingRoomendl;
cout"好基友正在訪問:"building-m_BedRoomendl;
voidtest01()
Buildingb;
goodGay(
intmain(){
test01();
system("pause");
return0;
}
2類做友元
classBuilding;
classgoodGay
public:
goodGay();
voidvisit();
private:
Building*building;
classBuilding
//告訴編譯器goodGay類是Building類的好朋友,可以訪問到Building類中私有內(nèi)容
friendclassgoodGay;
public:
Building();
public:
stringm_SittingRoom;//客廳
private:
stringm_BedRoom;//臥室
Building::Building()
this-m_SittingRoom="客廳";
this-m_BedRoom="臥室";
goodGay::goodGay()
building=newBuilding;
voidgoodGay::visit()
cout"好基友正在訪問"building-m_SittingRoomendl;
cout"好基友正在訪問"building-m_BedRoomendl;
voidtest01()
goodGaygg;
gg.visit();
intmain(){
test01();
system("pause");
return0;
}
3成員函數(shù)做友元
classBuilding;
classgoodGay
public:
goodGay();
voidvisit();//只讓visit函數(shù)作為Building的好朋友,可以發(fā)訪問Building中私有內(nèi)容
voidvisit2();
private:
Building*building;
classBuilding
//告訴編譯器goodGay類中的visit成員函數(shù)是Building好朋友,可以訪問私有內(nèi)容
friendvoidgoodGay::visit();
public:
Building();
public:
stringm_SittingRoom;//客廳
private:
stringm_BedRoom;//臥室
Building::Building()
this-m_SittingRoom="客廳";
this-m_BedRoom="臥室";
goodGay::goodGay()
building=newBuilding;
voidgoodGay::visit()
cout"好基友正在訪問"building-m_SittingRoomendl;
cout"好基友正在訪問"building-m_BedRoomendl;
voidgoodGay::visit2()
cout"好基友正在訪問"building-m_SittingRoomendl;
//cout"好基友正在訪問"building-m_BedRoomendl;
voidtest01()
goodGaygg;
gg.visit();
intmain(){
test01();
system("pause");
return0;
}
運(yùn)算符重載
運(yùn)算符重載概念:對(duì)已有的運(yùn)算符重新進(jìn)行定義,賦予其另一種功能,以適應(yīng)不同的數(shù)據(jù)類型
1加號(hào)運(yùn)算符重載
作用:實(shí)現(xiàn)兩個(gè)自定義數(shù)據(jù)類型相加的運(yùn)算
classPerson{
public:
Person(){};
Person(inta,intb)
this-m_A=a;
this-m_B=b;
//成員函數(shù)實(shí)現(xiàn)+號(hào)運(yùn)算符重載
Personoperator+(constPersonp){
Persontemp;
temp.m_A=this-m_A+p.m_A;
temp.m_B=this-m_B+p.m_B;
returntemp;
public:
intm_A;
intm_B;
//全局函數(shù)實(shí)現(xiàn)+號(hào)運(yùn)算符重載
//Personoperator+(constPersonp1,constPersonp2){
//Persontemp(0,0);
//temp.m_A=p1.m_A+p2.m_A;
//temp.m_B=p1.m_B+p2.m_B;
//returntemp;
//運(yùn)算符重載可以發(fā)生函數(shù)重載
Personoperator+(constPersonp2,intval)
Persontemp;
temp.m_A=p2.m_A+val;
temp.m_B=p2.m_B+val;
returntemp;
voidtest(){
Personp1(10,10);
Personp2(20,20);
//成員函數(shù)方式
Personp3=p2+p1;//相當(dāng)于p2.operaor+(p1)
cout"mA:"p3.m_A"mB:"p3.m_Bendl;
Personp4=p3+10;//相當(dāng)于operator+(p3,10)
cout"mA:"p4.m_A"mB:"p4.m_Bendl;
intmain(){
test();
system("pause");
return0;
}
總結(jié)1:對(duì)于內(nèi)置的數(shù)據(jù)類型的表達(dá)式的的運(yùn)算符是不可能改變的
總結(jié)2:不要濫用運(yùn)算符重載
2左移運(yùn)算符重載
作用:可以輸出自定義數(shù)據(jù)類型
classPerson{
friendostreamoperator(ostreamout,Personp);
public:
Person(inta,intb)
this-m_A=a;
this-m_B=b;
//成員函數(shù)實(shí)現(xiàn)不了pcout不是我們想要的效果
//voidoperator(Personp){
private:
intm_A;
intm_B;
//全局函數(shù)實(shí)現(xiàn)左移重載
//ostream對(duì)象只能有一個(gè)
ostreamoperator(ostreamout,Personp){
out"a:"p.m_A"b:"p.m_B;
returnout;
voidtest(){
Personp1(10,20);
coutp1"helloworld"endl;//鏈?zhǔn)骄幊?/p>
intmain(){
test();
system("pause");
return0;
}
總結(jié):重載左移運(yùn)算符配合友元可以實(shí)現(xiàn)輸出自定義數(shù)據(jù)類型
3遞增運(yùn)算符重載
作用:通過重載遞增運(yùn)算符,實(shí)現(xiàn)自己的整型數(shù)據(jù)
classMyInteger{
friendostreamoperator(ostreamout,MyIntegermyint);
public:
MyInteger(){
m_Num=0;
//前置++
MyIntegeroperator++(){
//先++
m_Num++;
//再返回
return*this;
//后置++
MyIntegeroperator++(int){
//先返回
MyIntegertemp=*this;//記錄當(dāng)前本身的值,然后讓本身的值加1,但是返回的是以前的值,達(dá)到先返回后++;
m_Num++;
returntemp;
private:
intm_Num;
ostreamoperator(ostreamout,MyIntegermyint){
outmyint.m_Num;
returnout;
//前置++先++再返回
voidtest01(){
MyIntegermyInt;
cout++myIntendl;
coutmyIntendl;
//后置++先返回再++
voidtest02(){
MyIntegermyInt;
coutmyInt++endl;
coutmyIntendl;
intmain(){
test01();
//test02();
system("pause");
return0;
}
總結(jié):前置遞增返回引用,后置遞增返回值
4賦值運(yùn)算符重載
c++編譯器至少給一個(gè)類添加4個(gè)函數(shù)
默認(rèn)構(gòu)造函數(shù)(無參,函數(shù)體為空)默認(rèn)析構(gòu)函數(shù)(無參,函數(shù)體為空)默認(rèn)拷貝構(gòu)造函數(shù),對(duì)屬性進(jìn)行值拷貝賦值運(yùn)算符operator=,對(duì)屬性進(jìn)行值拷貝
如果類中有屬性指向堆區(qū),做賦值操作時(shí)也會(huì)出現(xiàn)深淺拷貝問題
示例:
classPerson
public:
Person(intage)
//將年齡數(shù)據(jù)開辟到堆區(qū)
m_Age=newint(age);
//重載賦值運(yùn)算符
Personoperator=(Personp)
if(m_Age!=NULL)
deletem_Age;
m_Age=NULL;
//編譯器提供的代碼是淺拷貝
//m_Age=p.m_Age;
//提供深拷貝解決淺拷貝的問題
m_Age=newint(*p.m_Age);
//返回自身
return*this;
~Person()
if(m_Age!=NULL)
deletem_Age;
m_Age=NULL;
//年齡的指針
int*m_Age;
voidtest01()
Personp1(18);
Personp2(20);
Personp3(30);
p3=p2=p1;//賦值操作
cout"p1的年齡為:"*p1.m_Ageendl;
cout"p2的年齡為:"*p2.m_Ageendl;
cout"p3的年齡為:"*p3.m_Ageendl;
intmain(){
test01();
//inta=10;
//intb=20;
//intc=30;
//c=b=a;
//cout"a="aendl;
//cout"b="bendl;
//cout"c="cendl;
system("pause");
return0;
}
5關(guān)系運(yùn)算符重載
**作用:**重載關(guān)系運(yùn)算符,可以讓兩個(gè)自定義類型對(duì)象進(jìn)行對(duì)比操作
示例:
classPerson
public:
Person(stringname,intage)
this-m_Name=name;
this-m_Age=age;
booloperator==(Personp)
if(this-m_Name==p.m_Namethis-m_Age==p.m_Age)
returntrue;
else
returnfalse;
booloperator!=(Personp)
if(this-m_Name==p.m_Namethis-m_Age==p.m_Age)
returnfalse;
else
returntrue;
stringm_Name;
intm_Age;
voidtest01()
//inta=0;
//intb=0;
Persona("孫悟空",18);
Personb("孫悟空",18);
if(a==b)
cout"a和b相等"end
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2026貴州中醫(yī)藥大學(xué)博士后招聘備考題庫及1套完整答案詳解
- 2026貴州醫(yī)科大學(xué)附屬白云醫(yī)院養(yǎng)老護(hù)理員招聘8人備考題庫及答案詳解(奪冠系列)
- 寶寶皮膚護(hù)理與預(yù)防濕疹
- 2025 小學(xué)一年級(jí)道德與法治上冊(cè)獨(dú)自在家不害怕課件
- 2026年工地安全管理標(biāo)準(zhǔn)化建設(shè)
- 遠(yuǎn)程會(huì)診護(hù)理的經(jīng)濟(jì)效益
- 職業(yè)醫(yī)學(xué)與工程學(xué)的聯(lián)合防護(hù)模式
- 臨潭事業(yè)編招聘2022年考試模擬試題及答案解析42
- 職業(yè)健康素養(yǎng)對(duì)醫(yī)療員工組織承諾的預(yù)測作用
- 職業(yè)健康檔案電子化傳輸過程中的加密技術(shù)應(yīng)用
- 2026云南昭通市搬遷安置局招聘公益性崗位人員3人備考題庫及答案詳解(考點(diǎn)梳理)
- 2026中國電信四川公用信息產(chǎn)業(yè)有限責(zé)任公司社會(huì)成熟人才招聘備考題庫及一套答案詳解
- 2025-2030心理健康行業(yè)市場發(fā)展分析及趨勢(shì)前景與投資戰(zhàn)略研究報(bào)告
- 技術(shù)副總年終總結(jié)
- 《馬年馬上有錢》少兒美術(shù)教育繪畫課件創(chuàng)意教程教案
- 天津市專升本高等數(shù)學(xué)歷年真題(2016-2025)
- 2025年化工原理考試題及答案
- 湖南省益陽市2024-2025學(xué)年高二上學(xué)期語文1月期末考試試卷(含答案)
- 幕墻工程售后質(zhì)量保障服務(wù)方案
- 鋁合金鑄造項(xiàng)目可行性研究報(bào)告
- 2024年西藏自治區(qū)事業(yè)單位《職業(yè)能力傾向測驗(yàn)(D類)》考試真題及答案
評(píng)論
0/150
提交評(píng)論