// C++ 速成示例代码
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// 定义一个简单的类
class Person {
public:
string name;
int age;
// 构造函数
Person(string n, int a) : name(n), age(a) {}
// 成员函数
void introduce() const {
cout << "Hello, my name is " << name << " and I am " << age << " years old." << endl;
}
};
int main() {
// 输出 "Hello, World!"
cout << "Hello, World!" << endl;
// 使用标准库中的 vector 容器
vector<int> numbers = {1, 2, 3, 4, 5};
for (int num : numbers) {
cout << num << " ";
}
cout << endl;
// 创建一个 Person 对象并调用其成员函数
Person person("Alice", 30);
person.introduce();
return 0;
}
#include <iostream> 和 #include <string>:引入了标准输入输出流和字符串处理的库。using namespace std;:使用标准命名空间,避免每次调用标准库函数时都要加上 std:: 前缀。Person:展示了如何定义一个简单的类,并包含构造函数和成员函数。main 函数:程序的入口点,演示了基本的输入输出操作、使用标准库容器(如 vector)以及创建对象并调用其方法。这个示例代码涵盖了 C++ 的一些基础概念,包括类和对象、标准库容器以及基本的输入输出操作。
上一篇:new在c++语言中的意思
下一篇:c++20 协程
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站