C++ 是一门功能强大、灵活且广泛应用于各类系统开发的编程语言。自 1983 年由 Bjarne Stroustrup 在 C 语言基础上开发以来,C++ 融合了过程式编程与面向对象编程的优势,成为编译型语言中的佼佼者。随着 C++11、C++14、C++17 以及 C++20 等标准的引入,它逐渐成为开发高性能系统、游戏引擎、图形处理软件和嵌入式设备等的重要工具。
本文将从 C++ 的语法基础入手,逐步讲解其面向对象特性、标准模板库(STL)使用、内存管理方式,最后辅以一个简单项目实例,帮助读者全面掌握 C++ 的核心精髓。
C++ 支持多种数据类型,包括基本类型(int
、float
、char
、bool
)、构造类型(array
、struct
、class
)、指针、引用等。
cpp复制编辑int age = 25;
float score = 89.5f;
char grade = 'A';
bool passed = true;
C++ 支持标准的流程控制语句,如 if
、switch
、for
、while
、do-while
。
cpp复制编辑if (score >= 60) {
cout << "Passed!" << endl;
} else {
cout << "Failed!" << endl;
}
函数是 C++ 的基础构建块之一:
cpp复制编辑intadd(int a, int b) {
return a + b;
}
C++ 是最早支持面向对象编程的语言之一,核心概念包括类、对象、继承、多态、封装等。
cpp复制编辑classStudent {
private:
string name;
int age;
public:
Student(string n, int a) : name(n), age(a) {}
voidintroduce() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
继承允许子类复用父类的代码,而多态允许使用基类指针操作派生类对象。
cpp复制编辑classAnimal {
public:
virtualvoidsound() {
cout << "Animal sound" << endl;
}
};
classDog : public Animal {
public:
voidsound()override {
cout << "Woof!" << endl;
}
};
voidmakeSound(Animal* a) {
a->sound(); // 多态调用
}
C++ 的标准模板库提供了常用的数据结构与算法的实现,如 vector
、map
、set
、queue
等。
cpp复制编辑#include<vector>
vector<int> nums = {1, 2, 3, 4};
nums.push_back(5);
cpp复制编辑#include<map>
map<string, int> ages;
ages["Alice"] = 30;
ages["Bob"] = 25;
cpp复制编辑#include<algorithm>
vector<int> data = {3, 1, 4, 1, 5};
sort(data.begin(), data.end());
C++ 的内存控制细粒度强,支持手动申请与释放内存(尤其是在使用裸指针时)。
cpp复制编辑int a = 10;
int* p = &a;
cout << *p << endl;
int& ref = a;
ref = 20;
cpp复制编辑int* arr = newint[5]; // 分配内存delete[] arr; // 释放内存
使用智能指针(C++11 之后)更为推荐:
cpp复制编辑#include<memory>
shared_ptr<int> sp = make_shared<int>(100);
C++ 提供 try-catch
结构处理运行时错误。
cpp复制编辑try {
throwruntime_error("Something went wrong");
} catch (const exception& e) {
cout << e.what() << endl;
}
cpp复制编辑auto x = 10; // 自动推断为 int
cpp复制编辑vector<int> v = {1, 2, 3, 4};
for_each(v.begin(), v.end(), [](int x){ cout << x << " "; });
cpp复制编辑for (int n : v) {
cout << n << " ";
}
cpp复制编辑pair<int, string> p = {1, "apple"};
auto [id, name] = p;
下面是一个简单的 C++ 控制台程序,用于录入与展示学生信息。
cpp复制编辑#include<iostream>#include<vector>usingnamespace std;
classStudent {
private:
string name;
int age;
float grade;
public:
Student(string n, int a, float g) : name(n), age(a), grade(g) {}
voiddisplay()const {
cout << "Name: " << name << ", Age: " << age << ", Grade: " << grade << endl;
}
};
intmain() {
vector<Student> students;
int n;
cout << "Enter number of students: ";
cin >> n;
for (int i = 0; i < n; ++i) {
string name;
int age;
float grade;
cout << "Student " << i + 1 << " Name: ";
cin >> name;
cout << "Age: ";
cin >> age;
cout << "Grade: ";
cin >> grade;
students.emplace_back(name, age, grade);
}
cout << "\nStudent List:\n";
for (constauto& s : students) {
s.display();
}
return0;
}
C++ 虽然学习曲线较陡,但其高效执行能力和灵活的编程范式使其在众多应用场景中依然保持强劲生命力。掌握 C++ 不仅能提升对底层计算机原理的理解,还能为系统级开发打下坚实基础。通过本篇文章对 C++ 的基础、面向对象特性、STL 使用与实战代码的全面介绍,相信读者能更系统地把握这门语言的核心精髓。
作者: 小菜菜编程, 来源:面包板社区
链接: https://mbb.eet-china.com/blog/uid-me-4114532.html
版权声明:本文为博主原创,未经本人允许,禁止转载!
文章评论(0条评论)
登录后参与讨论