C++與C語言的區(qū)別你知道嗎
1. 結(jié)構(gòu)體區(qū)別
1.1. 類型上不再需要struct關(guān)鍵字,直接用結(jié)構(gòu)體名即可
#include <iostream> #include<string> using namespace std; struct MM { char name[20]; int age; }; int main() { struct MM girl; MM mm;//C++中不需要struct關(guān)鍵字 return 0; }
1.2. C++結(jié)構(gòu)體中允許函數(shù)存在
- 在結(jié)構(gòu)體中聲明,在結(jié)構(gòu)體外實現(xiàn),當(dāng)然可以直接在結(jié)構(gòu)體中實現(xiàn)
- 結(jié)構(gòu)體中函數(shù)訪問數(shù)據(jù),是可以直接訪問
- 學(xué)會調(diào)用,和數(shù)據(jù)成員方式時一樣的
- 對象(結(jié)構(gòu)體變量).成員
- 對象指針->成員
- (*對象指針).成員
- C++在沒有寫構(gòu)造函數(shù)和權(quán)限限定的時候,用法和C語言的用法是一樣
#include <iostream> #include<string> using namespace std; struct MM { //屬性 //數(shù)據(jù)成員 char name[20]; int age; //行為(方法) //成員函數(shù) void print() { cout << name << "\t" << age << endl; } void printData();//在結(jié)構(gòu)體中聲明,在外面實現(xiàn) int& getAge() { return age; } }; //結(jié)構(gòu)體名限定,就是告訴別人這個函數(shù)來自哪里 void MM::printData() { cout << name << "\t" << age << endl; } //結(jié)構(gòu)體中的變量必須要通過結(jié)構(gòu)體變量(結(jié)構(gòu)體指針)訪問 //c++結(jié)構(gòu)體中的函數(shù)訪問屬性,可以直接訪問 int main() { struct MM girl = {"小芳",28}; MM mm = {"小麗",24};//C++中不需要struct關(guān)鍵字 girl.print(); (&mm)->printData(); MM* p = &mm; p->printData(); p->getAge() = 84; p->printData(); p->age = 1991; p->printData(); return 0; }
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)申請
#include<iostream> #include<string> using namespace std; void testNoeMemory() { //申請不做初始化 int* pInt = new int; *pInt = 123; cout << *pInt << endl; char* pChar = new char; *pChar = 'A'; cout << *pChar << endl; //申請內(nèi)存做初始化 ()給單個數(shù)據(jù)做初始化 int* pNum = new int(134); cout << *pNum << endl; delete pInt; pInt = nullptr; delete pChar; pChar = nullptr; delete pNum; pNum = nullptr; } void testArrayMerrmory() { //一維數(shù)組 //1、不帶初始化 //長度可以是h變量,只要值就可以 int* pInt = new int[3];//等效產(chǎn)生了 int pInt[3]的數(shù)組 char* pstr = new char[15]; strcpy_s(pstr, 15, "I love you"); cout << pstr << endl; //帶初始化的 一堆數(shù)據(jù)用{} int* pNum = new int[3]{1, 2, 3}; for (int i = 0; i < 3; i++) { cout << pNum[i] << " "; } cout << endl; delete[] pNum; char* str = new char[20]{'A', 'B', '\0'}; cout << str << endl; delete[] str; str = nullptr; str = new char[20]{"Ilove you"}; cout << str << endl; delete[] str; str = nullptr; delete[] pInt;//數(shù)組的指針 不需要大小 //釋放只有兩種形式 delete 指針 delete [] 指針 //delete [][] p 沒有這種寫法 pInt = nullptr; } int main() { testNoeMemory(); return 0; }
例子:結(jié)構(gòu)體內(nèi)存申請
#include<iostream> #include<string> using namespace std; void testNoeMemory() { //申請不做初始化 int* pInt = new int; *pInt = 123; cout << *pInt << endl; char* pChar = new char; *pChar = 'A'; cout << *pChar << endl; //申請內(nèi)存做初始化 ()給單個數(shù)據(jù)做初始化 int* pNum = new int(134); cout << *pNum << endl; delete pInt; pInt = nullptr; delete pChar; pChar = nullptr; delete pNum; pNum = nullptr; } void testArrayMerrmory() { //一維數(shù)組 //1、不帶初始化 //長度可以是h變量,只要值就可以 int* pInt = new int[3];//等效產(chǎn)生了 int pInt[3]的數(shù)組 char* pstr = new char[15]; strcpy_s(pstr, 15, "I love you"); cout << pstr << endl; //帶初始化的 一堆數(shù)據(jù)用{} int* pNum = new int[3]{1, 2, 3}; for (int i = 0; i < 3; i++) { cout << pNum[i] << " "; } cout << endl; delete[] pNum; char* str = new char[20]{'A', 'B', '\0'}; cout << str << endl; delete[] str; str = nullptr; str = new char[20]; cout << str << endl; delete[] str; str = nullptr; delete[] pInt;//數(shù)組的指針 不需要大小 //釋放只有兩種形式 delete 指針 delete [] 指針 //delete [][] p 沒有這種寫法 pInt = nullptr; } struct MM { char* name; int age; void printMM() { cout << name << "\t" << age << endl; } }; void testStructMerrory() { //new一個對象 int* p = new int(123); //結(jié)構(gòu)體只能用大括號 MM* pMM = new MM; //結(jié)構(gòu)體中指針,要做二次申請,才能strcpy,或者賦值 pMM->name = new char[20]; strcpy_s(pMM->name, 20, "李四"); pMM->age = 188; pMM->printMM(); //申請的順序和釋放的順序是相反的 delete[]pMM->name; delete pMM; } int main() { //testNoeMemory(); testStructMerrory(); return 0; }
3. 內(nèi)存池
允許大家申請一段內(nèi)存,共給程序使用,綜合管理內(nèi)存
4. string類型
只需要知道有這種用法即可,不需要大家深究為什么,因為string本身是一個類,需要講完類的大部分知識,才能追究為什么這樣做。自己也可以封裝一個string 類型
- string創(chuàng)建
+ 帶初始化
+ 不帶初始化
+ 通過另一個字符串創(chuàng)建
- string基本操作
+ 拷貝
+ 賦值
+ 連接
+ 比較
- C++string與C語言string.h
- string 其他函數(shù)操作
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
C++編程中new運(yùn)算符的使用學(xué)習(xí)教程
這篇文章主要介紹了C++編程中new運(yùn)算符的使用學(xué)習(xí)教程,是C++入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2016-01-01c語言程序設(shè)計文件操作方法示例(CreateFile和fopen)
c主要的文件操作函數(shù)有:CreateFile,CloseHandle,ReadFile,WriteFile,SetFilePointer,GetFileSize。其中的讀寫操作是以字符為單位,獲得文件大小也是以字符為單位。2013-12-12ubuntu系統(tǒng)下C++調(diào)用matlab程序的方法詳解
學(xué)習(xí)c++與matlab混合編程一般是通過c++調(diào)用matlab函數(shù),因為matlab具有強(qiáng)大的數(shù)學(xué)函數(shù)庫,然而vc++具有界面設(shè)計靈活的優(yōu)點,下面這篇文章主要給大家介紹了關(guān)于在ubuntu系統(tǒng)下C++調(diào)用matlab程序的方法,需要的朋友可以參考下。2017-08-08深入解析C++設(shè)計模式編程中解釋器模式的運(yùn)用
這篇文章主要介紹了C++設(shè)計模式編程中解釋器模式的運(yùn)用,解釋器模式給定一個語言,定義它的文法的一種表示,并定義一個解釋器,這個解釋器使用該表示來解釋語言中的句子,需要的朋友可以參考下2016-03-03C++將二叉樹轉(zhuǎn)為雙向鏈表及判斷兩個鏈表是否相交
這篇文章主要介紹了C++將二叉樹轉(zhuǎn)為雙向鏈表及判斷兩個鏈表是否相交的方法,文中還給出了求兩個鏈表相交的第一個節(jié)點列的實現(xiàn)方法,需要的朋友可以參考下2016-02-02