C++ 是一门横跨数十年的强大编程语言,融合了过程式和面向对象编程范式,在现代软件工程中仍扮演着重要角色。它被广泛应用于系统软件、嵌入式设备、游戏引擎、图形处理、数据库系统等高性能领域。相比 Python、Java 等现代语言,C++ 更加贴近硬件,掌控力强,适合对性能要求极高的系统开发。
本文将带你全面了解 C++ 的基本语法、类与对象、标准模板库 STL、内存管理、高级语言特性,并通过一个简易控制台项目加以实践,构建起一个完整的知识体系。
C++ 使用 cin
和 cout
进行标准输入输出操作:
cpp复制编辑#include<iostream>usingnamespace std;
intmain() {
int x;
cout << "Enter a number: ";
cin >> x;
cout << "You entered: " << x << endl;
return0;
}
控制结构包括 if-else
、switch
、for
、while
、do-while
等:
cpp复制编辑for (int i = 0; i < 5; ++i) {
cout << i << " ";
}
C++ 的最大亮点在于其面向对象特性,支持类(Class)、封装、继承和多态。
cpp复制编辑classBox {
private:
double length, width, height;
public:
voidset(double l, double w, double h) {
length = l; width = w; height = h;
}
doublevolume() {
return length * width * height;
}
};
构造函数在对象创建时调用,析构函数在对象销毁时调用。
cpp复制编辑classDemo {
public:
Demo() {
cout << "Constructor called" << endl;
}
~Demo() {
cout << "Destructor called" << endl;
}
};
cpp复制编辑classShape {
public:
virtualvoiddraw() {
cout << "Drawing shape" << endl;
}
};
classCircle : public Shape {
public:
voiddraw()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 = newint[10];
// ...delete[] arr;
cpp复制编辑#include<memory>unique_ptr<int> up(newint(10));
智能指针能自动释放资源,防止内存泄漏。
自动推断变量类型:
cpp复制编辑auto x = 5; // intdecltype(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>usingnamespace std;
classStudent {
private:
string name;
int id;
float score;
public:
Student(string name, int id, float score)
: name(name), id(id), score(score) {}
voiddisplay()const {
cout << "ID: " << id << ", Name: " << name
<< ", Score: " << score << endl;
}
intgetId()const { return id; }
};
classStudentManager {
private:
vector<Student> students;
public:
voidaddStudent(const Student& s) {
students.push_back(s);
}
voidlistStudents()const {
if (students.empty()) {
cout << "No students found." << endl;
} else {
for (constauto& s : students)
s.display();
}
}
};
intmain() {
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));
} elseif (choice == 2) {
manager.listStudents();
} else {
break;
}
}
return0;
}
通过本文,我们系统回顾了 C++ 的基本语法、面向对象设计、STL 使用、现代特性与内存控制等方面,并通过一个控制台小项目加以巩固。
如果你想进一步提升 C++ 能力,建议深入学习以下方向:
std::thread
)作者: 小菜菜编程, 来源:面包板社区
链接: https://mbb.eet-china.com/blog/uid-me-4114532.html
版权声明:本文为博主原创,未经本人允许,禁止转载!
文章评论(0条评论)
登录后参与讨论