c++与c的区别2

C++与C的区别

新基本数据类型

bool类型

  • 占用内存是1个字节
  • 打印出来的值是: 0或者1 非零值表示成立
  • 通常用的是false和true做初始化
#include <iostream>
void testBool()
{
	bool bNum = 5;
	std::cout << bNum << std::endl;
	//一般是会用都用关键字初始化
	bNum = false;
	bNum = true;
	//用途:
	//做开关
	//做标记-->查找类的操作
	//做函数返回值
	while (true);	//死循环
}
int main() 
{
	testBool();
	return 0;
}

引用类型

希望把引用类型理解为一个起别名的用法

  • 左值引用

  • 当做函数参数: 直接修改实参,防止拷贝本产生

  • 当做返回值: 增加左值使用

  • const属性限定问题

  • 右值引用

  • 给右值起别名

  • 当做函数参数: 函数只能传入右值
    。想要传入左值,使用move函数移动

#include <iostream>
using namespace std;
//No.2  引用类型
void Modify(int& x)  //int& x=b;
{
	x = 9999;
}
//返回引用等效返回一个变量
//warning C4172: 返回局部变量或临时变量的地址: a
int g_num = 1001;
int& getValue() 
{
	return g_num;
}
int getData() 
{
	return g_num;
}
void printString(const char* str)  //char* str="Iloveyou"
{
	cout << str << endl;
}
void modifyNum(const int& num) 
{
	//num++;  //常引用不能做++
	cout << num << endl;
}
void modifyNum2(int&& num)  //只能传入右值
{
	num++;					//提供修改接口
	cout << num << endl;
}

void testNew() 
{
	int a = 1;
	int& b = a;
	b = 1001;
	cout << a << endl;
	//当做函数返回值:增加左值使用好处
	//当做函数参数:防止拷贝本的产生
	Modify(b);
	cout << a << endl;
	//getData() = 1003;  错误,返回是一个值,不是一个变量,不能充当运算符左值
	getValue() = 9999;   //g_num=9999;
	cout << g_num << endl;
	//常量起别名
	//C++对于const属性要求更为严格
	//1.用常引用
	const int& xx = 13;
	cout << xx << endl;
	//error C2440: “初始化”: 无法从“const char [9]”转换为“char *”
	const char* pstr = "ILoveyou";
	printString("ILoveyou");
	char array[10] = "LLLL";
	printString(array);
	//常引用不能修改
	//2.右值引用 &&  --->提供修改接口
	int&& yy = 134;
	yy = 55;
	const int c_num = 112;  //常属性的变量,依旧是左值
	//int&& zz = c_num;		//错误
	modifyNum(1);
	modifyNum2(23);
	//左值变成右值
	int data = 1001;
	int&& data2 = move(data);       //移动函数:把左值变成右值
	int value = 1234;
	//modifyNum2(value);			//错误,用右值引用当做函数参数,不能传左值
	modifyNum2(move(value));        //移动语义(完美转发)
}
int main() 
{
	testNew();
	return 0;
}

C++结构体类型

  • 类型名不在需要struct关键字了
  • C++结构体可以给成员直接赋初始值
  • C++结构体可以包含函数
  • 其实C++结构体的处理就是按照类的方式处理(后续在意)
    。用了构造函数时候C++结构体和C语言结构体处理方案是完全不同的
#include <iostream>
using namespace std;
//描述的是事物特征和行为
struct MM 
{
	char name[20]="MM";
	int age=111;
	int num=1001;
	void initData(const char* str, int age, int num);		//在结构体声明
	void printData() 
	{
		cout << name << "\t" << age << "\t" << num << endl;
	}
};
//在外面实现,必须结构体名限定
void MM::initData(const char* str,int age,int num) 
{
	strcpy_s(name,20, str);
	//同名问题
	MM::age = age;
	MM::num = num;
}
int main() 
{
	//No.1 类型不需要struct
	MM mm;
	//cout << mm.name << "\t" << mm.age << "\t" << mm.num << endl;
	mm.printData();
	MM mm2 = { "mm2",18,1030 };
	//cout << mm2.name << "\t" << mm2.age << "\t" << mm2.num << endl;
	mm2.printData();
	mm2.initData("小芳", 18, 1004);
	mm2.printData();
	return 0;
}

C++枚举类型

#include <iostream>
using namespace std;
enum State {Open,Close};
enum class Color
{
	Red,
	Blue
};
void print(int state)
{
	cout << state << endl;
}
void printEnum(Color color) 
{
	//cout << color << endl;  不能直接打印
}
void testEnum() 
{
	print(1);
	print(Open);
	//C++的枚举类型不能当做一个简单的int类型
	//print(Color::Red);  //访问必须要用枚举类型名限定
	printEnum(Color::Red);
}
int main() 
{
	testEnum() 
	return 0;
}

C++string类型

C++string本身是一个类

  • 头文件: #include
    。注意点和cstring区别,这个是C语言头文件
  • 没有用using namespace std ; string类型需要改为std::string
  • 掌握string常用方式
    。创建方式
    。基本操作(比较,连接)
    。转C语言char*
    微软帮助文档:basic_string 类 | Microsoft Docs
#include <iostream>
#include <string>
using namespace std;
int main() 
{
	//std::string str;
	//No.1创建
	string str1;
	str1 = "ILoveyoudsfasdfasdfasdfasdfasd";
	cout << str1 << endl;
	string str2 = "ILoveyousdfsdafads";
	cout << str2 << endl;
	string str3(str2);
	string str4 = str2;
	cout << str3 << endl;
	cout << str4 << endl;
	//No.2 基本操作
	cout << (str3 == str4) << endl;
	cout << (str3 >= str4) << endl;

	string password;
	cin >> password;
	cout << password;
	//原理是:运算符重载 后面会讲
	//比较直接比
	if (password == string("12345")) 
	{
		cout << endl << "密码正确" << endl;
	}
	int a = (int)1.3;		//C语言强制类型转换
	int b = int(1.34);		//C++强制类型转换
	//连接直接加法
	string name = "张三";
	string info = name + "很帅";
	cout << info << endl;
	//上述复杂的用法
	cout << info.compare(name) << endl;  //和strcmp 返回值一样
	cout << info.append(name) << endl;

	//下标的方式访问
	cout << info.length() << endl;	   //当前长度
	cout << info.size() << endl;
	for (int i = 0; i < info.length(); i++) 
	{
		cout << info[i];
	}
	cout << endl;
	for (int i = 0; i < info.length(); i++)
	{
		cout << info.at(i);
	}
	cout << endl;
	cout << info.capacity() << endl;   //容量
	//和C语言string.h有区别的 char* 
	// 不能用%s的方式打印
	//printf("%s\n", info);
	// 图形库中传char* 函数 都不能直接用string类型
	//提供两个函数: c_str() data();
	printf("%s\n", info.c_str());
	printf("%s\n", info.data());
	return 0;
}

C++自动推断类型

  • auto类型
  • decltype类型
#include <iostream>
using namespace std;
struct MM 
{
	int age;
};
MM g_MM = { 12 };
MM* createMM() 
{
	MM* p=&g_MM;
	return p;
}
int Max(int a, int b) 
{
	return a > b ? a : b;
}
void print(int(*Max)(int, int), int a, int b) 
{
	cout << Max(a, b) << endl;
}
void printData(int(*)(int, int), int, int) 
{
	cout << "另一个函数" << endl;
}

int main() 
{
	//auto 类型自动推断: 一定要有赋值 所以不能单独定义变量
	//auto a;  没有赋值 推断不出来
	auto a = 1;				//int 
	a = 134;
	auto pMM = createMM();
	cout << pMM->age << endl;
	auto pFunc = print;		//void (*)(int(*)(int, int), int , int ) 
	pFunc(Max, 1, 3);
	pFunc = printData;
	pFunc(Max, 1, 3);
	int* p = nullptr;			//新的空 等效C语言NULL
	//decltype 不需要赋值

	decltype(123) num = 123;    //decltype(123) 表示一个int类型
	//函数指针必须取地址
	decltype(&print) ppFunc;
	ppFunc = printData;
	//推断一个int
	decltype(num) Num = num;
	decltype(&num) pNum = &num;	//当你们用的时候,不知道类型,推断定义变量去赋值
	return 0;
}

C++动态内存申请

C++申请的内存是自由存储区的,C语言的堆区内存,所以C++类的对象内存不能用malloc申请

  • new申请内存
    。申请单个变量内存
    。申请一段内存
    。申请内存可以手动初始化
    。申请内存后可以再分配
  • delete释放内存
    。释放单个变量内存:delete 指针名;
    。释放一段变量内存: delete[] 指针名;
#include <iostream>
using namespace std;
struct MM 
{
	char name[20];
	int age;
	int num;
};
void testOne() 
{
	int* pInt = new int;
	*pInt = 123;
	cout << pInt[0] << endl;
	delete pInt;
	pInt = nullptr;
	char* pc = new char;
	delete pc;

	int* pNum = new int(1234);	//申请并且初始化
	cout << pNum[0] << endl;
	delete pNum;
	MM* pMM = new MM({ "name",12,1101 });
	cout << pMM->name << "\t" << pMM->age << "\t" << pMM->num << endl;
}
void testTwo() 
{
	//一段内存的申请
	int size = 4;
	int* pArray = new int[size];		//int pArray[4];
	MM* pMM = new MM[4];				//MM pMM[4];
	delete[] pArray;
	delete[] pMM;
	pArray = nullptr;
	pMM = nullptr;

	int* pNum = new int[4]{ 1, 2, 3, 4 };
	for (int i = 0; i < 4; i++) 
	{
		cout << pNum[i] << "\t";
	}
	cout << endl;
	delete[] pNum;
	pNum = nullptr;
}
void testThree() 
{
	char* pMem = new char[1024];
	//在原来上面拿个20字节存整数
	int* pInt = new(pMem + 0) int[5]{ 1,2,3,4,5 };
	//在原来上面拿出20个存字符
	char* pChar = new(pMem + 20) char[20]{"ILoveyou"};
	//等效:char* pChar = new(pInt + 5) char[20]{"ILoveyou"};
	delete[] pMem;
	pMem = nullptr;
}
int main() 
{
	testOne();
	testTwo();
	testThree();
	return 0;
}

小试牛刀

#include <iostream>
using namespace std;
int** create2DArray(int row, int cols)
{
    int** pArray = new int* [row];
    for (int i = 0; i < row; i++)
    {
        pArray[i] = new int[cols];
    }
    return pArray;
}
void create2DArray(int**& pArray, int row, int cols)
{
    pArray = new int* [row];
    for (int i = 0; i < row; i++) 
    {
        pArray[i] = new int[cols];
    }
}
void Free(int**& array,int row)
{
    for (int i = 0; i < row; i++) 
    {
        delete[] array[i];
    }
    delete[] array;
    array = nullptr;
}
int main()
{
    int** p2D = nullptr;
    int cols, rows;
    //让用户输入行列
    cin >> rows >> cols;
    //create2DArray(p2D, rows, cols);
    p2D = create2DArray(rows,cols);
    //让用户赋值
    for (int i = 0; i < rows; i++) 
    {
        for (int j = 0; j < cols; j++) 
        {
            cin >> p2D[i][j];
        }
    }
    //打印结果
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            cout << p2D[i][j]<<" ";
        }
        cout << endl;
    }
    //释放
    Free(p2D,rows);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

兴趣使然的Qsiri

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值