黑龙江交通系统网站建设杭州科技网站
2026/5/21 3:46:16 网站建设 项目流程
黑龙江交通系统网站建设,杭州科技网站,wordpress投票系统,大学生免费服务器C 结构体#xff08;struct#xff09;是用户自定义的数据类型#xff0c;核心用于封装多个不同类型的数据成员#xff0c;也支持成员函数和访问控制。核心特性默认访问权限为 public#xff0c;结构体间可直接访问成员#xff08;类 class 默认 private#xff09;。能…C 结构体struct是用户自定义的数据类型核心用于封装多个不同类型的数据成员也支持成员函数和访问控制。核心特性默认访问权限为 public结构体间可直接访问成员类 class 默认 private。能包含数据成员如 int、string 等和成员函数还支持构造函数、析构函数。可用于定义变量、作为函数参数或返回值也能实现继承和多态与类功能几乎一致。一、构造函数初始化结构体结构体支持自定义构造函数无参 / 有参 / 拷贝替代传统的 “赋值初始化”让初始化更规范。cpp运行#include iostream #include string using namespace std; struct Student { string name; int age; float score; // 1. 无参构造函数默认构造 Student() { name 未知; age 0; score 0.0; } // 2. 有参构造函数推荐 Student(string n, int a, float s) : name(n), age(a), score(s) {} // 3. 拷贝构造函数复制已有对象 Student(const Student other) { name other.name; age other.age; score other.score; cout 拷贝构造被调用 endl; } void showInfo() { cout 姓名 name 年龄 age 成绩 score endl; } }; int main() { Student stu1; // 调用无参构造 stu1.showInfo(); // 输出姓名未知年龄0成绩0 Student stu2(李四, 19, 92.0); // 调用有参构造 stu2.showInfo(); // 输出姓名李四年龄19成绩92 Student stu3 stu2; // 调用拷贝构造 stu3.showInfo(); // 输出姓名李四年龄19成绩92 return 0; }二、指针 / 引用访问结构体成员结构体变量的指针 / 引用需用-指针或.引用访问成员是数组、函数参数传递的高频用法。cpp运行#include iostream #include string using namespace std; struct Book { string title; float price; }; // 引用传参避免拷贝可修改原数据 void updatePrice(Book book, float newPrice) { book.price newPrice; } // 指针传参 void printBook(const Book* book) { // const 防止误修改 cout 书名 book-title 价格 book-price endl; } int main() { Book b1 {C Primer, 89.9}; // 引用访问 Book refB b1; refB.title C Primer 中文版; updatePrice(refB, 79.9); // 指针访问 Book* ptrB b1; printBook(ptrB); // 输出书名C Primer 中文版价格79.9 return 0; }三、结构体数组批量管理结构体对象常用于存储列表数据如学生列表、商品列表。cpp运行#include iostream #include string using namespace std; struct Product { string name; int stock; // 库存 float price; }; int main() { // 初始化结构体数组 Product products[3] { {手机, 50, 2999.9}, {耳机, 200, 199.9}, {充电器, 500, 49.9} }; // 遍历数组并修改/输出 for (int i 0; i 3; i) { products[i].price * 0.9; // 打9折 cout 商品 products[i].name 库存 products[i].stock 折后价 products[i].price endl; } return 0; }四、结构体继承与类一致结构体支持继承默认 public 继承可复用父结构体的成员实现 “扩展”。cpp运行#include iostream #include string using namespace std; // 父结构体基类 struct Person { string name; int age; void basicInfo() { cout 姓名 name 年龄 age endl; } }; // 子结构体派生类继承 Person struct Teacher : Person { // 默认 public 继承 string subject; // 新增成员 int salary; // 新增成员 void teachInfo() { basicInfo(); // 调用父结构体成员 cout 授课科目 subject 薪资 salary endl; } }; int main() { Teacher t1; t1.name 王老师; // 访问父结构体成员 t1.age 35; t1.subject 数学; t1.salary 8000; t1.teachInfo(); return 0; }五、静态成员共享数据结构体的静态成员属于 “整个结构体”而非单个对象所有对象共享该值如统计结构体对象数量。cpp运行#include iostream #include string using namespace std; struct Car { string brand; static int count; // 静态成员统计创建的汽车数量 // 构造函数创建对象时计数1 Car(string b) : brand(b) { count; } // 静态成员函数只能访问静态成员 static void showCount() { cout 已创建 count 辆汽车 endl; } }; // 静态成员必须在全局作用域初始化 int Car::count 0; int main() { Car c1(宝马); Car c2(奔驰); Car c3(奥迪); Car::showCount(); // 输出已创建 3 辆汽车 cout 直接访问静态成员 Car::count endl; // 输出3 return 0; }六、运算符重载自定义结构体运算重载、、等运算符让结构体支持 “类原生类型” 的运算如比较、输出。cpp运行#include iostream #include string using namespace std; struct Point { int x; int y; // 重载 运算符两个点相加 Point operator(const Point other) const { return {x other.x, y other.y}; } // 重载 运算符判断两个点是否相等 bool operator(const Point other) const { return x other.x y other.y; } }; // 重载 运算符直接输出结构体全局函数 ostream operator(ostream os, const Point p) { os ( p.x , p.y ); return os; } int main() { Point p1 {1, 2}; Point p2 {3, 4}; Point p3 p1 p2; cout p1 p2 p3 endl; // 输出(4, 6) if (p1 p2) { cout p1 和 p2 相等 endl; } else { cout p1 和 p2 不相等 endl; // 执行此分支 } return 0; }七、结构体嵌套复合数据结构体内部可包含其他结构体实现 “复杂数据模型”如 “学生” 包含 “地址” 结构体。cpp运行#include iostream #include string using namespace std; // 嵌套的子结构体 struct Address { string province; string city; string detail; }; // 主结构体 struct Student { string name; int age; Address addr; // 包含 Address 结构体 void showAll() { cout 姓名 name 年龄 age endl; cout 地址 addr.province 省 addr.city 市 addr.detail endl; } }; int main() { Student stu; stu.name 赵五; stu.age 20; stu.addr {广东, 深圳, 南山区XX路XX号}; stu.showAll(); // 输出 // 姓名赵五年龄20 // 地址广东省深圳市南山区XX路XX号 return 0; }核心总结C 结构体与类的功能几乎完全一致唯一核心区别是默认访问权限struct成员默认 publicclass成员默认 private。实际开发中若仅封装 “纯数据”无复杂行为优先用 struct若需封装 “数据 复杂逻辑”如多态、严格访问控制优先用 class。

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询