C++

C++基础

Posted by WK on 2025-04-10
Estimated Reading Time 3 Minutes
Words 853 In Total
Viewed Times

C++的基本语法

程序结构

  • #include:用于引入头文件。
  • main() 函数:程序入口点。
  • 注释:
    • 单行注释://
    • 多行注释:/* ... */
  • 输入输出:
    • 输出:std::cout
    • 输入:std::cin
  • 命名空间:
    • 使用 namespace 组织代码。
      1
      2
      3
      namespace myNamespace {
      void foo() { /* ... */ }
      }
    • 使用 using 指令简化访问:
      1
      using namespace std;

数据类型与变量

  • 基本数据类型:
    • 整型:int, short, long, long long
    • 浮点型:float, double, long double
    • 字符型:char, wchar_t, char16_t, char32_t
    • 布尔型:booltruefalse
  • 类型修饰符:
    • 符号修饰符:signed, unsigned
    • 存储修饰符:const, volatile
  • 变量声明与初始化:
    • 声明:int x;
    • 初始化:int x = 10;
    • 列表初始化(C++11):int x{10};
  • 自动类型推导(C++11 及以上):auto:根据初始值推导类型。
    1
    auto x = 10; // x 类型为 int
  • 常量:使用 const 定义常量。

运算符与表达式

  • 算术运算符:
    • 加减乘除:+, -, *, /
    • 取模:%
  • 比较运算符:
    • 比较大小:==, !=, >, <, >=, <=
  • 逻辑运算符:
    • 逻辑与:&&
    • 逻辑或:||
    • 逻辑非:!
  • 位运算符:
    • 按位与:&
    • 按位或:|
    • 按位异或:^
    • 左移:<<
    • 右移:>>
  • 赋值运算符:
    • 简单赋值:=
    • 复合赋值:+=, -=, *=, /=, %=
  • 其他运算符:
    • 条件运算符:? :
    • 逗号运算符:,

控制结构

  • 条件语句:
    • if-else
      1
      2
      3
      4
      5
      if (x > 0) {
      // ...
      } else {
      // ...
      }
    • switch-case
      1
      2
      3
      4
      switch (x) {
      case 1: /* ... */ break;
      default: /* ... */
      }
  • 循环语句:
    • for 循环:
      1
      for (int i = 0; i < 10; ++i) { /* ... */ }
    • while 循环:
      1
      while (x > 0) { /* ... */ }
    • do-while 循环:
      1
      do { /* ... */ } while (x > 0);
  • 跳转语句:
    • break:跳出当前循环或 switch
    • continue:跳过当前迭代,进入下一次循环。
    • goto:跳转到指定标签。
    • return:从函数返回。

函数

  • 函数定义:
    1
    2
    3
    int add(int a, int b) {
    return a + b;
    }
  • 默认参数:
    1
    2
    3
    void printMessage(std::string msg = "Hello") {
    std::cout << msg << std::endl;
    }
  • 函数重载:根据参数类型或数量区分同名函数。
    1
    2
    void foo(int x) { /* ... */ }
    void foo(double x) { /* ... */ }
  • 内联函数:使用 inline 提示编译器内联展开。
    1
    inline int square(int x) { return x * x; }
  • Lambda 表达式(C++11 及以上):
    1
    auto add = [](int a, int b) { return a + b; };

数组与指针

  • 数组:
    • 静态数组:int arr[5] = {1, 2, 3, 4, 5};
    • 动态数组(使用 newdelete):int* arr = new int[5]; delete[] arr;
  • 指针:
    • 定义指针:int* p;
    • 指针操作:
      1
      2
      3
      int x = 10;
      int* p = &x;
      *p = 20; // 修改 x 的值
  • 引用:引用是变量的别名:
    1
    2
    3
    int x = 10;
    int& ref = x;
    ref = 20; // 修改 x 的值

面向对象编程(OOP)

  • 类与对象:
    • 定义类:
      1
      2
      3
      4
      5
      class MyClass {
      public:
      int x;
      void print() { std::cout << x << std::endl; }
      };
  • 创建对象:
    1
    2
    3
    MyClass obj;
    obj.x = 10;
    obj.print();
  • 构造函数与析构函数:
    • 构造函数:MyClass(int value) : x(value) {}
    • 析构函数:~MyClass() { /* 清理资源 */ }
  • 继承:
    1
    2
    class Base { /* ... */ };
    class Derived : public Base { /* ... */ };
  • 多态:
    • 虚函数:virtual void foo() { /* ... */ }
    • 纯虚函数与抽象类:virtual void bar() = 0; // 纯虚函数
  • 访问控制:public, private, protected

异常处理

  • 抛出异常:throw std::runtime_error("Error message");
  • 捕获异常:
    1
    2
    3
    4
    5
    try {
    // 可能抛出异常的代码
    } catch (const std::exception& e) {
    std::cerr << e.what() << std::endl;
    }

其他高级特性

  • 模板:
    • 泛型编程:
      1
      2
      template <typename T>
      T add(T a, T b) { return a + b; }
  • 智能指针(C++11 及以上):
    • std::unique_ptr, std::shared_ptr, std::weak_ptr
  • 多线程(C++11 及以上):
    • 使用 std::thread 创建线程。
    • 同步机制:std::mutex, std::lock_guard

If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !