C++基础知识(上)

这篇博客主要介绍了C++的基础知识,包括C++的关键字、命名空间的定义与使用、输入输出、缺省参数、函数重载的概念及名字修饰、extern "C"的作用,以及引用的概念、特性、使用场景和与指针的区别。
摘要由CSDN通过智能技术生成

1.C++关键字

C++总计63个关键字,C语言32个关键字

大概了解一下C++的关键字都有哪些

asm do if return try continue
auto double inline short typedef for
bool dynamic_cast int signed typeid public
break else long sizeof typename throw
case enum mutable static union wchar_t
catch explicit namespace static_cast unsigned default
char export new struct using friend
class extern operator switch virtual register
const false private template void true
const_cast float protected this volatile while
delete goto reinterpret_cast

2. 命名空间

在C/C++中,变量、函数和后面要学到的类都是大量存在的,这些变量、函数和类的名称将都存在于全局作用域中,可能会导致很多冲突。使用命名空间的目的是对标识符的名称进行本地化以避免命名冲突或名字污染,namespace关键字的出现就是针对这种问题的。

2.1 命名空间定义

定义命名空间,需要使用到namespace关键字,后面跟命名空间的名字,然后接一对{}即可,{}中即为命名空间的成员。

1.普通的命名空间

namespace N1 //N1为命名空间的内容
{
    //命名空间中既可以定义变量,也可以定义函数
	int a;
	int add(int left, int right)
	{
		return left + right;
	}
}

2.命名空间可以嵌套

namespace N2
{
	int i;
	double d;
	int Add(int x, int y)
	{
		return x + y;
	}
	namespace N3
	{
		int a;
		int b;
		int Sub(int x, int y)
		{
			return x - y;
		}
	}
}

3. 同一个工程中允许存在多个相同名称的命名空间,编译器最后会合成到同一个命名空间中。

namespace N1
{
	int a;
	int add(int left, int right)
	{
		return left + right;
	}
}

namespace N1
{
	int Mul(int x, int y)
	{
		return x + y;
	}
}
  • 34
    点赞
  • 114
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 16
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

闻 术

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

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

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

打赏作者

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

抵扣说明:

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

余额充值