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)體外實(shí)現(xiàn),當(dāng)然可以直接在結(jié)構(gòu)體中實(shí)現(xiàn)
- 結(jié)構(gòu)體中函數(shù)訪問數(shù)據(jù),是可以直接訪問
- 學(xué)會(huì)調(diào)用,和數(shù)據(jù)成員方式時(shí)一樣的
- 對(duì)象(結(jié)構(gòu)體變量).成員
- 對(duì)象指針->成員
- (*對(duì)象指針).成員
- C++在沒有寫構(gòu)造函數(shù)和權(quán)限限定的時(shí)候,用法和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)體中聲明,在外面實(shí)現(xiàn)
int& getAge()
{
return age;
}
};
//結(jié)構(gòu)體名限定,就是告訴別人這個(gè)函數(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. 動(dòng)態(tài)內(nèi)存申請(qǐng)
C語言的動(dòng)態(tài)內(nèi)存申請(qǐng)
- malloc 不帶初始化 ,calloc 帶初始化,realloc 重新申請(qǐng)
- free 釋放
C++的動(dòng)態(tài)申請(qǐng)
- new(申請(qǐng))和delete(釋放)
- 單個(gè)變量內(nèi)存申請(qǐng)
- 數(shù)組的動(dòng)態(tài)申請(qǐng)
- 結(jié)構(gòu)體內(nèi)存申請(qǐng)
例子:單個(gè)變量內(nèi)存申請(qǐng)和數(shù)組的動(dòng)態(tài)申請(qǐng)
#include<iostream>
#include<string>
using namespace std;
void testNoeMemory()
{
//申請(qǐng)不做初始化
int* pInt = new int;
*pInt = 123;
cout << *pInt << endl;
char* pChar = new char;
*pChar = 'A';
cout << *pChar << endl;
//申請(qǐng)內(nèi)存做初始化 ()給單個(gè)數(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)存申請(qǐng)
#include<iostream>
#include<string>
using namespace std;
void testNoeMemory()
{
//申請(qǐng)不做初始化
int* pInt = new int;
*pInt = 123;
cout << *pInt << endl;
char* pChar = new char;
*pChar = 'A';
cout << *pChar << endl;
//申請(qǐng)內(nèi)存做初始化 ()給單個(gè)數(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一個(gè)對(duì)象
int* p = new int(123);
//結(jié)構(gòu)體只能用大括號(hào)
MM* pMM = new MM;
//結(jié)構(gòu)體中指針,要做二次申請(qǐng),才能strcpy,或者賦值
pMM->name = new char[20];
strcpy_s(pMM->name, 20, "李四");
pMM->age = 188;
pMM->printMM();
//申請(qǐng)的順序和釋放的順序是相反的
delete[]pMM->name;
delete pMM;
}
int main()
{
//testNoeMemory();
testStructMerrory();
return 0;
}
3. 內(nèi)存池
允許大家申請(qǐng)一段內(nèi)存,共給程序使用,綜合管理內(nèi)存
4. string類型
只需要知道有這種用法即可,不需要大家深究為什么,因?yàn)閟tring本身是一個(gè)類,需要講完類的大部分知識(shí),才能追究為什么這樣做。自己也可以封裝一個(gè)string 類型
- string創(chuàng)建
+ 帶初始化
+ 不帶初始化
+ 通過另一個(gè)字符串創(chuàng)建
- string基本操作
+ 拷貝
+ 賦值
+ 連接
+ 比較
- C++string與C語言string.h
- string 其他函數(shù)操作
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
C++中POCO庫的安裝與基礎(chǔ)知識(shí)介紹(Windwos和Linux)
這篇文章主要為大家介紹了C++ POCO庫的簡單介紹、下載以及安裝方式、簡單代碼示例,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-05-05
基于OpenCV實(shí)現(xiàn)車道線檢測(cè)(自動(dòng)駕駛 機(jī)器視覺)
無人駕駛技術(shù)是機(jī)器學(xué)習(xí)為主的一門前沿領(lǐng)域,在無人駕駛領(lǐng)域中機(jī)器學(xué)習(xí)的各種算法隨處可見,本文將為大家介紹無人駕駛技術(shù)中的車道線檢測(cè),感興趣的小伙伴可以了解一下2021-11-11

