C++基础之C与C++的区别二

C与C++的区别二

动态申请内存的区别

  • C语言中动态内存申请
    • malloc、calloc、realloc
  • C++的动态内存申请
    • new
  • C语言的内存释放
    • free
  • C++的内存释放
    • delete

单个变量内存申请

  • C语言

    	int* pC = (int*)malloc(sizeof(int));
    	*pC = 123;
         free(pC);
    	pC = NULL;
    
  • C++

    int* cpp = new int;
    	*cpp = 123;
    	cout << *cpp << endl;
    	delete cpp;
    

一段内存申请

  • C语言

    int* pcArray = (int*)malloc(sizeof(int) * 3);  //int pcArray[3];
    
  • C++

    int* cppArray = new int[3];    //int cppArray[3];
        delete[] cppArray;      //释放
    	cppArray = nullptr;
    

申请内存并做初始化

  • C语言

    • calloc
    int* pC = (int*)calloc(3, sizeof(int));
    
  • C++

    • 单个数据用()

      int* pNum = new int(100);      //*pNum=100
      	cout << pNum << endl;
      	delete pNum;
      	pNum = nullptr;
      
    • 多个数据用{}

      int* pArray = new int[3]{ 1,2,3 };  //int pArray[3]={1,2,3};
      

C++允许重新管理申请堆内存

  • (sum + 0)代表是从那个位置开始申请

    char* sum = new char[100];
    	int* pInt = new(sum + 0) int[4]{1,2,3,4};
    //再来10个字节存储字符串
    char* pstr = new(pInt + 4) char[10]{"ASDCFR"};//pInt+4可以用sum+16替换
        cout << sum + 16 << endl;
    	cout << pstr << endl;
    
    	delete[] sum;
    	sum = nullptr;
    

结构体的基本区别

  • 类型上
    • 不在需要struct ,直接结构体名可以充当类型,.c文件必须struct关键字
  • 访问方式和C语言没区别
    • 必须要用结构体变量访问
    • 变量访问的方式: 变量.成员
    • 指针表示,访问: 指针->成员
  • C++结构体中允许存在函数
    • 结构体中的函数如何访问数据:直接访问
    • 结构体中函数如何在类外实现:函数名必须要用:结构体名::函数名
    • 通过结构体中的函数去设置结构体数据
    • C++结构体申请内存

C语言结构体

struct MAB
{
	char name[20];
	int age;
};

void testStruct()
{
	struct MAB mab = { "张四",20 };
	printf("%s,%d", mab.name, mab.age);
}

C++结构体

struct MAB
{
	char name[20];
	int age;
};

void testStruct()
{
	MAB mab = { "张四",20 };
	cout << mab.name << "\t" << mab.age << endl;
}

C++结构体

#include <cstring>
#include <iostream>
using namespace std;
struct MAB 
{
	//数据成员
	char name[20];
	int age;
	//成员函数
	void print() 
	{
		cout << name << "\t" << age << endl;
	}
	void printData();
	void setData(const char* mabName, int mabAge);
};
void MAB::printData() 
{
	cout << name << "\t" << age << endl;
}
void MAB::setData(const char* mabName, int mabAge) 
{
	strcpy_s(name,20, mabName);    //"宝宝"
	age = mabAge;				  //19
}
void testCppStruct() 
{
	//1.基本的访问方式
	MAB mab = { "张三",19 };
	cout << mab.name << "\t" << mab.age << endl;
	MAB* pMAB = &mab;
	cout << pMAB->name << "\t" << pMAB->age << endl;
	//2.C++结构体中的函数的访问
	mab.print();						//打印就是mab.name,mab.age
	pMAB->print();
	MAB baby = { "baby",19 };
	baby.print();					//baby.name, baby.age
	baby.printData();
	//3.通过函数去描述行为
	MAB boy;
	boy.setData("小華", 19);			//boy.name="小華",boy.age=19
	boy.print();					//boy.name ,boy.age
	//4.C++结构体申请内存
	//这种写法是建立在没有构造函数的基础下是对的
	MAB* p = new MAB;
	p->setData("申请内存", 199);
	p->print();
	delete p;
	p = nullptr;
	MAB* pArray = new MAB[3];    
	for (int i = 0; i < 3; i++) 
	{
		pArray[i].setData("数组", i + 19);
		pArray[i].print();
	}
	delete[] pArray;
	pArray = nullptr;
}
int main() 
{
	testCppStruct();
	return 0;
}

C++String类型

  1. 常用的创建方式

    string str1;				//类似创建变量的方式
    	std::string noStd;			//没有using namespace std ,也需要加前缀
    	str1 = "Dfefefa";
    	cout << str1 << endl;
    	string str2("FDFEgad");
    	cout << str2 << endl;
    	string str3(str2);
    	cout << str3 << endl;
    
    • 不常用的创建方式

      string str4(5, 'O');				//str4="OOOOO";
      	cout << str4 << endl;
      	string str5("SDrdfeg", 1, 5);		//从0开始,用第一个到第五个字符初始化
      	cout << str5 << endl;
      
  2. 基本操作

    • 表示字符串属性

      cout << str5.size() << endl;		//元素个数
      	cout << str5.length() << endl;		//长度
      	cout << str5.capacity() << endl;	//容量
      	string  longStr = "12345678910123423";
      	cout << longStr.capacity() << endl;
      
    • 常规操作

      • 比较、等于、不等于,

      • 所有条件运算符直接用

        • 运算符重载
        cout << (longStr > str5) << endl;
        cout << (longStr == str5) << endl;
        	cout << (longStr != str5) << endl;
        
    • 连接:直接用+号连接

      string strF = "First";
      	string strS = "Second";
      	string result = strF + strS;
      	cout << result << endl;
      
  3. string与char* 的转换

    • 不能用%s直接打印string,需要调用函数data和c_str()

      string info("C++中的string");
      	printf("%s\n", info.c_str());
      	printf("%s\n", info.data());
      
    • 下标法访问string

      string pStr = "DFSFDEF";
      	for (int i = 0; i < pStr.length(); i++) 
      	{
      		cout << pStr.at(i);
      	}
      	cout << endl;
      	cout << pStr << endl;
      }
      

C++类型转换

  • 基本类型转换

    int cnum = (int)1.1;
    	cout << cnum << endl;
    	int cppnum = int(1.2);
    	cout << cppnum << endl;
    
  • static_cast

    • 可以用做基本数据类型转换

    • 把空类型的指针转换为目标指针类型

    • 不能转换带有const属性指针

      	//static_cast<要转换类型>(要转换的东西);
      //基本数据类型
      int num = static_cast<int>(dNum);
      	cout << num << endl;
      //const类型转换: 增加const
      	int x = 0;
      	const int cNum = static_cast<const int>(x);
      
  • const_cast: const属性指针的一些转换操作

    void setData(const char* mmName, int mmAge) 
    	{
    		name = const_cast<char*>(mmName);
    		age = mmAge;
    	}
    
  • reinterpret_cast:指针转整数,整数转指针

    void testreinterpret_cast() 
    {
    	auto pMax = Max;
    	cout << pMax << endl;
    	//int intNum = pMax;   //正常写是报错的,指针不能直接复制给变量
    	int num = reinterpret_cast<int>(pMax);
    	cout << num << endl;
    	auto ppMax = reinterpret_cast<int(*)(int, int)>(num);
    	cout << ppMax(1, 2) << endl;
    }
    
  • dynamic_cast :多态

posted @ 2022-10-13 21:23  理想还很年轻  阅读(46)  评论(0)    收藏  举报