1. 打印“Hello, World!”

#include <iostream> using namespace std; int main() { cout << "Hello, World!" << endl; return 0; } 

2. 变量声明和赋值

#include <iostream> using namespace std; int main() { int age = 25; cout << "My age is: " << age << endl; return 0; } 

3. 控制台输入输出

#include <iostream> using namespace std; int main() { int number; cout << "Enter a number: "; cin >> number; cout << "You entered: " << number << endl; return 0; } 

4. 数据类型转换

#include <iostream> using namespace std; int main() { int num = 10; double result = static_cast<double>(num) + 3.5; cout << "Result: " << result << endl; return 0; } 

5. 算术运算符

#include <iostream> using namespace std; int main() { int a = 5, b = 3; cout << "Sum: " << (a + b) << endl; cout << "Difference: " << (a - b) << endl; cout << "Product: " << (a * b) << endl; cout << "Quotient: " << (a / b) << endl; cout << "Remainder: " << (a % b) << endl; return 0; } 

6. 条件语句(if-else)

#include <iostream> using namespace std; int main() { int number = 10; if (number > 0) { cout << "The number is positive." << endl; } else { cout << "The number is not positive." << endl; } return 0; } 

7. 循环语句(for)

#include <iostream> using namespace std; int main() { for (int i = 1; i <= 5; ++i) { cout << i << endl; } return 0; } 

8. 循环语句(while)

#include <iostream> using namespace std; int main() { int i = 1; while (i <= 5) { cout << i << endl; i++; } return 0; } 

9. 数组声明和初始化

#include <iostream> using namespace std; int main() { int arr[5] = {1, 2, 3, 4, 5}; for (int i = 0; i < 5; ++i) { cout << arr[i] << endl; } return 0; } 

10. 函数定义和调用

#include <iostream> using namespace std; void sayHello() { cout << "Hello!" << endl; } int main() { sayHello(); return 0; } 

11. 函数返回值

#include <iostream> using namespace std; int add(int a, int b) { return a + b; } int main() { int sum = add(5, 3); cout << "Sum: " << sum << endl; return 0; } 

12. 指针简介

#include <iostream> using namespace std; int main() { int x = 10; int *ptr = &x; cout << "Value of x: " << x << endl; cout << "Address of x: " << &x << endl; cout << "Value of ptr: " << ptr << endl; cout << "Value of *ptr: " << *ptr << endl; return 0; } 

13. 结构体定义和实例化

#include <iostream> using namespace std; struct Person { string name; int age; }; int main() { Person person; person.name = "John"; person.age = 25; cout << "Name: " << person.name << ", Age: " << person.age << endl; return 0; } 

14. 枚举类型

#include <iostream> using namespace std; enum Weekday { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }; int main() { Weekday today = Wednesday; cout << "Today is: " << today << endl; return 0; } 

15. 构造函数和析构函数

#include <iostream> using namespace std; class Rectangle { private: int width, height; public: Rectangle(int w, int h) : width(w), height(h) { cout << "Rectangle created with width " << width << " and height " << height << endl; } ~Rectangle() { cout << "Rectangle destroyed." << endl; } }; int main() { Rectangle rect(5, 10); return 0; } 

16. 继承

#include <iostream> using namespace std; class Base { public: void show() { cout << "This is Base class" << endl; } }; class Derived : public Base { public: void show() { cout << "This is Derived class" << endl; } }; int main() { Derived d; d.show(); return 0; } 

17. 多态

#include <iostream> using namespace std; class Base { public: virtual void display() { cout << "Base class display" << endl; } }; class Derived : public Base { public: void display() override { cout << "Derived class display" << endl; } }; int main() { Base *bptr; Derived obj; bptr = &obj; bptr->display(); return 0; } 

18. 抽象类

#include <iostream> using namespace std; class Shape { public: virtual void draw() = 0; // Pure virtual function }; class Circle : public Shape { public: void draw() override { cout << "Drawing Circle" << endl; } }; int main() { Circle c; c.draw(); return 0; } 

19. 基于范围的for循环

#include <iostream> #include <vector> using namespace std; int main() { vector<int> vec = {1, 2, 3, 4, 5}; for (int elem : vec) { cout << elem << endl; } return 0; } 

20. 函数模板

#include <iostream> using namespace std; template <typename T> T add(T a, T b) { return a + b; } int main() { cout << "Sum of integers: " << add(5, 10) << endl; cout << "Sum of floats: " << add(5.5, 10.5) << endl; return 0; } 

21. 类模板

#include <iostream> using namespace std; template <typename T> class Stack { private: vector<T> elements; public: void push(T element) { elements.push_back(element); } T pop() { if (elements.empty()) { throw runtime_error("Stack is empty"); } T element = elements.back(); elements.pop_back(); return element; } }; int main() { Stack<int> intStack; intStack.push(5); intStack.push(10); cout << "Popped: " << intStack.pop() << endl; return 0; } 

22. 迭代器

#include <iostream> #include <vector> #include <iterator> using namespace std; int main() { vector<int> vec = {1, 2, 3, 4, 5}; copy(vec.begin(), vec.end(), ostream_iterator<int>(cout, " ")); cout << endl; return 0; } 

23. 异常处理

#include <iostream> #include <stdexcept> using namespace std; int divide(int a, int b) { if (b == 0) { throw runtime_error("Division by zero"); } return a / b; } int main() { try { cout << divide(10, 0) << endl; } catch (const runtime_error& e) { cout << "Exception caught: " << e.what() << endl; } return 0; } 

24. 智能指针

#include <iostream> #include <memory> using namespace std; class Resource { public: void use() { cout << "Using resource" << endl; } }; int main() { unique_ptr<Resource> ptr(new Resource()); ptr->use(); return 0; } 

25. Lambda表达式

#include <iostream> #include <vector> using namespace std; int main() { vector<int> vec = {1, 2, 3, 4, 5}; for (auto it = vec.begin(); it != vec.end(); ++it) { cout << *it << endl; } return 0; } 

26. 标准库算法

#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { vector<int> vec = {5, 2, 9, 1, 5, 6}; sort(vec.begin(), vec.end()); for (int num : vec) { cout << num << " "; } cout << endl; return 0; } 

27. 模板元编程

#include <iostream> using namespace std; template <typename T, size_t N> struct Array { T data[N]; }; template <typename T, size_t N> void printArray(const Array<T, N>& arr) { for (size_t i = 0; i < N; ++i) { cout << arr.data[i] << " "; } cout << endl; } int main() { Array<int, 5> arr = {1, 2, 3, 4, 5}; printArray(arr); return 0; } 

28. 命名空间

#include <iostream> using namespace std; namespace myNamespace { void sayHello() { cout << "Hello from myNamespace!" << endl; } } int main() { myNamespace::sayHello(); return 0; } 

29. 静态成员和静态方法

#include <iostream> using namespace std; class MyClass { public: static int count; static void printCount() { cout << "Count: " << count << endl; } }; int MyClass::count = 0; int main() { MyClass::count = 10; MyClass::printCount(); return 0; } 

30. 动态内存分配和释放

#include <iostream> using namespace std; int main() { int* ptr = new int(5); cout << "Value of ptr: " << *ptr << endl; delete ptr; cout << "Memory freed" << endl; return 0; } 

以上是C++编程入门的30个实用代码示例,涵盖了基本语法、数据结构、面向对象编程和标准库等方面。通过这些示例,你可以快速上手C++编程。