




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quá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)體外實現(xiàn),當然可以直接在結(jié)構(gòu)體中實現(xiàn)結(jié)構(gòu)體中函數(shù)訪問數(shù)據(jù),是可以直接訪問學會調(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)體中聲明,在外面實現(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類型
只需要知道有這種用法即可,不需要大家深究為什么,因為string本身是一個類,需要講完類的大部分知識,才能追究
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 圣誕節(jié)童話活動方案
- 城市拓展活動方案
- 天冷足浴店活動方案
- 夫妻保單活動方案
- 央行五四活動方案
- 夏季雪糕活動方案
- 基本實踐活動方案
- 大班英文活動方案
- 大班城市活動方案
- 大型買菜活動方案
- 《智能駕駛輔助系統(tǒng)ADAS》課件
- 2024年自然資源部所屬單位招聘筆試真題
- 江西吉安市吉水縣吉瑞招商運營有限公司招聘筆試題庫2025
- 自然照護理念體位管理
- 《關(guān)稅政策解析》課件
- 武漢網(wǎng)約車從業(yè)資格證考試題庫及答案
- 鋁粉交易居間協(xié)議合同
- 耐高溫有機硅樹脂合成及改性技術(shù)
- 竹編非遺面試題及答案
- 國家開放大學漢語言文學本科《中國現(xiàn)代文學專題》期末紙質(zhì)考試第三大題分析題庫2025春期版
- 離婚協(xié)議書 標準版電子版(2025年版)
評論
0/150
提交評論