YCYouwei Chenincpp-blog.hashnode.dev·Mar 10, 2025 · 4 min read指针与运用(Pointers):引用运算符(&): 基本引用 #include <iostream> int main() { int a = 10; int& ref = a; // ref 是 a 的引用,ref 和 a 绑定在一起 ref = 20; // 修改 ref 也会修改 a std::cout << "a: " << a << std::endl; // 输出 20 return 0; } 函数参数中的作用 传递变量的引用: #inc...00
YCYouwei Chenincpp-blog.hashnode.dev·Mar 10, 2025 · 1 min readC++函数 (functions)函数声明: 函数声明告诉编译器函数的名称,返回类型和参数列表,但不包含函数体。作用: 让编译器知道函数的存在,以便在定义之前使用(通常用于头文件) 允许代码结构更加清晰(函数声明在前,定义在后)。 语法: 返回类型 函数名(参数列表); 函数定义: 函数定义包括函数的完整实现,即包括函数体(代码逻辑)。 语法: 返回类型 函数名(参数列表) { // 函数体(实现代码) return 返回值; // (如果返回类型不是 `void`) } 函数声明和定义的分离: 通...00
YCYouwei Chenincpp-blog.hashnode.dev·Mar 5, 2025 · 1 min readC++数组和向量:数组 (Array) 数组是固定大小的连续内存块,在编译时确定大小。它们在内存中是静态分配的,意味着大小不可改变。 声明和初始化数组 int arr[5] = {1, 2, 3, 4, 5}; //声明并初始化一个包含5个整数数组 优点: 性能高:数组的访问速度较快,因为它们在内存中是连续的。 内存占用少:没有额外的开销 缺点: 大小固定: 一旦声明,数组的大小不可更改。 不便于扩展:如果需要增加元素,必须手动创建一个新的更大的数组并复制原数组的内容。 std::array (...00
YCYouwei Chenincpp-blog.hashnode.dev·Feb 10, 2025 · 3 min readPolymorphism in C++What is Polymorphism? Polymorphism means “many forms“. In C++, it allows a single function or operator to behave differently depending on the context in which it is used. For example, a function makeSound() can behave differently for a Dog object and...00
YCYouwei Chenincpp-blog.hashnode.dev·Jan 25, 2025 · 3 min readUnderstanding Header Files and Source Files in C++Header Files (.h) Header files are used to declare the interface of our code. Source Files (.cpp): In C++, a source file contain implementations of functions/classes. Purpose of Header Files: Declare, not define: Header files declare what functions,...00