C++重要知识点代码示例

C++语言设计期末知识点附示例代码。

1. 基础语法

  • 变量和数据类型

    int a = 10;       // 整型
    float b = 5.25f;  // 单精度浮点型
    double c = 5.25;  // 双精度浮点型
    char d = 'A';     // 字符型
    bool e = true;    // 布尔型
    const int PI = 3.14;  // 常量
    
  • 输入输出

    #include <iostream>
    using namespace std;
    
    int main() {
        int num;
        cout << "Enter a number: ";
        cin >> num;
        cout << "You entered: " << num << endl;
        return 0;
    }
    

2. 流程控制

  • 条件语句

    if (a > b) {
        cout << "a is greater than b";
    } else if (a == b) {
        cout << "a is equal to b";
    } else {
        cout << "a is less than b";
    }
    
    switch (a) {
        case 1: 
            cout << "a is 1"; 
            break;
        case 2: 
            cout << "a is 2"; 
            break;
        default: 
            cout << "a is neither 1 nor 2";
    }
    
  • 循环语句

    // for loop
    for (int i = 0; i < 5; ++i) {
        cout << i << " ";
    }
    
    // while loop
    int i = 0;
    while (i < 5) {
        cout << i << " ";
        ++i;
    }
    
    // do-while loop
    int j = 0;
    do {
        cout << j << " ";
        ++j;
    } while (j < 5);
    

3. 函数

  • 函数定义与声明

    int add(int a, int b);  // 函数声明
    
    int main() {
        cout << add(5, 3);  // 调用函数
        return 0;
    }
    
    int add(int a, int b) {  // 函数定义
        return a + b;
    }
    
  • 函数重载

    int add(int a, int b) { return a + b; }
    double add(double a, double b) { return a + b; }
    
    int main() {
        cout << add(5, 3) << endl;          // 调用 int 版本
        cout << add(5.0, 3.0) << endl;      // 调用 double 版本
        return 0;
    }
    
  • 递归

    int factorial(int n) {
        if (n == 1) return 1;
        return n * factorial(n - 1);
    }
    
    int main() {
        cout << factorial(5);  // 输出 120
        return 0;
    }
    

4. 数组和指针

  • 数组

    int arr[5] = {1, 2, 3, 4, 5};  // 一维数组
    
    for (int i = 0; i < 5; ++i) {
        cout << arr[i] << " ";
    }
    
    int arr2D[2][3] = {{1, 2, 3}, {4, 5, 6}};  // 二维数组
    
  • 指针

    int var = 10;
    int *ptr = &var;  // 指针指向变量的地址
    
    cout << "Value of var: " << *ptr << endl;  // 输出指针指向的值
    
  • 动态内存分配

    int *p = new int[5];  // 动态分配一个长度为 5 的整数数组
    delete[] p;           // 释放内存
    

5. 面向对象编程(OOP)

  • 类与对象

    class Person {
    private:
        string name;
        int age;
    
    public:
        // 构造函数
        Person(string n, int a) : name(n), age(a) {}
    
        void display() {
            cout << "Name: " << name << ", Age: " << age << endl;
        }
    };
    
    int main() {
        Person p("John", 25);
        p.display();
        return 0;
    }
    
  • 继承

    class Animal {
    public:
        void eat() { cout << "Animal is eating" << endl; }
    };
    
    class Dog : public Animal {  // 公有继承
    public:
        void bark() { cout << "Dog is barking" << endl; }
    };
    
    int main() {
        Dog d;
        d.eat();  // 基类的方法
        d.bark(); // 派生类的方法
        return 0;
    }
    
  • 多态性

    class Animal {
    public:
        virtual void sound() { cout << "Some animal sound" << endl; }
    };
    
    class Dog : public Animal {
    public:
        void sound() override { cout << "Bark" << endl; }
    };
    
    int main() {
        Animal* a = new Dog();
        a->sound();  // 输出 "Bark"
        delete a;
        return 0;
    }
    

6. 运算符重载

  • 加法运算符重载
    class Complex {
    private:
        int real, imag;
    
    public:
        Complex(int r = 0, int i = 0) : real(r), imag(i) {}
    
        Complex operator + (const Complex& obj) {
            Complex temp;
            temp.real = real + obj.real;
            temp.imag = imag + obj.imag;
            return temp;
        }
    
        void display() { cout << real << " + i" << imag << endl; }
    };
    
    int main() {
        Complex c1(5, 3), c2(2, 4);
        Complex c3 = c1 + c2;
        c3.display();  // 输出 "7 + i7"
        return 0;
    }
    

7. 标准模板库(STL)

  • vector 使用
    #include <iostream>
    #include <vector>
    using namespace std;
    
    int main() {
        vector<int> vec = {1, 2, 3, 4, 5};
        vec.push_back(6);  // 添加元素
    
        for (int i : vec) {
            cout << i << " ";  // 遍历 vector
        }
    
        return 0;
    }
    

8. 异常处理

  • 异常捕获
    #include <iostream>
    using namespace std;
    
    int main() {
        try {
            int age = -1;
            if (age < 0) {
                throw "Invalid age!";
            }
        } catch (const char* msg) {
            cout << "Error: " << msg << endl;
        }
    
        return 0;
    }
    

9. 文件操作

  • 文件读写
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main() {
        // 写入文件
        ofstream outFile("example.txt");
        outFile << "Hello, World!" << endl;
        outFile.close();
    
        // 从文件读取
        string line;
        ifstream inFile("example.txt");
        if (inFile.is_open()) {
            while (getline(inFile, line)) {
                cout << line << endl;
            }
            inFile.close();
        }
        return 0;
    }
    

10. 预处理器与宏

  • 宏与条件编译
    #define PI 3.14
    
    #include <iostream>
    using namespace std;
    
    int main() {
        cout << "PI is: " << PI << endl;
        return 0;
    }
    
    #ifdef DEBUG
        cout << "Debug mode" << endl;
    #endif
    

11. 名字空间(namespace)

  • 使用名字空间
    #include <iostream>
    using namespace std;
    
    namespace myNamespace {
        int x = 10;
    }
    
    int main() {
        cout << myNamespace::x << endl;
        return 0;
    }
    

12. 动态内存管理

  • 智能指针
    #include <iostream>
    #include <memory>
    using namespace std;
    
    int main() {
          shared_ptr<int> p1 = make_shared<int>(10);
          cout << *p1 << endl;
          return 0;
      }
    
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

码上有前

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

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

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

打赏作者

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

抵扣说明:

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

余额充值