C++ 是一门由 Bjarne Stroustrup 于 1985 年在贝尔实验室开发的编程语言。它是 C 语言的扩展,加入了面向对象编程的特性,同时保持了 C 语言的高性能和灵活性。
C++ 概述
C++ 的特点
- 高性能:接近 C 语言的执行效率
- 面向对象:支持类、继承、多态
- 泛型编程:模板机制
- 标准库丰富:STL(标准模板库)
- 向后兼容:兼容大部分 C 语言代码
C++ 的应用场景
- 系统软件和操作系统开发
- 游戏引擎开发(Unreal Engine)
- 高性能服务器
- 嵌入式系统
- 图形图像处理
第一个 C++ 程序
1 2 3 4 5 6
| #include <iostream> using namespace std;
int main() { cout << "Hello, C++!" << endl; return 0;
|
代码解释:
#include <iostream>:包含输入输出流库
using namespace std;:使用标准命名空间
cout <<:输出到控制台
endl:换行
类与对象
类的定义
1 2 3 4 5 6 7 8 9 10 11 12 13
| class Person { private: string name; int age; public: Person(string n, int a) : name(n), age(a) {} void introduce() { cout << "我叫" << name << ",今年" << age << "岁。" << endl; } int getAge() const { return age; } void setAge(int a) { age = a; } };
|
对象的创建与使用
1 2 3 4 5 6 7 8
| Person p1("张三", 25); p1.introduce();
Person *p2 = new Person("李四", 30); p2->introduce(); delete p2;
cout << p1.getAge() << endl;
|
继承
继承允许一个类(子类)获取另一个类(父类)的属性和方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| class Student : public Person { private: string school; public: Student(string n, int a, string s) : Person(n, a), school(s) {} void study() { cout << name << "在" << school << "学习" << endl; } };
Student stu("王五", 20, "清华大学"); stu.introduce(); stu.study(); stu.setAge(21);
|
多态
多态允许不同类的对象对同一消息做出不同的响应。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| class Shape { public: virtual double getArea() = 0; virtual ~Shape() = default; };
class Circle : public Shape { private: double radius; public: double getArea() override { return 3.14159 * radius * radius; } };
Shape *shape = new Circle(5.0); cout << shape->getArea() << endl; delete shape;
|
模板
模板是泛型编程的基础,允许编写与类型无关的代码。
函数模板
1 2 3 4 5 6 7
| template <typename T> T max(T a, T b) { return a > b ? a : b; }
int x = max(3, 5); double y = max(2.5, 1.8);
|
类模板
1 2 3 4 5 6 7 8 9 10
| template <typename T> class Stack { private: vector<T> elements; public: void push(T elem) { elements.push_back(elem); } T pop() { T elem = elements.back(); elements.pop_back(); return elem; } };
Stack<int> intStack;
|
STL 标准模板库
容器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| #include <vector> #include <map> #include <set> #include <string>
vector<int> vec = {1, 2, 3}; vec.push_back(4); for (int num : vec) { cout << num << " "; }
map<string, int> dict; dict["apple"] = 5; cout << dict["apple"] << endl;
|
算法
1 2 3 4 5 6 7 8
| #include <algorithm>
vector<int> arr = {3, 1, 4, 1, 5};
sort(arr.begin(), arr.end()); int cnt = count(arr.begin(), arr.end(), 1); auto it = find(arr.begin(), arr.end(), 4);
|
智能指针
智能指针自动管理内存,避免内存泄漏。
1 2 3 4 5 6 7 8 9 10 11 12
| #include <memory>
unique_ptr<Person> p1 = make_unique<Person>("张三", 25); p1->introduce();
shared_ptr<Person> p2 = make_shared<Person>("李四", 30); shared_ptr<Person> p3 = p2; cout << p2.use_count() << endl;
weak_ptr<Person> p4 = p2;
|
异常处理
1 2 3 4 5 6 7 8 9 10 11
| try { int result = 10 / 0; } catch (exception &e) { cout << "错误: " << e.what() << endl; } catch (...) { cout << "未知错误" << endl; }
throw runtime_error("自定义错误");
|
Lambda 表达式
1 2 3 4 5 6 7
| auto add = [](int a, int b) { return a + b; }; cout << add(3, 5) << endl;
vector<int> nums = {1, 2, 3, 4, 5}; sort(nums.begin(), nums.end(), [](int a, int b) { return a > b; });
|
总结
本文介绍了 C++ 的核心特性,包括:
- 类与对象
- 继承
- 多态
- 模板
- STL 标准模板库
- 智能指针
- 异常处理
- Lambda 表达式
C++ 是一门功能强大的编程语言,适合需要高性能和面向对象特性的场景。掌握 C++ 需要一定的时间和实践,但它会成为你编程工具箱中非常有价值的工具。