从小白到精通 | 高性能编程语言 | 包含对比、实用性和作业
C++是一种通用的、编译型的、高性能的编程语言。它是在C语言的基础上开发的,添加了面向对象的特性。C++的设计目标是提供高效率和高性能的编程能力。
C++中的"++"是C语言中的递增运算符,表示C语言的升级版本。所以C++读作"C加加"。
C++是最难学的编程语言,但也是最强大的。如果你想成为真正的编程高手,必须学好C++。
| 特性 | C++ | Java | Python |
|---|---|---|---|
| 学习难度 | ⭐⭐⭐⭐ 困难 | ⭐⭐⭐ 中等 | ⭐ 简单 |
| 执行速度 | 很快 | 快 | 慢 |
| 内存控制 | 完全控制 | 自动管理 | 自动管理 |
| 开发速度 | 慢 | 中等 | 快 |
| 跨平台性 | 一般 | 优秀 | 优秀 |
| 最佳用途 | 系统、游戏、高性能 | 企业应用、Web | 数据分析、AI |
任务:输出"Hello, World!"
C++(5行):
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}Java(8行):
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}Python(1行):
print("Hello, World!")C++需要编译器来将代码转换成可执行程序。
编译器是一个程序,它把你写的C++代码转换成计算机能理解的机器代码。
g++ --version 验证安装xcode-select --installg++ --versionsudo apt-get updatesudo apt-get install g++g++ --version输入 g++ --version 后,看到版本号说明安装成功。
C++程序的基本结构包括头文件、命名空间和main函数。
hello.cppg++ hello.cpp -o hello./hello(Linux/Mac)或 hello.exe(Windows)#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}#include <iostream> - 包含输入输出库using namespace std; - 使用标准命名空间int main() - 程序的入口点cout << ... << endl; - 输出文本return 0; - 返回0表示程序正常结束C++需要编译才能运行。编译过程会检查语法错误,所以要仔细阅读错误信息。
C++有丰富的数据类型,每种类型占用不同的内存空间。
#include <iostream>
using namespace std;
int main() {
int age = 25;
double height = 1.75;
char grade = 'A';
bool isStudent = true;
cout << "年龄: " << age << endl;
cout << "身高: " << height << endl;
cout << "等级: " << grade << endl;
cout << "是学生: " << isStudent << endl;
return 0;
}C++中的数据类型大小取决于编译器和操作系统。可以使用 sizeof() 函数查看大小。
int x = 10;
double y = 3.14;
char c = 'A';
string name = "Alice"; // 字符串#include <iostream>
using namespace std;
int main() {
int x = 10, y = 3;
// 算术运算
cout << x + y << endl; // 13
cout << x - y << endl; // 7
cout << x * y << endl; // 30
cout << x / y << endl; // 3
cout << x % y << endl; // 1
// 比较运算
cout << (x > y) << endl; // 1(true)
cout << (x == y) << endl; // 0(false)
return 0;
}#include <iostream>
using namespace std;
int main() {
int age = 18;
if (age < 13) {
cout << "儿童" << endl;
} else if (age < 18) {
cout << "青少年" << endl;
} else {
cout << "成年人" << endl;
}
return 0;
}#include <iostream>
using namespace std;
int main() {
int day = 3;
switch (day) {
case 1:
cout << "星期一" << endl;
break;
case 2:
cout << "星期二" << endl;
break;
case 3:
cout << "星期三" << endl;
break;
default:
cout << "其他日期" << endl;
}
return 0;
}#include <iostream>
using namespace std;
int main() {
// 基本for循环
for (int i = 0; i < 5; i++) {
cout << i << endl; // 输出 0 1 2 3 4
}
return 0;
}#include <iostream>
using namespace std;
int main() {
int count = 0;
while (count < 3) {
cout << count << endl;
count++;
}
return 0;
}函数是可重用的代码块。
#include <iostream>
using namespace std;
// 函数声明
int add(int a, int b);
// 函数定义
int add(int a, int b) {
return a + b;
}
void greet(string name) {
cout << "Hello, " << name << endl;
}
int main() {
int result = add(5, 3);
cout << result << endl; // 8
greet("Alice"); // Hello, Alice
return 0;
}#include <iostream>
using namespace std;
// 版本1:两个int参数
int add(int a, int b) {
return a + b;
}
// 版本2:两个double参数
double add(double a, double b) {
return a + b;
}
int main() {
cout << add(1, 2) << endl; // 调用版本1,输出3
cout << add(1.5, 2.5) << endl; // 调用版本2,输出4
return 0;
}指针和引用是C++最强大也最复杂的特性。
指针是一个变量,存储另一个变量的内存地址。
#include <iostream>
using namespace std;
int main() {
int x = 10;
int* p = &x; // p是指向x的指针,&x是x的地址
cout << "x的值: " << x << endl; // 10
cout << "x的地址: " << &x << endl; // 内存地址
cout << "p的值: " << p << endl; // 内存地址
cout << "p指向的值: " << *p << endl; // 10
*p = 20; // 通过指针修改x的值
cout << "x的新值: " << x << endl; // 20
return 0;
}引用是变量的别名,一旦绑定就不能改变。
#include <iostream>
using namespace std;
int main() {
int x = 10;
int& ref = x; // ref是x的引用
cout << x << endl; // 10
cout << ref << endl; // 10
ref = 20; // 修改ref也会修改x
cout << x << endl; // 20
return 0;
}指针和引用是C++最容易出错的地方。要特别小心指针的使用,避免野指针和内存泄漏。
C++支持面向对象编程,通过类来定义对象。
#include <iostream>
using namespace std;
class Person {
public:
string name;
int age;
// 构造函数
Person(string n, int a) {
name = n;
age = a;
}
// 方法
void introduce() {
cout << "我叫" << name << ",今年" << age << "岁" << endl;
}
};
int main() {
Person p1("Alice", 25);
Person p2("Bob", 30);
p1.introduce(); // 我叫Alice,今年25岁
p2.introduce(); // 我叫Bob,今年30岁
return 0;
}#include <iostream>
using namespace std;
// 父类
class Animal {
public:
void eat() {
cout << "动物在吃东西" << endl;
}
};
// 子类
class Dog : public Animal {
public:
void bark() {
cout << "狗在叫" << endl;
}
};
int main() {
Dog dog;
dog.eat(); // 继承的方法
dog.bark(); // 自己的方法
return 0;
}C++允许手动管理内存,这是C++的强大之处,也是最容易出错的地方。
#include <iostream>
using namespace std;
int main() {
// 分配内存
int* p = new int;
*p = 10;
cout << *p << endl; // 10
// 释放内存
delete p;
p = nullptr; // 设置为空指针
return 0;
}#include <iostream>
using namespace std;
int main() {
// 分配数组
int* arr = new int[5];
for (int i = 0; i < 5; i++) {
arr[i] = i * 10;
}
for (int i = 0; i < 5; i++) {
cout << arr[i] << endl;
}
// 释放数组
delete[] arr;
arr = nullptr;
return 0;
}每个 new 都必须对应一个 delete,否则会造成内存泄漏。这是C++最常见的bug。
使用Unreal Engine或Cocos2d-x开发高性能游戏。
开发操作系统、编译器、数据库引擎等系统软件。
开发高频交易系统,需要极致的性能。
开发单片机、物联网设备的固件。
开发3D渲染引擎、图像处理软件。
C++是性能最优的编程语言。任何需要极致性能的应用都会选择C++。
⭐ 简单 | ⭐⭐ 中等 | ⭐⭐⭐ 困难 | ⭐⭐⭐⭐ 挑战
要求:创建一个计算器,实现加减乘除四个函数。
要求:创建Person类,包含姓名、年龄属性和introduce()方法。
要求:使用new分配数组,计算最大值、最小值、平均值。
要求:使用指针交换两个变量的值。
要求:创建Student类,实现添加、删除、查询学生的功能。
要求:使用指针实现一个简单的链表数据结构。
完成作业后,可以在我们的客服中心提交你的代码,获得专业的代码审查。
现在你已经掌握了C++的核心概念。
C++学习的关键是理解内存管理和指针。这是最难的部分,但也是最重要的。