C++ 是一门功能强大的通用编程语言,广泛应用于系统软件、游戏开发、嵌入式系统和高性能计算等领域。本文将带你快速入门 C++ 编程。
什么是 C++?
C++ 由 Bjarne Stroustrup 于 1979 年在贝尔实验室开发,是 C 语言的扩展。它支持:
- 面向过程编程:继承自 C 语言
- 面向对象编程:类、继承、多态、封装
- 泛型编程:模板(Templates)
- 函数式编程:Lambda 表达式(C++11)
环境配置
1. 安装编译器
**Linux (Ubuntu/Debian)**:
1 2
| sudo apt update sudo apt install g++ build-essential
|
macOS:
Windows:
2. 验证安装
第一个 C++ 程序
创建文件 hello.cpp:
1 2 3 4 5 6
| #include <iostream>
int main() { std::cout << "Hello, World!" << std::endl; return 0; }
|
编译并运行:
1 2
| g++ hello.cpp -o hello ./hello
|
基本语法
变量与数据类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| #include <iostream> #include <string>
int main() { int age = 25; double price = 99.99; float rate = 3.14f; char grade = 'A'; bool isActive = true; std::string name = "张三"; std::cout << "姓名: " << name << std::endl; std::cout << "年龄: " << age << std::endl; return 0; }
|
控制流语句
条件语句:
1 2 3 4 5 6 7 8 9
| int score = 85;
if (score >= 90) { std::cout << "优秀" << std::endl; } else if (score >= 60) { std::cout << "及格" << std::endl; } else { std::cout << "不及格" << std::endl; }
|
循环语句:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| for (int i = 0; i < 5; i++) { std::cout << i << " "; }
int j = 0; while (j < 5) { std::cout << j << " "; j++; }
int arr[] = {1, 2, 3, 4, 5}; for (int num : arr) { std::cout << num << " "; }
|
函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #include <iostream>
int add(int a, int b); void greet(std::string name);
int main() { int result = add(3, 5); std::cout << "3 + 5 = " << result << std::endl; greet("小明"); return 0; }
int add(int a, int b) { return a + b; }
void greet(std::string name) { std::cout << "你好, " << name << "!" << std::endl; }
|
面向对象编程
类与对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| #include <iostream> #include <string>
class Person { private: std::string name; int age;
public: Person(std::string n, int a) : name(n), age(a) {} void introduce() { std::cout << "我是 " << name << ",今年 " << age << " 岁。" << std::endl; } std::string getName() { return name; } int getAge() { return age; } void setAge(int a) { age = a; } };
int main() { Person p1("张三", 25); p1.introduce(); p1.setAge(26); std::cout << "明年 " << p1.getName() << " 就 " << p1.getAge() << " 岁了。" << std::endl; return 0; }
|
继承
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| #include <iostream>
class Animal { protected: std::string name; public: Animal(std::string n) : name(n) {} virtual void speak() { std::cout << name << " 发出声音" << std::endl; } };
class Dog : public Animal { public: Dog(std::string n) : Animal(n) {} void speak() override { std::cout << name << " 汪汪叫" << std::endl; } };
class Cat : public Animal { public: Cat(std::string n) : Animal(n) {} void speak() override { std::cout << name << " 喵喵叫" << std::endl; } };
int main() { Dog dog("旺财"); Cat cat("咪咪"); dog.speak(); cat.speak(); return 0; }
|
现代 C++ 特性 (C++11/14/17/20)
智能指针
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| #include <iostream> #include <memory>
class MyClass { public: MyClass() { std::cout << "构造" << std::endl; } ~MyClass() { std::cout << "析构" << std::endl; } void hello() { std::cout << "Hello!" << std::endl; } };
int main() { std::unique_ptr<MyClass> ptr1 = std::make_unique<MyClass>(); ptr1->hello(); std::shared_ptr<MyClass> ptr2 = std::make_shared<MyClass>(); std::shared_ptr<MyClass> ptr3 = ptr2; return 0; }
|
Lambda 表达式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| #include <iostream> #include <vector> #include <algorithm>
int main() { std::vector<int> nums = {3, 1, 4, 1, 5, 9, 2, 6}; std::sort(nums.begin(), nums.end(), [](int a, int b) { return a > b; }); std::for_each(nums.begin(), nums.end(), [](int n) { std::cout << n << " "; }); return 0; }
|
auto 关键字
1 2 3 4 5 6 7 8
| auto x = 10; auto y = 3.14; auto str = "hello";
std::vector<int> vec = {1, 2, 3}; for (auto& num : vec) { num *= 2; }
|
常用标准库
| 头文件 |
功能 |
<iostream> |
输入输出流 |
<string> |
字符串处理 |
<vector> |
动态数组 |
<map> |
键值对容器 |
<algorithm> |
常用算法 |
<memory> |
智能指针 |
<thread> |
多线程 |
<fstream> |
文件操作 |
编译选项建议
1 2 3 4 5
| g++ -std=c++17 -Wall -Wextra -o program source.cpp
g++ -std=c++17 -O2 -o program source.cpp
|
学习资源
总结
C++ 是一门功能强大但学习曲线较陡的语言。建议学习路线:
- 基础语法 → 变量、控制流、函数
- 面向对象 → 类、继承、多态
- 内存管理 → 指针、引用、智能指针
- 标准库 → STL 容器和算法
- 现代特性 → C++11/14/17/20 新特性
掌握这些基础后,你就可以开始编写实际的 C++ 项目了!