C++系统化自学路线及阶段代码实战

一、基础语法阶段(3-4周)

1.1 开发环境搭建

  • 安装G++编译器(推荐MinGW-w64/MSYS2)

  • 配置VS Code/CLion开发环境

  • 掌握CMake基础语法

1.2 核心语法要素

cpp

复制

// 基础类型与面向对象雏形
#include <iostream>
using namespace std;

int main() {
    // 基本类型与自动推导
    int num = 42;
    auto pi = 3.14159;
    const char* msg = "Hello C++";
    
    // 范围for循环
    int arr[] {5,4,3,2,1};
    for(auto& n : arr) {
        n *= 2;
    }
    
    // 函数重载
    cout << "最大值:" << max(3.14, 2.71) << endl;
    cout << "字符串长:" << max("apple", "banana") << endl;
    return 0;
}

// 函数重载示例
template<typename T>
T max(T a, T b) { return (a > b) ? a : b; }

template<>
const char* max(const char* a, const char* b) {
    return (strlen(a) > strlen(b)) ? a : b;
}

1.3 类与对象基础

cpp

复制

// 银行账户类
class BankAccount {
private:
    string owner;
    double balance;
    
public:
    BankAccount(string name, double init = 0)
        : owner(name), balance(init) {}
        
    void deposit(double amount) {
        if(amount > 0) balance += amount;
    }
    
    bool withdraw(double amount) {
        if(amount <= balance) {
            balance -= amount;
            return true;
        }
        return false;
    }
    
    void print() const {
        cout << owner << "的余额:" << balance << endl;
    }
};

int main() {
    BankAccount alice("Alice", 1000);
    alice.deposit(500);
    alice.withdraw(200);
    alice.print(); // 输出:Alice的余额:1300
    return 0;
}

二、面向对象阶段(4-5周)

2.1 继承与多态

cpp

复制

// 图形类层次
class Shape {
protected:
    string color;
public:
    Shape(string c) : color(c) {}
    virtual double area() const = 0;
    virtual void draw() const {
        cout << "绘制" << color << "图形" << endl;
    }
    virtual ~Shape() = default;
};

class Circle : public Shape {
    double radius;
public:
    Circle(string c, double r) : Shape(c), radius(r) {}
    
    double area() const override {
        return 3.14159 * radius * radius;
    }
    
    void draw() const override {
        cout << "绘制" << color << "圆形,半径" << radius << endl;
    }
};

// 使用示例
void printArea(const Shape& s) {
    cout << "面积:" << s.area() << endl;
}

int main() {
    Circle redCircle("红色", 5.0);
    redCircle.draw();
    printArea(redCircle); // 输出:78.5397
    return 0;
}

2.2 运算符重载

cpp

复制

// 向量类重载
class Vector3D {
    double x, y, z;
public:
    Vector3D(double a=0, double b=0, double c=0) 
        : x(a), y(b), z(c) {}
        
    Vector3D operator+(const Vector3D& rhs) const {
        return Vector3D(x+rhs.x, y+rhs.y, z+rhs.z);
    }
    
    Vector3D& operator+=(const Vector3D& rhs) {
        x += rhs.x;
        y += rhs.y;
        z += rhs.z;
        return *this;
    }
    
    friend ostream& operator<<(ostream& os, const Vector3D& v);
};

ostream& operator<<(ostream& os, const Vector3D& v) {
    return os << "(" << v.x << ", " << v.y << ", " << v.z << ")";
}

int main() {
    Vector3D v1(1,2,3), v2(4,5,6);
    auto v3 = v1 + v2;
    v1 += v2;
    cout << "v3=" << v3 << endl;  // (5,7,9)
    cout << "v1=" << v1 << endl;  // (5,7,9)
    return 0;
}

三、标准库与模板阶段(5-6周)

3.1 STL容器与算法


https://loldashi.github.io

cpp

复制

#include <vector>
#include <algorithm>
#include <unordered_map>

void stlDemo() {
    // 向量与排序
    vector<int> nums {3,1,4,1,5,9,2,6};
    sort(nums.begin(), nums.end(), greater<int>());
    
    // 哈希表统计词频
    unordered_map<string, int> wordCount;
    vector<string> words {"apple", "banana", "apple", "orange"};
    for(auto& word : words) {
        wordCount[word]++;
    }
    
    // Lambda表达式遍历
    for_each(nums.begin(), nums.end(), [](int n) {
        cout << n << " "; // 输出:9 6 5 4 3 2 1 1 
    });
}

3.2 模板元编程

LoL换肤助手官方网站_LOL换肤_兔子换肤_UU换肤_英雄联盟换肤最新版

cpp

复制

// 编译期阶乘计算
template<int N>
struct Factorial {
    static const int value = N * Factorial<N-1>::value;
};

template<>
struct Factorial<0> {
    static const int value = 1;
};

// 类型萃取示例
template<typename T>
void printType() {
    if constexpr(is_pointer<T>::value) {
        cout << "指针类型" << endl;
    } else if constexpr(is_integral<T>::value) {
        cout << "整型" << endl;
    } else {
        cout << "其他类型" << endl;
    }
}

int main() {
    cout << Factorial<5>::value << endl; // 120
    printType<int*>();   // 指针类型
    printType<double>(); // 其他类型
    return 0;
}

四、高级特性阶段(6-8周)

4.1 智能指针与RAII

LOL换肤助手官方网站-LOL换肤助手_lol换肤盒子_LOL兔子换肤最新版

cpp

复制

#include <memory>

class FileHandler {
    unique_ptr<FILE, decltype(&fclose)> file;
public:
    FileHandler(const char* name, const char* mode)
        : file(fopen(name, mode), &fclose) {}
        
    void write(const string& data) {
        if(file) fputs(data.c_str(), file.get());
    }
};

void raiiDemo() {
    FileHandler f("data.txt", "w");
    f.write("RAII示例数据");
    // 自动关闭文件
}

4.2 并发编程

cpp

复制

#include <thread>
#include <mutex>
#include <future>

mutex mtx;

void threadTask(int id) {
    lock_guard<mutex> lock(mtx);
    cout << "线程" << id << "运行" << endl;
}

asyncDemo() {
    auto future = async(launch::async, [](){
        this_thread::sleep_for(1s);
        return 3.14159;
    });
    cout << "异步结果:" << future.get() << endl;
}

int main() {
    thread t1(threadTask, 1);
    thread t2(threadTask, 2);
    t1.join();
    t2.join();
    asyncDemo();
    return 0;
}

五、项目实战阶段(持续进行)

5.1 简易游戏引擎

LOL换肤大师官方网站-LOL换肤助手_lol换肤盒子_LOL兔子换肤最新版

cpp

复制

// 游戏对象基类
class GameObject {
protected:
    float x, y;
public:
    GameObject(float x, float y) : x(x), y(y) {}
    virtual void update(float delta) = 0;
    virtual void render() = 0;
    virtual ~GameObject() = default;
};

// 玩家角色类
class Player : public GameObject {
    float speed = 5.0f;
public:
    Player() : GameObject(0,0) {}
    
    void update(float delta) override {
        // 处理输入移动
        x += speed * delta;
    }
    
    void render() override {
        cout << "渲染玩家位置:(" << x << ", " << y << ")\n";
    }
};

// 游戏循环
void gameLoop() {
    unique_ptr<GameObject> player = make_unique<Player>();
    while(true) {
        player->update(0.016f); // 60FPS
        player->render();
        this_thread::sleep_for(16ms);
    }
}

5.2 数据结构实现

LoL换肤魔盒官方网站_LOL换肤_LOLuu换肤_LOL换肤大师_LOL换肤盒子

cpp

复制

// 模板化双向链表
template<typename T>
class DoublyLinkedList {
    struct Node {
        T data;
        Node* prev;
        Node* next;
        Node(const T& d) : data(d), prev(nullptr), next(nullptr) {}
    };
    
    Node* head = nullptr;
    Node* tail = nullptr;
    
public:
    void append(const T& data) {
        Node* newNode = new Node(data);
        if(!head) {
            head = tail = newNode;
        } else {
            tail->next = newNode;
            newNode->prev = tail;
            tail = newNode;
        }
    }
    
    void print() const {
        Node* current = head;
        while(current) {
            cout << current->data << " ";
            current = current->next;
        }
        cout << endl;
    }
    
    ~DoublyLinkedList() {
        while(head) {
            Node* temp = head;
            head = head->next;
            delete temp;
        }
    }
};

学习路线总结

  1. 工具链建设

    • 掌握GDB调试技巧

    • 使用Clang-Tidy进行代码检查

    • 集成Google Test单元测试框架

  2. 现代C++重点

    • C++11/14/17新特性(lambda、移动语义、constexpr)

    • 掌握RAII设计模式

    • 理解右值引用与完美转发

  3. 性能优化方向

    • 学习缓存友好设计

    • 掌握多线程同步机制

    • 研究内存池实现原理

  4. 推荐实践项目

    • 实现STL容器(Vector/HashMap)

    • 开发简易HTTP服务器

    • 编写3D数学库(矩阵/四元数)

    • 创建脚本语言解释器

学习周期建议:每天保持3小时有效学习,8个月可达到中级开发水平。重点攻克面向对象设计、模板元编程、内存管理三大核心领域,通过开发图形应用或游戏引擎等实践项目深化理解。建议同步学习《Effective C++》《C++ Primer》等经典著作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值