宿松网站建设设计网站开发浏览器包

张小明 2026/1/16 19:26:36
宿松网站建设设计,网站开发浏览器包,呼和浩特可以做网站的公司,seo排名培训C继承详解#xff1a;从概念到实战应用 1. 继承的基本概念 1.1 什么是继承#xff1f; 继承是面向对象程序设计中最核心的代码复用机制。它允许我们在保持原有类特性的基础上进行扩展#xff0c;增加新的方法和属性#xff0c;从而产生新的类#xff08;派生类#xff09…C继承详解从概念到实战应用1. 继承的基本概念1.1 什么是继承继承是面向对象程序设计中最核心的代码复用机制。它允许我们在保持原有类特性的基础上进行扩展增加新的方法和属性从而产生新的类派生类。简单理解就像儿子继承父亲的某些特征一样派生类可以继承基类的成员。1.2 继承的优势在没有继承之前我们需要重复定义相同的成员// 没有继承的冗余设计 class Student { public: string name; // 姓名 string address; // 地址 string phone; // 电话 int age; // 年龄 void identity() { cout 学生身份验证 endl; } void study() { cout 学生学习 endl; } }; class Teacher { public: string name; // 姓名 string address; // 地址 string phone; // 电话 int age; // 年龄 void identity() { cout 教师身份验证 endl; } void teaching() { cout 教师授课 endl; } };使用继承后代码变得简洁// 基类包含公共成员 class Person { public: string name; // 姓名 string address; // 地址 string phone; // 电话 int age; // 年龄 void identity() { cout 身份验证 endl; } }; // 派生类继承基类 class Student : public Person { public: void study() { cout 学生学习 endl; } protected: int stuId; // 学号 }; class Teacher : public Person { public: void teaching() { cout 教师授课 endl; } protected: string title; // 职称 }; int main() { Student s; Teacher t; s.identity(); // 继承自Person类 t.identity(); // 继承自Person类 s.study(); // Student特有方法 t.teaching(); // Teacher特有方法 return 0; }2. 继承的定义和访问控制2.1 继承的定义格式class 派生类名 : 继承方式 基类名 { // 派生类新增成员 };示例class Person { public: string name; protected: int age; private: string idCard; }; // public继承 class Student : public Person { public: void printInfo() { name 张三; // OK: public成员在派生类中仍是public age 20; // OK: protected成员在派生类中仍是protected // idCard 123; // ERROR: private成员在派生类中不可见 } };2.2 继承方式与访问控制基类成员/继承方式public继承protected继承private继承public成员publicprotectedprivateprotected成员protectedprotectedprivateprivate成员不可见不可见不可见重要规则基类private成员在派生类中不可见但仍被继承访问权限 Min(基类中的访问权限, 继承方式)class默认private继承struct默认public继承2.3 实际应用建议推荐使用public继承其他继承方式在实际开发中很少使用// 推荐public继承 class Student : public Person { // ... }; // 不推荐protected/private继承 class Student : protected Person { // 尽量避免 // ... };3. 继承中的特殊问题3.1 隐藏规则Name Hiding当派生类与基类有同名成员时派生类成员会隐藏基类成员class Person { protected: string name 小李子; int num 111; // 身份证号 }; class Student : public Person { public: void print() { cout 姓名: name endl; cout 身份证号: Person::num endl; // 需要指定基类作用域 cout 学号: num endl; // 访问的是Student的num } protected: int num 999; // 学号隐藏了基类的num }; int main() { Student s; s.print(); return 0; }输出结果姓名:小李子 身份证号:111 学号:9993.2 派生类的默认成员函数派生类的特殊成员函数需要正确处理基类部分class Person { public: Person(const char* name peter) : name(name) { cout Person() endl; } Person(const Person p) : name(p.name) { cout Person(const Person p) endl; } Person operator(const Person p) { cout Person operator() endl; if (this ! p) { name p.name; } return *this; } ~Person() { cout ~Person() endl; } protected: string name; }; class Student : public Person { public: Student(const char* name, int num) : Person(name) // 必须调用基类构造函数 , num(num) { cout Student() endl; } Student(const Student s) : Person(s) // 调用基类拷贝构造 , num(s.num) { cout Student(const Student s) endl; } Student operator(const Student s) { cout Student operator() endl; if (this ! s) { Person::operator(s); // 调用基类operator num s.num; } return *this; } ~Student() { // 析构函数会自动调用基类析构函数 cout ~Student() endl; } protected: int num; }; int main() { Student s1(张三, 1001); Student s2 s1; // 调用拷贝构造 s2 s1; // 调用赋值运算符 return 0; }输出结果Person() Student() Person(const Person p) Student(const Student s) Student operator() Person operator() ~Student() ~Person() ~Student() ~Person()4. 继承中的高级特性4.1 友元关系不能继承class Student; class Person { public: friend void display(const Person p, const Student s); protected: string name; }; class Student : public Person { protected: int stuNum; }; void display(const Person p, const Student s) { cout p.name endl; // OK: Person的友元 // cout s.stuNum endl; // ERROR: 不能访问Student的protected成员 } // 解决方案让display也成为Student的友元 class Student : public Person { friend void display(const Person p, const Student s); protected: int stuNum; };4.2 静态成员的继承基类的静态成员在整个继承体系中只有一份class Person { public: string name; static int count; // 静态成员 }; int Person::count 0; class Student : public Person { protected: int stuNum; }; int main() { Person p; Student s; cout p.count endl; // 相同地址 cout s.count endl; // 相同地址 cout Person::count endl; // 通过基类访问 cout Student::count endl; // 通过派生类访问 return 0; }5. 多继承和菱形继承问题5.1 多继承的基本用法class Printable { public: virtual void print() 0; }; class Drawable { public: virtual void draw() 0; }; // 多继承 class Shape : public Printable, public Drawable { public: void print() override { cout 打印形状 endl; } void draw() override { cout 绘制形状 endl; } };5.2 菱形继承问题菱形继承会导致数据冗余和二义性class Person { public: string name; }; class Student : public Person { protected: int stuNum; }; class Teacher : public Person { protected: int teacherId; }; class Assistant : public Student, public Teacher { protected: string majorCourse; }; int main() { Assistant a; // a.name 张三; // ERROR: 二义性不知道是Student::name还是Teacher::name a.Student::name 张三; // 需要明确指定 a.Teacher::name 李四; return 0; }5.3 虚继承解决方案使用虚继承解决菱形继承问题class Person { public: string name; }; class Student : virtual public Person { // 虚继承 protected: int stuNum; }; class Teacher : virtual public Person { // 虚继承 protected: int teacherId; }; class Assistant : public Student, public Teacher { protected: string majorCourse; }; int main() { Assistant a; a.name 张三; // OK: 现在只有一份name return 0; }6. 继承 vs 组合6.1 继承is-a关系// Car和BMW是is-a关系 class Car { public: void drive() { cout 驾驶汽车 endl; } }; class BMW : public Car { public: void specialFeature() { cout 宝马特色功能 endl; } };6.2 组合has-a关系// Car和Tire是has-a关系 class Tire { public: void rotate() { cout 轮胎旋转 endl; } }; class Car { private: Tire tires[4]; // 组合Car有4个Tire public: void drive() { for (auto tire : tires) { tire.rotate(); } } };6.3 选择原则优先使用组合组合的耦合度更低维护性更好// 推荐使用组合 class Stack { private: vectorint data; // 组合 public: void push(int value) { data.push_back(value); } void pop() { data.pop_back(); } int top() { return data.back(); } }; // 不推荐使用继承除非确实需要is-a关系 class Stack : public vectorint { // 不推荐 public: void push(int value) { push_back(value); } void pop() { pop_back(); } int top() { return back(); } };7. 实际应用示例7.1 实现一个不能被继承的类方法1构造函数私有化class NonInheritable { private: NonInheritable() {} // 私有构造函数 public: static NonInheritable* create() { return new NonInheritable(); } }; // class Derived : public NonInheritable {}; // ERROR: 无法访问私有构造函数方法2使用C11的final关键字class NonInheritable final { // 使用final关键字 // ... }; // class Derived : public NonInheritable {}; // ERROR: 不能继承final类7.2 继承类模板示例templatetypename T class Stack : private vectorT { // 私有继承 public: void push(const T x) { vectorT::push_back(x); // 需要指定基类作用域 } void pop() { vectorT::pop_back(); } const T top() { return vectorT::back(); } bool empty() { return vectorT::empty(); } }; int main() { Stackint st; st.push(1); st.push(2); st.push(3); while (!st.empty()) { cout st.top() ; st.pop(); } return 0; }总结继承是C面向对象编程的核心特性正确使用继承可以大大提高代码的复用性和可维护性。关键要点优先使用public继承避免protected/private继承注意隐藏规则避免同名成员引起的混淆正确处理派生类的特殊成员函数确保基类部分正确初始化避免菱形继承如必须使用则采用虚继承优先选择组合而非继承降低耦合度使用final关键字防止不希望的继承通过合理运用继承机制可以构建出层次清晰、易于维护的面向对象程序。
版权声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!

山西省三基建设办公室网站青岛百度seo

一、继承基础与概念解析1.1 继承的核心概念1.1.1 继承的本质与定义继承(inheritance)本质是类设计层次的复用继承机制是面向对象程序设计使代码可以复用的最重要的手段,它允许我们在保持原有类特性的基础上进行扩展,增加方法&…

张小明 2026/1/14 10:11:01 网站建设

凡科建设网站还用买服务器吗设计公司画册模板

文章目录系统截图项目简介大数据系统开发流程主要运用技术介绍爬虫核心代码展示结论源码文档获取定制开发/同行可拿货,招校园代理 :文章底部获取博主联系方式!系统截图 Python_9u10883y_ 论文大数据基于深度学习的蘑菇种类识别系统的设计与实现 …

张小明 2026/1/16 16:38:50 网站建设

企业网站建设58同城网站建设 深圳宝安

OBS RTSP服务器插件:构建专业级视频流分发系统 【免费下载链接】obs-rtspserver RTSP server plugin for obs-studio 项目地址: https://gitcode.com/gh_mirrors/ob/obs-rtspserver 项目概述 OBS RTSP服务器插件是一款专为OBS Studio设计的实时流传输协议服…

张小明 2026/1/16 14:40:42 网站建设

360百度网站怎么做电子商务网站建设的模式

处于成长期的企业,需要精准投资才能维持或加速增长势头。老话说 “要赚钱,先花钱”,而更贴切的说法应该是 “要赚钱,得聪明地花钱”。找到最能拉动企业增长的关键领域,投入充足预算进行优化与拓展,是企业迈…

张小明 2026/1/13 18:02:18 网站建设

企业网站服务器租用wordpress右边微信

如何将旧项目迁移到最新 ms-swift 版本 在大模型技术飞速演进的今天,一个曾经“能跑”的训练脚本,可能在几个月后就变成了维护成本高昂的技术债。许多团队面临这样的困境:老项目依赖过时框架、无法接入新硬件、微调效率低下、部署方式陈旧—…

张小明 2026/1/11 13:58:14 网站建设

网站建设哪些公司好有谁知道知乎网站是谁做的

Linly-Talker容器构建与部署全指南 在AI虚拟人技术迅速落地的今天,如何快速搭建一个能“说话、思考、表达”的数字人系统,已成为许多开发者和企业的共同需求。Linly-Talker 正是为此而生——它不是一个简单的语音合成或动画播放器,而是一个融…

张小明 2026/1/11 14:04:54 网站建设