C++特殊成員詳解
const成員
1.const數(shù)據(jù)成員:const類型變量不可修改(只讀模式),必須采用初始化參數(shù)列表的方式初始化。
2.const成員函數(shù):const寫在小括號(hào)的后面,常成員函數(shù)不能修改數(shù)據(jù)成員(只讀),常成員函數(shù)與普通函數(shù)同時(shí)存在時(shí),函數(shù)名相同時(shí),普通對(duì)象有限調(diào)用普通函數(shù),普通對(duì)象可以調(diào)用常成員函數(shù)。
3.const對(duì)象:const修飾的對(duì)象,只能調(diào)用常成員函數(shù)。
#include<iostream>
#include<string>
using namespace std;
class king{
public:
king(int data) :num(data)//初始化參數(shù)列表初始化數(shù)據(jù)
{
cout << num << endl;
}
void print()const//常成員函數(shù)
{
cout << "hello!" << endl;
}
void printdata()//普通函數(shù)
{
str = "lijue";
cout << str << endl;
}
protected:
string str;
const int num;//常數(shù)據(jù)成員
};
int main(){
king prince(18);
prince.print();//普通對(duì)象調(diào)用常成員函數(shù)
const king boy(12);
boy.print();//常對(duì)象調(diào)用常成員函數(shù)
while (1);
return 0;
}
static成員
#static屬于類,是所有對(duì)象共有的,可以當(dāng)對(duì)象調(diào)用
1.static數(shù)據(jù)成員:必須在類外初始化,不需要static修飾,需要類名限定(::),不允許初始化參數(shù)列表的方式初始化。
2.static成員函數(shù):static寫在函數(shù)的前面,調(diào)用非靜態(tài)數(shù)據(jù)成員必須要指定對(duì)象。
3.static對(duì)象:釋放是最后釋放的。
#include<iostream>using namespace std;class desk{public:static void print(desk&chair){chair.data1 = 12;cout << chair.data1//非靜態(tài)數(shù)據(jù)成員調(diào)用靜態(tài)成員函數(shù)必須指定對(duì)象<< "\t" << data //靜態(tài)數(shù)據(jù)成員的調(diào)用可以不需要指明對(duì)象<< endl; }protected:static int data;//靜態(tài)數(shù)據(jù)成員int data1;//非靜態(tài)數(shù)據(jù)成員};int desk::data = 50;int main(){desk chair;chair.print(chair);while (1);return 0;}
友元類
#什么是友元:用friend描述的關(guān)系,友元只是提供一個(gè)場(chǎng)所,賦予對(duì)象打破權(quán)限的限定
1.友元函數(shù):分為普通友元函數(shù)和以另一個(gè)類的成員函數(shù)充當(dāng)友元函數(shù)。
//普通友元函數(shù),可以打破權(quán)限的限制
#include<iostream>
using namespace std;
void print();
class myfriend{
public:
protected:
int data = 150;
friend void print(myfriend&k){
cout << k.data << endl;
}
};
int main(){
myfriend k;
print(k);
while (1);
return 0;
}
//以另一個(gè)類的成員函數(shù)充當(dāng)友元函數(shù)
#include<iostream>
using namespace std;
class myfriend;
class I{
public:
void print();
protected:
};
class myfriend{
public:
friend void I::print();
protected:
int data = 150;
};
void I::print()
{
myfriend k;
cout << k.data << endl;
}
int main(){
I K;
K.print();
while (1);
return 0;
}
2.友元類
//友元類
#include<iostream>
using namespace std;
class A;
class B{
public:
friend class A;
protected:
int data = 123;
};
class A{
public:
void printData()
{
B l;
cout << l.data << endl;
}
protected:
};
int main()
{
A l;
l.printData();
while (1);
return 0;
}
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
C語言實(shí)現(xiàn)簡(jiǎn)單航班管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)簡(jiǎn)單航班管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12
C語言運(yùn)算符及其優(yōu)先級(jí)匯總表口訣
由于C語言的運(yùn)算符優(yōu)先級(jí)與C++的不完全一樣(主要是增加了幾個(gè)運(yùn)算符),所以這個(gè)口訣不能完全實(shí)用于C++.但是應(yīng)該能夠兼容,大家可以比較一下他們的區(qū)別應(yīng)該就能夠很快掌握C++的優(yōu)先級(jí)的2013-07-07
C語言數(shù)據(jù)結(jié)構(gòu)進(jìn)階之棧和隊(duì)列的實(shí)現(xiàn)
棧和隊(duì)列,嚴(yán)格意義上來說,也屬于線性表,因?yàn)樗鼈円捕加糜诖鎯?chǔ)邏輯關(guān)系為 "一對(duì)一" 的數(shù)據(jù),但由于它們比較特殊,因此將其單獨(dú)作為一章,做重點(diǎn)講解2021-11-11

