C++ 由 Bjarne Stroustrup 在 1985 年设计,最初名为 "带类的 C",是 C 语言的扩展。它融合了面向过程编程(C 语言特性)、面向对象编程(OOP)和泛型编程,是一种静态类型、编译型、通用目的的编程语言。
#include
#include
using namespace std; // 使用标准命名空间
int main() { // 程序入口函数
cout << "Hello, C++!" << endl; // 输出到控制台
return 0; // 程序正常结束
}
类型 |
说明 |
示例 |
基本类型 |
int, float, double, char |
int age = 18; |
复合类型 |
数组、结构体、枚举 |
struct Student { string name; }; |
布尔类型 |
bool(true/false) |
bool is_ok = true; |
类型别名 |
using 定义别名 |
using ll = long long; |
int score = 85;
if (score >= 90) {
cout << "A" << endl;
} else if (score >= 80) {
cout << "B" << endl; // 输出B
} else {
cout << "C" << endl;
}
// for循环:打印1-5
for (int i=1; i<=5; i++) {
cout << i << " "; // 1 2 3 4 5
}
// while循环:计算1+2+...+100
int sum = 0, n=1;
while (n <= 100) {
sum += n;
n++;
}
cout << sum << endl; // 5050
// 函数声明
int add(int a, int b);
// 函数定义
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(3, 5); // 调用函数
cout << "3+5=" << result << endl; // 8
return 0;
}
// 定义类:Circle(圆)
class Circle {
private: // 私有成员,外部不可直接访问
double radius; // 半径
public: // 公有成员,外部可访问
// 构造函数:初始化对象
Circle(double r) : radius(r) {}
// 成员函数:计算面积
double getArea() {
return 3.14 * radius * radius;
}
};
int main() {
Circle c(5.0); // 创建对象,调用构造函数
cout << "面积:" << c.getArea() << endl; // 78.5
return 0;
}
// 基类:Animal
class Animal {
public:
void speak() {
cout << "动物发出声音" << endl;
}
};
// 派生类:Dog,继承Animal
class Dog : public Animal {
public:
void speak() { // 重写(覆盖)基类函数
cout << "汪汪汪" << endl;
}
};
int main() {
Dog d;
d.speak(); // 汪汪汪(多态体现)
return 0;
}
class Shape {
public:
virtual double getArea() { // 虚函数
return 0.0;
}
};
class Rectangle : public Shape {
private:
double width, height;
public:
Rectangle(double w, double h) : width(w), height(h) {}
double getArea() override { // C++11新增关键字,显式声明重写
return width * height;
}
};
int main() {
Shape* s = new Rectangle(4, 5);
cout << "面积:" << s->getArea() << endl; // 20(动态多态)
delete s;
return 0;
}
#include
#include
using namespace std;
int main() {
// 输入
string name;
int age;
cout << "请输入姓名和年龄:";
cin >> name >> age; // 支持自动类型匹配
// 输出格式化
cout << "你好," << name << "!你今年" << age << "岁" << endl;
cout << "圆周率:" << fixed << setprecision(4) << 3.1415926 << endl; // 3.1416
return 0;
}
#include
#include
int main() {
string str = "Hello, C++!";
// 长度与遍历
cout << "长度:" << str.length() << endl; // 12
for (char c : str) cout << c; // 范围for循环(C++11)
// 操作函数
str.append(" Welcome!"); // 拼接
str.erase(5, 1); // 删除索引5的逗号
transform(str.begin(), str.end(), str.begin(), ::toupper); // 转大写
return 0;
}
#include
#include
#include
int main() {
// vector:动态数组
vector
nums.push_back(4); // 尾部插入
cout << "第一个元素:" << nums.front() << endl; // 1
// list:双向链表
list
names.push_front("Alice"); // 头部插入
names.sort(); // 内置排序函数
return 0;
}
#include
#include
int main() {
// map:键值对存储
map
scores["张三"] = 90;
scores["李四"] = 85;
cout << "张三的分数:" << scores["张三"] << endl; // 90
// set:无序不重复集合
set
s.insert(3); // 重复插入无效
cout << "大小:" << s.size() << endl; // 3
return 0;
}
#include
int main() {
vector
sort(nums.begin(), nums.end()); // 排序:1 1 3 4 5 9
reverse(nums.begin(), nums.end()); // 反转:9 5 4 3 1 1
int count = count(nums.begin(), nums.end(), 1); // 统计1的个数:2
return 0;
}
int main() {
// 指针:存储内存地址
int a = 10;
int* ptr = &a; // 取地址
cout << "*ptr = " << *ptr << endl; // 10(解引用)
// 引用:变量的别名
int& ref = a;
ref = 20;
cout << "a = " << a << endl; // 20
return 0;
}
int* arr = new int[5]; // 分配数组内存
for (int i=0; i<5; i++) arr[i] = i;
delete[] arr; // 释放内存(避免内存泄漏)
#include
class Resource {
public:
~Resource() { cout << "释放资源" << endl; }
};
int main() {
// 独占所有权:unique_ptr
unique_ptr
// 共享所有权:shared_ptr(引用计数)
shared_ptr
return 0; // 离开作用域自动释放
}
// 函数模板
template
T max(T a, T b) {
return a > b ? a : b;
}
// 类模板
template
class Vector {
private:
T* data;
public:
Vector() { data = new T[0]; }
};
int main() {
cout << max(3, 5) << endl; // 5(int类型)
cout << max(3.14, 2.71) << endl; // 3.14(double类型)
return 0;
}
g++ main.cpp -o program && ./program
实现功能:录入学生信息(姓名、学号、成绩),计算平均分,排序输出。技术点:结构体、数组、函数封装、文件读写(fstream)。
使用 Qt 库:
#include
#include
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QPushButton button("Hello Qt!");
button.show();
return app.exec();
}
#include
#include
// 快速排序(STL实现)
vector
sort(nums.begin(), nums.end()); // 内置快速排序
// 二分查找
bool exists = binary_search(nums.begin(), nums.end(), 3); // true
C++ 虽然入门难度较高,但掌握后能深入理解计算机底层原理,培养高效的编程思维。它不仅是一门编程语言,更是理解现代软件开发的基础。无论你未来从事系统开发、游戏编程还是算法研究,C++ 都能为你打下坚实的基础。现在就打开编译器,写下你的第一个 C++ 程序吧!记住:编程学习的唯一捷径,就是不断实践!