C++和C的区别2

auto

#include<iostream>

using namespace std;

//auto自适应数据类型,自动推理
void main1()
{
	//auto num = 10;//int
	//auto num = 10.1;//double
	//outo num = 'A';//char
	auto num = L"bobo";
	cout << typeid(num).name() << endl;

	cin.get();
}

void main2()
{
	int a[5] = { 1,2,3,4,5 };
	for (auto i : a)//auto i是副本
	{
		i += 1;
		cout << i << endl;//12345
	}
	for (auto &i : a)//加上&是原本
	{
		i += 1;
		cout << i << endl;//12345
	}
	for (auto i : a)
	{
		cout << i << endl;//12345
	}

	system("pause");
}

template <class T1,class T2>
auto add(T1 t1, T2 t2)//自动推理数据类型
{
	return t1 + t2;
}

void main()
{
	cout << add(10, 11.1) << endl;
	cout << add(10.1, 11) << endl;

	system("pause");
}

array

#include<iostream>
#include<array>

using namespace std;

void main1()
{
	//C风格数组
	int a[5];
	int b[5][5];
	/*for (auto i : a)//不能用auto循环
	{
		for (auto j : i)
		{

		}
	}*/

	cin.get();
}

void main2()
{
	array<int, 10>myint{ 1,2,3,4,5,6,7,8,9,10 };//CPP风格数组
	for (auto i : myint)
	{
		cout << i <<"   " << &i << endl;//i地址一样,有副本机制
	}
	//内存连续,在栈上
	for (int i = 0; i < 10; i++)
	{
		cout << myint[i] << "   " << &myint[i] << endl;//这样打印地址是正确的
	}

	cin.get();
}

void main()
{
	//CPP风格二维数组
	array<int, 10>myint1{ 12,2,33,42,5,6,7,8,9,10 };
	array<int, 10>myint2{ 13,2,3,44,5,65,7,8,9,10 };
	array<int, 10>myint3{ 1,22,3,4,35,6,47,8,9,10 };
	array<array<int, 10>, 3>myint{ myint1 ,myint2 ,myint3 };//嵌套
	for (auto j : myint)
	{
		for (auto i : j)
		{
			cout << i << "  ";
		}
		cout << "\n";
	}
	//第二种打印方式
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 10; j++)
		{
			cout << myint[i][j] << "   ";
		}
		cout << endl;
	}

	cin.get();
}

bool

#include<iostream>
using namespace std;

void main1()
{
	//bool b = 3;//非0解析为1   0解析为0
	bool b = true;//应该赋值为true或者false
	cout << sizeof(b) << endl;//1个字节
	cout << b << endl;//1

	while (b)//死循环
	{
		cout << "haahha" << endl;
	}

	cin.get();
}

void main()
{
	bool b1(false);//初始化
	bool b1(true);
	//b1 = 0 && 10 && -1 || 2 + 3;//+运算符优先,所以是死循环
	//b1 = 0 && 10 && -1 || 2;// 0 && 10 && -1可以看做一个整体,0||2  还是死循环
	b1 = 3 > 2 && 3 < 1 || 1 | 2;//|位或  肯定是非0   死循环

	while (b1)
	{
		cout << "11111";
	}
}

nullspr

#include<iostream>
using namespace std;

void show(int num)
{
	cout << "int" << endl;
}
void show(int *p)
{
	cout << "int *" << endl;
}

void main()
{
	show(NULL);//int  会解释为一个整数0  C风格的空指针,会被误认为整数
	show(nullptr);//int *  C++风格空指针
	cout << typeid(NULL).name() << endl;//int
	cout << typeid(nullptr).name() << endl;//str::nullptr_t
	//严格类型匹配
	cin.get();
}

decltype

#include<iostream>
using namespace std;

void main1()
{
	auto a1("hello");
	auto b1(1 && 0 && 3 || -1);
	cout << typeid(b1).name() << "    " << b1 << endl;//bool   1
	auto b2(b1);//auto可以实现拷贝,但是只能拷贝一个变量
	cout << typeid(b2).name() << "    " << b2 << endl;//bool   1
	//decltype是一个关键字
	//decltype(b1)根据一个变量表达式获取类型,创建数组,创建指针
	decltype(b1) b3[5]{ 0 };
	for (auto i : b3)
	{
		cout << typeid(i).name() << "    " << i << endl;//bool   1
	}

	cin.get();
}

template<class T>
void show(T *p)
{
	decltype(*p) num(*p);//内存备份
	cout << num << endl;
	cout << *p << endl;
}
void main2()
{
	int num = 10;
	show(&num);//10   10
}

template<class T>
void show1(T *p)
{
	int a = *p;//获取一个类型
	decltype(a) num[5]{ a,a ,a ,a ,a };
	for (auto i : num)
	{
		cout << i << endl;
	}
}

void main()
{
	int num = 10;
	show1(&num);
	cin.get();
}



<think>嗯,用户想知道C++C语言的区别共同点。首先,我得回顾一下两者的基本背景。C语言是1972年由Dennis Ritchie开发的,主要用于系统编程,比如操作系统。而C++是Bjarne Stroustrup在1980年代初期创建的,最初叫做“C with Classes”,目的是为了增强C语言的面向对象能力。 共同点的话,两者都是编译型语言,语法结构相似,很多基础语法比如循环、条件语句、运算符都是相同的。它们都支持指针内存管理,可以直接操作内存,这也是它们高效的原因之一。此外,C++兼容大部分C语言的代码,所以很多C程序可以在C++编译器里运行,当然有些地方可能需要调整。 区别方面,最大的不同是C++支持面向对象编程,有类、继承、多态这些特性。而C是过程式的,主要用函数来组织代码。C++还有标准模板库(STL),提供了很多容器算法,比如vector、map,这些在C里是没有的。异常处理也是C++特有的,用try/catch块,而C通常通过返回错误码来处理错误。函数重载在C++允许同名函数参数不同,但C不允许。另外,C++有引用类型,而C只能用指针。内存管理上,C++有newdelete运算符,C则用mallocfree,而且类型转换方面C++更严格,比如static_cast、dynamic_cast等,而C是隐式转换。 举个代码例子,比如面向对象的部分,C++可以用类来封装数据方法,而C只能用结构体函数。还有函数重载的例子,C++可以定义多个同名函数,参数不同,而C不行。 可能用户是想了解是否应该学习C还是C++,或者他们项目中选择哪个更合适。或者他们可能对两种语言的性能差异、适用场景感兴趣。需要确保回答涵盖主要的技术差异共同点,同时给出实际的例子帮助理解。</think><< C++与C语言的区别共同点如下: ### **共同点**: 1. **语法基础**:C++继承了C的基本语法,如数据类型、运算符、控制结构(`if`/`for`/`while`)等。 2. **编译型语言**:两者均需编译为机器码后执行,性能接近底层硬件。 3. **内存操作**:均支持指针直接内存管理(如动态内存分配)。 4. **兼容性**:C++兼容大部分C代码(需注意少数例外,如C++要求函数原型必须声明)。 ```cpp // 示例:CC++共有的指针操作 int* ptr = (int*)malloc(sizeof(int)); // C风格(C++中也可用) *ptr = 42; free(ptr); ``` --- ### **主要区别**: 1. **编程范式**: - **C**:面向过程,以函数为核心。 - **C++**:支持面向对象(类、继承、多态)、泛型编程(模板)函数式编程。 ```cpp // C++面向对象示例 class Animal { public: virtual void sound() = 0; // 纯虚函数(多态性) }; class Dog : public Animal { public: void sound() override { std::cout << "Woof!"; } }; ``` 2. **标准库**: - **C**:仅提供基础库(如`stdio.h`、`stdlib.h`)。 - **C++**:包含STL(标准模板库),提供容器(`vector`、`map`)、算法(`sort`)等。 3. **异常处理**: - **C**:通过返回值或全局变量(如`errno`)处理错误。 - **C++**:支持`try`/`catch`异常机制。 4. **函数特性**: - **C++**支持函数重载、默认参数、引用传递(`int&`),而**C**不支持。 ```cpp // C++函数重载示例 void print(int x) { /*...*/ } void print(double x) { /*...*/ } ``` 5. **内存管理**: - **C**:使用`malloc`/`free`,需手动计算类型大小。 - **C++**:引入`new`/`delete`,自动计算类型大小并调用构造函数/析构函数。 --- ### **适用场景**: - **C**:嵌入式系统、操作系统内核等需要直接硬件操作的场景。 - **C++**:游戏开发、高性能计算、大型软件框架(如Unreal Engine)。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值