




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
第C++與C語言的區(qū)別你知道嗎目錄1.結(jié)構(gòu)體區(qū)別1.1.類型上不再需要struct關(guān)鍵字,直接用結(jié)構(gòu)體名即可1.2.C++結(jié)構(gòu)體中允許函數(shù)存在2.動態(tài)內(nèi)存申請C語言的動態(tài)內(nèi)存申請C++的動態(tài)申請3.內(nèi)存池4.string類型總結(jié)
1.結(jié)構(gòu)體區(qū)別
1.1.類型上不再需要struct關(guān)鍵字,直接用結(jié)構(gòu)體名即可
#includeiostream
#includestring
usingnamespacestd;
structMM
charname[20];
intage;
intmain()
structMMgirl;
MMmm;//C++中不需要struct關(guān)鍵字
return0;
1.2.C++結(jié)構(gòu)體中允許函數(shù)存在
在結(jié)構(gòu)體中聲明,在結(jié)構(gòu)體外實(shí)現(xiàn),當(dāng)然可以直接在結(jié)構(gòu)體中實(shí)現(xiàn)結(jié)構(gòu)體中函數(shù)訪問數(shù)據(jù),是可以直接訪問學(xué)會調(diào)用,和數(shù)據(jù)成員方式時一樣的對象(結(jié)構(gòu)體變量).成員對象指針-成員(*對象指針).成員C++在沒有寫構(gòu)造函數(shù)和權(quán)限限定的時候,用法和C語言的用法是一樣
#includeiostream
#includestring
usingnamespacestd;
structMM
//屬性
//數(shù)據(jù)成員
charname[20];
intage;
//行為(方法)
//成員函數(shù)
voidprint()
coutname"\t"ageendl;
voidprintData();//在結(jié)構(gòu)體中聲明,在外面實(shí)現(xiàn)
intgetAge()
returnage;
//結(jié)構(gòu)體名限定,就是告訴別人這個函數(shù)來自哪里
voidMM::printData()
coutname"\t"ageendl;
//結(jié)構(gòu)體中的變量必須要通過結(jié)構(gòu)體變量(結(jié)構(gòu)體指針)訪問
//c++結(jié)構(gòu)體中的函數(shù)訪問屬性,可以直接訪問
intmain()
structMMgirl={"小芳",28};
MMmm={"小麗",24};//C++中不需要struct關(guān)鍵字
girl.print();
(mm)-printData();
MM*p=
p-printData();
p-getAge()=84;
p-printData();
p-age=1991;
p-printData();
return0;
2.動態(tài)內(nèi)存申請
C語言的動態(tài)內(nèi)存申請
malloc不帶初始化,calloc帶初始化,realloc重新申請free釋放
C++的動態(tài)申請
new(申請)和delete(釋放)單個變量內(nèi)存申請數(shù)組的動態(tài)申請結(jié)構(gòu)體內(nèi)存申請
例子:單個變量內(nèi)存申請和數(shù)組的動態(tài)申請
#includeiostream
#includestring
usingnamespacestd;
voidtestNoeMemory()
//申請不做初始化
int*pInt=newint;
*pInt=123;
cout*pIntendl;
char*pChar=newchar;
*pChar='A';
cout*pCharendl;
//申請內(nèi)存做初始化()給單個數(shù)據(jù)做初始化
int*pNum=newint(134);
cout*pNumendl;
deletepInt;
pInt=nullptr;
deletepChar;
pChar=nullptr;
deletepNum;
pNum=nullptr;
voidtestArrayMerrmory()
//一維數(shù)組
//1、不帶初始化
//長度可以是h變量,只要值就可以
int*pInt=newint[3];//等效產(chǎn)生了intpInt[3]的數(shù)組
char*pstr=newchar[15];
strcpy_s(pstr,15,"Iloveyou");
coutpstrendl;
//帶初始化的一堆數(shù)據(jù)用{}
int*pNum=newint[3]{1,2,3};
for(inti=0;ii++)
coutpNum[i]"";
coutendl;
delete[]pNum;
char*str=newchar[20]{'A','B','\0'};
coutstrendl;
delete[]str;
str=nullptr;
str=newchar[20]{"Iloveyou"};
coutstrendl;
delete[]str;
str=nullptr;
delete[]pInt;//數(shù)組的指針不需要大小
//釋放只有兩種形式delete指針delete[]指針
//delete[][]p沒有這種寫法
pInt=nullptr;
intmain()
testNoeMemory();
return0;
例子:結(jié)構(gòu)體內(nèi)存申請
#includeiostream
#includestring
usingnamespacestd;
voidtestNoeMemory()
//申請不做初始化
int*pInt=newint;
*pInt=123;
cout*pIntendl;
char*pChar=newchar;
*pChar='A';
cout*pCharendl;
//申請內(nèi)存做初始化()給單個數(shù)據(jù)做初始化
int*pNum=newint(134);
cout*pNumendl;
deletepInt;
pInt=nullptr;
deletepChar;
pChar=nullptr;
deletepNum;
pNum=nullptr;
voidtestArrayMerrmory()
//一維數(shù)組
//1、不帶初始化
//長度可以是h變量,只要值就可以
int*pInt=newint[3];//等效產(chǎn)生了intpInt[3]的數(shù)組
char*pstr=newchar[15];
strcpy_s(pstr,15,"Iloveyou");
coutpstrendl;
//帶初始化的一堆數(shù)據(jù)用{}
int*pNum=newint[3]{1,2,3};
for(inti=0;ii++)
coutpNum[i]"";
coutendl;
delete[]pNum;
char*str=newchar[20]{'A','B','\0'};
coutstrendl;
delete[]str;
str=nullptr;
str=newchar[20];
coutstrendl;
delete[]str;
str=nullptr;
delete[]pInt;//數(shù)組的指針不需要大小
//釋放只有兩種形式delete指針delete[]指針
//delete[][]p沒有這種寫法
pInt=nullptr;
structMM
char*name;
intage;
voidprintMM()
coutname"\t"ageendl;
voidtestStructMerrory()
//new一個對象
int*p=newint(123);
//結(jié)構(gòu)體只能用大括號
MM*pMM=newMM;
//結(jié)構(gòu)體中指針,要做二次申請,才能strcpy,或者賦值
pMM-name=newchar[20];
strcpy_s(pMM-name,20,"李四");
pMM-age=188;
pMM-printMM();
//申請的順序和釋放的順序是相反的
delete[]pMM-name;
deletepMM;
intmain()
//testNoeMemory();
testStructMerrory();
return0;
3.內(nèi)存池
允許大家申請一段內(nèi)存,共給程序使用,綜合管理內(nèi)存
4.string類型
只需要知道有這種用法即可,不需要大家深究為什么,因?yàn)閟tring本身是一個類,需要講完類的大部分知識,才能追究
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 買賣農(nóng)村房子合同范例
- 兒童模特合同樣本
- 設(shè)計師與客戶溝通的有效策略試題及答案
- 酒店財務(wù)風(fēng)險識別試題及答案
- 解析2024年機(jī)械工程師資格考試試題及答案
- 個人閑置租車合同范例
- 介紹業(yè)務(wù)抽成合同范例
- 個人醫(yī)院收購合同范例
- 倆人合伙裝修合同范例
- 保底收益合同范例
- 不良事件分級及上報流程
- 申請做女朋友的申請書
- 弱電系統(tǒng)維保合同
- 高中家長會 共筑夢想,攜手未來課件-高二下學(xué)期期末家長會
- 家電店慶活動方案范文
- 《特種設(shè)備無損檢測機(jī)構(gòu)檢測能力確認(rèn) 工作導(dǎo)則》
- 醫(yī)療糾紛鑒定委員會工作制度及職責(zé)
- 投標(biāo)項(xiàng)目售后服務(wù)方案
- 國土安全課件
- 第一講-17.1一元二次方程的概念
- 【MOOC】《電子技術(shù)實(shí)驗(yàn)》(北京科技大學(xué))中國大學(xué)MOOC慕課答案
評論
0/150
提交評論