c++的起源

# C++的起源故事与简介
## 📜 起源故事
1979年,在贝尔实验室,一位名叫**Bjarne Stroustrup**(比雅尼·斯特劳斯特鲁普)的丹麦计算机科学家遇到了一个问题:他需要为他的博士论文进行分布式系统的模拟,但现有的语言要么太慢(Simula),要么太底层(C语言)。
于是,Stroustrup萌生了一个想法:**”为什么不把Simula的面向对象特性和C语言的高效性结合起来呢?”**
最初,这个项目被称为”**C with Classes**”(带类的C),就像给C语言穿上了一件面向对象的外衣。1983年,这个语言正式改名为**C++**,其中的”++”源自C语言的自增运算符,寓意着”C语言的进化版”。
## 🎯 C++简介
C++是一门:
– **多范式编程语言**:支持面向过程、面向对象、泛型编程
– **高性能语言**:接近硬件,效率媲美C语言
– **应用广泛**:从操作系统、游戏引擎到嵌入式系统都有它的身影
**核心特性:**
– 类和对象(OOP)
– 模板和泛型编程
– 手动内存管理(指针)
– 标准模板库(STL)
– 运算符重载
## 💻 简单代码示例
### 1. Hello World – 经典开场
“`cpp
#include
int main() {
std::cout << "Hello, C++!" << std::endl; return 0; } ``` ### 2. 类和对象 - 面向对象的魅力 ```cpp #include
#include
class Person {
private:
std::string name;
int age;
public:
// 构造函数
Person(std::string n, int a) : name(n), age(a) {}

// 成员函数
void introduce() {
std::cout << "我叫" << name << ",今年" << age << "岁" << std::endl; } }; int main() { Person person("小明", 25); person.introduce(); return 0; } ``` ### 3. 模板 - 泛型编程 ```cpp #include
// 函数模板:可以处理任意类型
template
T getMax(T a, T b) {
return (a > b) ? a : b;
}
int main() {
std::cout << "整数最大值: " << getMax(10, 20) << std::endl; std::cout << "浮点数最大值: " << getMax(3.14, 2.71) << std::endl; return 0; } ``` ### 4. STL容器 - 标准库的力量 ```cpp #include
#include
#include
int main() {
std::vector numbers = {5, 2, 8, 1, 9};

// 排序
std::sort(numbers.begin(), numbers.end());

// 遍历输出
for (int num : numbers) {
std::cout << num << " "; } std::cout << std::endl; return 0; } ``` ### 5. 智能指针 - 现代C++(C++11后) ```cpp #include
#include
class Resource {
public:
Resource() { std::cout << "资源已创建" << std::endl; } ~Resource() { std::cout << "资源已释放" << std::endl; } void use() { std::cout << "正在使用资源" << std::endl; } }; int main() { // 智能指针自动管理内存 std::shared_ptr ptr = std::make_shared();
ptr->use();
// 离开作用域时自动释放,无需手动delete
return 0;
}
“`
## 🌟 有趣的小知识
– C++标准每三年更新一次:C++11、C++14、C++17、C++20、C++23…
– C++之父Bjarne Stroustrup说过:”**C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.**”(C语言容易让你射到自己的脚;C++更难做到,但一旦出错,整条腿都会炸飞)
– 许多知名软件都用C++编写:Chrome浏览器、MySQL数据库、Adobe产品、游戏引擎(虚幻4/5)等

**从1979年到现在,C++已经走过了40多年的历程,它依然是最重要的系统级编程语言之一!** 🚀

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注