目录

  1. Hello World 示例
  2. 变量和数据类型
  3. 运算符示例
  4. 控制流(判断 & 循环)
  5. 函数示例
  6. 数组与字符串
  7. 指针与引用
  8. 结构体与类
  9. 继承与多态
  10. 模板与泛型编程
  11. 文件操作
  12. STL 容器与算法
  13. 异常处理
  14. 多线程
  15. C++ Web 编程示例

1. Hello World 示例

🔗 C++ Hello World 教程

#include <iostream>
using namespace std;

int main() {
    cout << "Hello, World!" << endl;
    return 0;
}


2. 变量和数据类型

🔗 C++ 数据类型

#include <iostream>
using namespace std;

int main() {
    int a = 10;
    double b = 20.5;
    char c = 'A';
    bool d = true;
    
    cout << "整数: " << a << "\n小数: " << b << "\n字符: " << c << "\n布尔值: " << d << endl;
    return 0;
}


3. 运算符示例

🔗 C++ 运算符

#include <iostream>
using namespace std;

int main() {
    int a = 10, b = 3;
    
    cout << "加法: " << (a + b) << endl;
    cout << "减法: " << (a - b) << endl;
    cout << "乘法: " << (a * b) << endl;
    cout << "除法: " << (a / b) << endl;
    cout << "取余: " << (a % b) << endl;
    
    return 0;
}


4. 控制流(判断 & 循环)

🔗 C++ if-else & for 循环

(1) if-else 语句

#include <iostream>
using namespace std;

int main() {
    int num;
    cout << "输入一个整数: ";
    cin >> num;
    
    if (num % 2 == 0)
        cout << "偶数" << endl;
    else
        cout << "奇数" << endl;

    return 0;
}

(2) for 循环

#include <iostream>
using namespace std;

int main() {
    for (int i = 1; i <= 5; i++) {
        cout << "第 " << i << " 次迭代" << endl;
    }
    return 0;
}


5. 函数示例

🔗 C++ 函数

#include <iostream>
using namespace std;

int add(int a, int b) {
    return a + b;
}

int main() {
    cout << "5 + 10 = " << add(5, 10) << endl;
    return 0;
}


6. 数组与字符串

🔗 C++ 数组 & 字符串

#include <iostream>
using namespace std;

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    for (int i = 0; i < 5; i++) {
        cout << "数组元素[" << i << "]: " << arr[i] << endl;
    }
    
    string str = "Hello C++";
    cout << "字符串: " << str << endl;
    
    return 0;
}


7. 指针与引用

🔗 C++ 指针

#include <iostream>
using namespace std;

int main() {
    int num = 10;
    int* ptr = &num;
    
    cout << "变量值: " << num << endl;
    cout << "指针指向的值: " << *ptr << endl;
    
    return 0;
}


8. 结构体与类

🔗 C++ 结构体 & 类

#include <iostream>
using namespace std;

struct Person {
    string name;
    int age;
};

int main() {
    Person p1 = {"Alice", 25};
    cout << "姓名: " << p1.name << ", 年龄: " << p1.age << endl;
    return 0;
}


9. 继承与多态

🔗 C++ 继承与多态

#include <iostream>
using namespace std;

class Animal {
public:
    virtual void makeSound() {
        cout << "动物叫声" << endl;
    }
};

class Dog : public Animal {
public:
    void makeSound() override {
        cout << "汪汪" << endl;
    }
};

int main() {
    Animal* a = new Dog();
    a->makeSound();
    
    delete a;
    return 0;
}


10. 模板与泛型编程

🔗 C++ 模板

#include <iostream>
using namespace std;

template <typename T>
T add(T a, T b) {
    return a + b;
}

int main() {
    cout << "整数相加: " << add(5, 10) << endl;
    cout << "浮点数相加: " << add(2.5, 3.7) << endl;
    return 0;
}


15. C++ Web 编程示例

🔗 C++ RESTful Web 服务器

#include <iostream>
#include <cpprest/http_listener.h>
using namespace web;
using namespace http;
using namespace http::experimental::listener;

void handle_request(http_request request) {
    request.reply(status_codes::OK, "Hello, C++ Web Server!");
}

int main() {
    http_listener listener("http://localhost:8080");
    listener.support(methods::GET, handle_request);
    
    listener.open().wait();
    cout << "服务器运行中..." << endl;
    
    string exitCommand;
    cin >> exitCommand;
    
    listener.close().wait();
    return 0;
}


参考资料