不可以修改成員的屬性,在成員函數(shù)后面加const(既不可以修改指向,也不可以修改值),本質(zhì)上是修飾的this指針,而this指針本質(zhì)上是指針常量(哪個對象調(diào)用該成員函數(shù),這個this指針就指向誰,不可以修改指針指向)。
常對象不可以修改成員屬性,且常對象只能調(diào)用常函數(shù)(聯(lián)想到靜態(tài)成員函數(shù)只能訪問靜態(tài)成員屬性)
1、方法:在類中聲明類外的全局函數(shù)是類的好朋友,在聲明的前面加上friend。這樣全局函數(shù)就可以訪問類中私有成員屬性
1、方法:在類中聲明另一個類為友元,前面加friend修飾。
2、注意:類中的成員函數(shù)可以在類外實現(xiàn),但是要在類中聲明,在類外加類的作用域限制:building::building()
#includeusing namespace std;
#includeclass building
{friend class goodgay;
public:
string m_sitting_room;
building();
private:
string m_bed_room;
};
class goodgay
{public:
void visit(building& b)
{cout<< "好基友正在訪問"<< b.m_sitting_room<< endl;
cout<< "好基友正在訪問"<< b.m_bed_room<< endl;
}
};
building::building()
{m_sitting_room = "客廳";
m_bed_room = "臥室";
}
void test()
{building b1;
goodgay g;
g.visit(b1);
}
int main()
{test();
system("pause");
return 0;
}
在一個類中調(diào)用另一個類的成員方法
(1)類引用作為形參
class goodgay
{public:
void visit(building& b)
{cout<< "好基友正在訪問"<< b.m_sitting_room<< endl;
cout<< "好基友正在訪問"<< b.m_bed_room<< endl;
}
};
(2)類作為另一個類的成員
#includeusing namespace std;
#includeclass goodgay;
class building
{friend goodgay;
public:
string m_sitting_room;
building();
private:
string m_bed_room;
};
class goodgay
{public:
building * b;
goodgay();//構(gòu)造函數(shù)初始化成員屬性
void visit();
};
building::building()
{m_sitting_room = "客廳";
m_bed_room = "臥室";
}
goodgay::goodgay()
{b = new(building);
}
void goodgay::visit()
{cout<< "好基友正在訪問"<< b->m_sitting_room<< endl;
cout<< "好基友正在訪問"<< b->m_bed_room<< endl;
}
void test()
{//building b;在創(chuàng)建g時就會調(diào)用構(gòu)造函數(shù)創(chuàng)建b
goodgay g;
g.visit();
}
int main()
{test();
system("pause");
return 0;
}
三)、成員函數(shù)做友元對已有的運算符重新定義,賦予其它的功能。(對于內(nèi)置的數(shù)據(jù)類型,編譯器知道如何進(jìn)行運算。)
1、加法運算符重載
operator +(對于加法重載的統(tǒng)一名稱)
(1)通過成員函數(shù)進(jìn)行加法
(2)通過全局函數(shù)進(jìn)行加法
person operator+(person& b)
{person temp;
temp.m_a = this->m_a + b.m_a;
temp.m_b = this->m_b + b.m_b;
return temp;
}
本質(zhì)調(diào)用:person p3=p1.operator+(p2);
#includeusing namespace std;
//加號運算符重載
//1、成員函數(shù)實現(xiàn)加法運算符重載
//2、全局函數(shù)實現(xiàn)加法運算符重載
class person
{public:
int m_a;
int m_b;
};
//2
person operator+(person& a, person& b)
{person temp;
temp.m_a = a.m_a + b.m_a;
temp.m_b = a.m_b + b.m_b;
return temp;
}
void test()
{person p1;
p1.m_a = 20;
p1.m_b = 30;
person p2;
p2.m_a = 10;
p2.m_b = 20;
person p3 = p1 + p2;
cout<< p3.m_a<< endl;
cout<< p3.m_b<< endl;
}
int main()
{test();
system("pause");
return 0;
}
2、運算符重載也可以發(fā)生函數(shù)重載
out屬于ostream類型,ostream對象有且只能有一個,所以重載時需要用引用;
由于cout只能在運算符左側(cè),所以只能用全局函數(shù)進(jìn)行運算符重載;
想要鏈?zhǔn)捷敵?,則需要返回ostream的引用。
左移運算符配合友元可以輸出自定義運算符。
回憶自定義遞增
前置遞增
#includeusing namespace std;
//前置遞增重載
//后置遞增重載
class number
{friend ostream& operator<<(ostream& cout, number& n1);
private:
int m_a;
public:
number(int a);
number& operator++()
{++m_a;
return *this;
}
};
number::number(int a)
{m_a = a;
}
ostream& operator<<(ostream& cout, number& n1)
{cout<< n1.m_a<< endl;
return cout;
}
int main()
{number n1(10);
cout<< ++(++n1)<< endl;
system("pause");
return 0;
}
前置遞增運算符重載,返回的是引用。為了一直對一個數(shù)據(jù)進(jìn)行遞增,如果返回的是值,那么返回的不是本體,而是他的復(fù)制。
后置遞增
#includeusing namespace std;
//前置遞增重載
//后置遞增重載
class number
{friend ostream& operator<<(ostream& cout, number& n1);
int m_a;
public:
number(int a)
{m_a = a;
}
number& operator++()
{++m_a;
return *this;
}
number& operator++(int)//int為占位符,將后置和前置遞增區(qū)分開來
{number temp = *this;
m_a++;
return temp;
}
};
ostream& operator<<(ostream& cout, number& n1)
{cout<< n1.m_a<< endl;
return cout;
}
int main()
{number n1(10);
cout<< n1++<< endl;
cout<< n1<< endl;
system("pause");
return 0;
}
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧
當(dāng)前文章:C++學(xué)習(xí)第十九天:類與對象:const修飾成員函數(shù),友元,運算符重載-創(chuàng)新互聯(lián)
網(wǎng)頁地址:http://sd-ha.com/article18/dedpgp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計、動態(tài)網(wǎng)站、網(wǎng)站策劃、企業(yè)網(wǎng)站制作、自適應(yīng)網(wǎng)站、移動網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容