C++ 是一门横跨数十年的强大编程语言,融合了过程式和面向对象编程范式,在现代软件工程中仍扮演着重要角色。它被广泛应用于系统软件、嵌入式设备、游戏引擎、图形处理、数据库系统等高性能领域。相比 Python、Java 等现代语言,C++ 更加贴近硬件,掌控力强,适合对性能要求极高的系统开发。
本文将带你全面了解 C++ 的基本语法、类与对象、标准模板库 STL、内存管理、高级语言特性,并通过一个简易控制台项目加以实践,构建起一个完整的知识体系。
C++ 使用 cin
和 cout
进行标准输入输出操作:
cpp复制编辑#include <iostream>
using namespace std;
int main() {
int x;
cout << "Enter a number: ";
cin >> x;
cout << "You entered: " << x << endl;
return 0;
}
控制结构包括 if-else
、switch
、for
、while
、do-while
等:
cpp复制编辑for (int i = 0; i < 5; ++i) {
cout << i << " ";
}
C++ 的最大亮点在于其面向对象特性,支持类(Class)、封装、继承和多态。
cpp复制编辑class Box {
private:
double length, width, height;
public:
void set(double l, double w, double h) {
length = l; width = w; height = h;
}
double volume() {
return length * width * height;
}
};
构造函数在对象创建时调用,析构函数在对象销毁时调用。
cpp复制编辑class Demo {
public:
Demo() {
cout << "Constructor called" << endl;
}
~Demo() {
cout << "Destructor called" << endl;
}
};
cpp复制编辑class Shape {
public:
virtual void draw() {
cout << "Drawing shape" << endl;
}
};
class Circle : public Shape {
public:
void draw() override {
cout << "Drawing circle" << endl;
}
};
多态的关键是通过虚函数实现运行时行为的改变。
STL 是 C++ 的精髓,提供泛型容器、算法和迭代器。
vector
:动态数组
list
:双向链表
map
:关联键值对
set
:不重复集合
cpp复制编辑#include <vector>
vector<int> nums = {1, 2, 3};
nums.push_back(4);
cpp复制编辑#include <algorithm>
vector<int> v = {3, 1, 4, 2};
sort(v.begin(), v.end());
cpp复制编辑int a = 5;
int* ptr = &a;
int& ref = a;
cpp复制编辑int* arr = new int[10];
// ...
delete[] arr;
cpp复制编辑#include <memory>
unique_ptr<int> up(new int(10));
智能指针能自动释放资源,防止内存泄漏。
自动推断变量类型:
cpp复制编辑auto x = 5; // int
decltype(x) y = 6; // y 也是 int
cpp复制编辑auto square = [](int x) { return x * x; };
cout << square(4); // 输出 16
cpp复制编辑pair<int, string> p = {1, "A"};
auto [id, name] = p;
我们要创建一个控制台应用程序,支持添加学生、展示所有学生信息,具备基本的面向对象结构。
cpp复制编辑#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Student {
private:
string name;
int id;
float score;
public:
Student(string name, int id, float score)
: name(name), id(id), score(score) {}
void display() const {
cout << "ID: " << id << ", Name: " << name
<< ", Score: " << score << endl;
}
int getId() const { return id; }
};
class StudentManager {
private:
vector<Student> students;
public:
void addStudent(const Student& s) {
students.push_back(s);
}
void listStudents() const {
if (students.empty()) {
cout << "No students found." << endl;
} else {
for (const auto& s : students)
s.display();
}
}
};
int main() {
StudentManager manager;
int choice;
while (true) {
cout << "\n1. Add Student\n2. List Students\n3. Exit\nChoose: ";
cin >> choice;
if (choice == 1) {
string name;
int id;
float score;
cout << "Enter Name: ";
cin >> name;
cout << "Enter ID: ";
cin >> id;
cout << "Enter Score: ";
cin >> score;
manager.addStudent(Student(name, id, score));
} else if (choice == 2) {
manager.listStudents();
} else {
break;
}
}
return 0;
}
通过本文,我们系统回顾了 C++ 的基本语法、面向对象设计、STL 使用、现代特性与内存控制等方面,并通过一个控制台小项目加以巩固。
如果你想进一步提升 C++ 能力,建议深入学习以下方向:
std::thread
)
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。