C语言与C++之间的区别(二)
文章目录
前言
经过进一步学习现扩展C语言与C++之间数据结构 动态内存和string类型之间的区别
提示:以下是本篇文章正文内容,下面案例可供参考
一、结构体之间的区别
1.类型上的区别
类型上C语言与C++不同,C++不再需要struct关键字,直接用结构体名即可
#include <iostream>
using namespace std;
struct AA
{
char name[20]; //数据成员
int age;
};
int main()
{
struct AA teacher = { "王", 28 };
AA student = { "李", 24 };
cout << teacher.name << student.age << endl;
cout << student.name << student.age << endl;
while (1);
return 0;
}
运行结果:
2.C++结构体内允许函数存在
在结构体声明,在结构体外实现,也可以直接在结构体中实现
结构体中函数访问数据是可以直接访问
调用方法和数据成员方式是一样的
1、对象(结构体变量).成员
2、对象指针->成员
3、(*对象指针).成员
C++在没有写构造函数和权限限定的时候,用法和C语言的用法是一样的
#include <iostream>
using namespace std;
struct AA
{
char name[20]; //数据成员
int age;
void print() //成员函数
{
cout << name << "\t" << age << endl;
}
void printData(); //在结构中声明,在外面实现
int& getAge() //通过外部函数修改数据
{
return age;
}
};
//结构体中的变量必须要通过结构体变量(结构体指针)访问
//C++结构体中的函数访问属性,可以直接访问
void AA::printData()
{
cout << name << "\t" << age << endl;
}
int main()
{
struct AA teacher = { "王", 28 };
AA student = { "李", 24 };
teacher.print();
(&student)->printData;
AA* p = &student;
p->printData();
p->getAge() = 30;
p->printData();
p->age = 21;
p->printData();
AA array[3]; //定义结构体数组
while (1);
return 0;
}
运行结果:
二、动态内存申请
1.C语言动态内存申请
malloc 不带初始化 ,calloc 带初始化,realloc 重新申请
free 释放
2.C++的动态申请
new(申请)和delete(释放)
单个变量内存申请
#include <iostream>
#include <cstring>
using namespace std;
void testOneMemory()
{
//申请不做初始化
int* pInt = new int;
*pInt = 123;
cout << *pInt << endl;
char* pChar = new char;
*pChar = 'A';
cout << *pChar << endl;
//申请内存做初始化 ()给单个数据初始化
int* pNum = new int(134);
cout << *pNum << endl;
delete pInt;
pInt = nullptr;
pInt = new int;
*pInt = 332;
cout << *pInt << endl;
delete pInt;
pInt = nullptr;
delete pChar;
pChar = nullptr;
delete pNum;
pNum = nullptr;
}
int main()
{
testOneMemory();
while (1);
return 0;
}
运行结果:
数组的动态申请
#include <iostream>
#include <cstring>
using namespace std;
void testArrayMemory()
{
//一维数组
//1.不带初始化
//长度可以是变量,只要值就可以
int* pInt = new int[3]; //等效产生了int pInt[3]的数组
char* pstr = new char[15];
strcpy_s(pstr, 15, "申请动态数组");
cout << pstr << endl;
//cout << pstr1 << endl;
//带初始化的 一堆数据用 {}
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; //数组的释放 不需要大小
//释放只有两种形式 delete 指针 delete [] 指针
//delete [][] p 没有这种写法
pInt = nullptr;
}
int main()
{
testArrayMemory();
while (1);
return 0;
}
在这里插入代码片
结构体内存申请
#include <iostream>
using namespace std;
struct AA{
char* name;
int age;
void printAA() //成员函数
{
cout << name << "\t" << age << endl;
}
};
void testStructMemory(){
int *p = new int(23);
AA* pAA = new AA; //结构体之能用大括号
pAA->name = new char[20]; //结构体中指针,要做二次申请,才能strcpy,或者赋值
strcpy_s(pAA->name, 20, "丽丝");
pAA->age = 188;
pAA->printAA();
delete [] pAA->name; //申请顺序和释放顺序是相反的
delete pAA;
}
int main()
{
testStructMemory();
while (1);
return 0;
}
运行结果:
内存池
C++允许申请一段内存,共给程序使用,综合管理内存
#include <iostream>
using namespace std;
//允许申请一段内存,共给程序使用,综合管理内存
//malloc内存是在堆区
//new内存是在自由存储区
void testmemory()
{
char* memorySum = new char[1024];
int *pNum = new(memorySum ) int[3]{1, 2, 3}; //int* pNum = new(申请内存的开始位置) int[3]
char *pstr = new(memorySum + 12) char[20]{"综合管理内存"};
//char* pstr = new(pNum + 3) char[20]{ "综合管理内存" };
//和下面这句话是等效的
for (int i = 0; i < 3; i++)
{
cout << pNum[i] << " ";
}
cout << endl;
for (int i = 0; i < 3; i++)
{
cout << ((int *)memorySum [i]) << " ";
}
cout << endl << pstr << endl;
cout << (memorySum + 12) << endl;
delete[]memorySum ;
memorySum = nullptr;
}
int mian(){
testmemory();
while (1);
return 0;
}
String类型
String创建
带初始化
不带初始化
通过另一个字符串创建
#include <iostream>
#include <string> //注意和string.h的区别
using namespace std;
void CreatString()
{
string str1;
str1 = "string";
const string cstr; //cstr="string";是错误的常属性不能改变
cout << "First:" << str1 << endl;
string str2("string");
cout << str2 << endl;
string str3 = "string";
cout << str3 << endl;
string str4(str3);
cout << str4 << endl;
string str5 = str4;
cout << str5 << endl;
//一般没有长度限定,在使用范围下
}
int main()
{
CreatString();
while (1);
return 0;
}
运行结果:
string基本操作
拷贝
赋值
链接
比较
#include <iostream>
#include <string> //注意和string.h的区别
using namespace std;
void operatorString()
{
string str1 = "one";
string str2 = "two";
string str3 = str2;
cout << str3 << endl;
//没有减法
string str4 = str2 + str1;
cout << str4 << endl;
//等效: string str4=str1.append(str2);
//C++中尽量用string 不要用char* ,可以用
//比较直接比较即可
//> < != ==
//str1.compare(str2) 0 -1 1
if (str1 > str2) //比较依旧按照char* 去比较
{
cout << "大的" << str1 << endl;
}
else
{
cout << "小的" << str2 << endl;
}
}
int main()
{
operatorString();
while (1);
return 0;
}
运行结果:
## C++string与C语言string.h
#include <iostream>
#include <string> //注意和string.h的区别
#include <stdio.h>
#include <cstring> //string.h和cstring是一样
using namespace std;
void compareCAndCpp()
{
//C++中是一个自定义类型(类),目前当作结构体即可
//C++string 不能用到C语言的字符串处理函数
//C++如何转换为C语言的char*
//c_str() data()函数
string str1 = "string";
//printf("%s", str1);
printf("%s\n", str1.c_str());
printf("%s\n", str1.data());
//outtextxy(int x,int y,char* str);
//直接把数字转换为相应的字符串
string str2 = to_string(1234);
//atoi
cout << str2 << endl;
}
int main()
{
compareCAndCpp();
while (1);
return 0;
}
运行结果:
string 其他函数操作
#include <iostream>
#include <string> //注意和string.h的区别
#include <stdio.h>
#include <cstring> //string.h和cstring是一样
using namespace std;
void exoperator()
{
//下标法打印string
string str = "string";
//C++string中没有记录\0
for (int i = 0; i < 8; i++)
{
cout << str[i];
}
cout << endl;
//empty()
//size();
string mystring = "string";
//cout << sizeof(mystring) << endl; //28
//cout <<"容量:" <<mystring.capacity() << endl;
cout << "mystring:" << mystring.size() << endl;
string strEmpty;
if (strEmpty.empty()) //return length==0;
{
cout << "string为空" << endl;
}
}
int main()
{
exoperator();
while (1);
return 0;
}
运行结果:
作业
二维数组的动态内存申请,采用子函数的方式 为二级指针申请内存,和释放内存
#include<iostream>
#define cow 4
#define row 4
using namespace std;
int** Creatp(int , int )
{
int **p = new int*[row]; //申请二级指针动态内存
for (int i = 0; i < row; i++) //给每个二级指针申请一级指针动态内存
{
p[i] = new int[cow];
}
return p;
}
int main()
{
int** p = Creatp(cow, row);
for (int i = 0; i < 4; i++) //赋值
{
for (int j = 0; j < 4; j++)
{
p[i][j] = i*j;
cout << p[i][j] << "\t";
}
cout << endl;
}
delete[]p;
p = nullptr;
while (1);
return 0;
}
运行结果: