C++基础语法详解:从命名空间到内联函数
C++作为C语言的扩展,不仅保留了C语言的特性,还引入了许多强大的新特性。本文将详细介绍C++中的核心概念,包括命名空间、输入输出流、缺省参数、函数重载、引用、const关键字、内联函数和nullptr等。
一、namespace命名空间
命名空间是C++中解决命名冲突的重要机制,可以将变量、函数、结构体等封装在特定的作用域内。
基本定义
namespace yzq {// 定义变量int a = 10;// 定义函数int sum(int a, int b) {return a + b;}// 定义结构体struct Student {char name[10];int age;int sex;};// 定义类class A {}; }
访问方式
#include <iostream> using namespace std;int main() {// 1. 指定命名空间访问cout << yzq::a << endl;// 2. 展开某个命名空间成员using yzq::a;cout << a << endl;// 3. 展开整个命名空间(不推荐)using namespace yzq;cout << a << endl;return 0; }
嵌套命名空间
namespace yzq {char name[10] = "hhh";int age = 10;namespace xrf {char name[10] = "xxx";int age = 10;} }// 访问嵌套命名空间 cout << yzq::name << endl; // 输出: hhh cout << yzq::xrf::name << endl; // 输出: xxx
二、输入输出流
C++使用cin
和cout
进行输入输出操作,比C语言的scanf
和printf
更加类型安全。
#include <iostream> using namespace std;int main() {int x;cin >> x; // 输入cout << x << endl; // 输出int a, b;cin >> a >> b; // 一次输入多个变量cout << a << " " << b << endl;return 0; }
std::cin
:标准输入流(istream类对象)std::cout
:标准输出流(ostream类对象)std::endl
:插入换行符并刷新缓冲区
三、缺省参数
缺省参数允许在函数声明时为参数指定默认值。
全缺省参数
int fun(int a = 10, int b = 20) {return a + b; }cout << fun() << endl; // 输出: 30 cout << fun(5) << endl; // 输出: 25 cout << fun(5, 15) << endl; // 输出: 20
半缺省参数
int fun(int a, int b = 10) {return a + b; }cout << fun(2) << endl; // 输出: 12 cout << fun(2, 5) << endl; // 输出: 7
注意:半缺省参数必须从右往左依次连续缺省,不能间隔。
四、函数重载
C++支持在同一作用域中定义同名函数,只要它们的参数个数或类型不同。
#include <iostream> using namespace std;void fun(int a, int b) {cout << "fun1()" << endl; }// 形参类型不同 void fun(double a, double b) {cout << "fun2()" << endl; }// 形参个数不同 void fun(int a) {cout << "fun3()" << endl; }int main() {fun(2, 3); // 调用第一个funfun(2.0, 3.0); // 调用第二个funfun(2); // 调用第三个funreturn 0; }
五、引用
引用是变量的别名,与原始变量共享同一块内存空间。
基本使用
#include <iostream> using namespace std;int main() {int a = 10;int b = 20;int& a1 = a; // a1是a的别名int& b1 = b;b1 = 50; // 修改b1相当于修改bcout << "a=" << a << " b=" << b << endl; // 输出: a=10 b=50cout << "a=" << a1 << " b=" << b1 << endl; // 输出: a=10 b=50int& a2 = a1; // 对别名再取别名cout << "a=" << a2 << endl; // 输出: a=10return 0; }
引用特性
定义时必须初始化
一个变量可以有多个引用
引用一旦绑定实体,不能再引用其他实体
const引用
const int a = 10; // int& a1 = a; // 错误:权限放大 const int& a1 = a; // 正确:权限保持int b = 20; const int& b1 = b; // 正确:权限缩小 // b1 = 10; // 错误:不能通过const引用修改值// 临时变量具有常性,必须用const引用 const int& sum = a + b; // 正确 // int& sum = a + b; // 错误
六、const关键字
在C++中,const修饰的变量是真正的常量,可以直接用于定义数组长度等场景。
#include <iostream> using namespace std;int main() {const int n = 10;int b[n]; // 可以直接定义数组for (int i = 0; i < n; i++) {b[i] = i;cout << b[i] << endl;}return 0; }
七、内联函数
使用inline
修饰的函数称为内联函数,编译器会在调用处展开函数体,避免函数调用的开销。
#include <iostream> using namespace std;inline int Add(int a, int b) {return a + b; }int main() {// 在编译时可能会被展开为:cout << 30 << endl;cout << Add(10, 20) << endl;return 0; }
注意:
inline
只是建议,编译器可能忽略适用于短小且频繁调用的函数
不建议且也不能将声明和定义分离到不同文件
八、nullptr
C++11引入nullptr
来解决NULL的歧义问题。
#include <iostream> using namespace std;void fun(int x) {cout << "fun1(int x)" << endl; }void fun(int* ptr) {cout << "fun2(int* ptr)" << endl; }int main() {fun(0); // 调用fun(int)fun(NULL); // 可能调用fun(int),产生歧义fun((int*)NULL); // 调用fun(int*)fun(nullptr); // 明确调用fun(int*)return 0; }