【C++】继承

【C++】继承

目录

一. 概念

二. 基类和派生类对象赋值转换

三. 继承中的作用域

四. 派生类的默认成员函数

1. 构造函数

2. 拷贝构造

3. 赋值重载

4. 析构函数

五. 继承与友元

六. 继承与静态成员

七. 多继承、菱形继承、菱形虚拟继承

虚拟继承解决数据冗余和二义性的原理

八. 继承和组合


一. 概念

继承是类设计层次的复用

语法:Person是父类,也称作基类。Student是子类,也称作派生类


继承关系和访问限定符:

继承以后,保护和私有不一样了

  1. 不可见:基类的私有成员还是被继承到了派生类对象中,但是语法上限制派生类对象不管在类里面还是类外面,都不能去访问它。基类的私有成员在基类中还是能用,在基类外不能用
  2. 如果基类成员不想在类外直接被访问,但需要在 派生类中能访问,就把基类成员定义为protected
  3. 基类的私有成员在子类都是不可见。基类的其他成员在子类的访问方式 == Min(成员在基类的访问限定符,继承方式),public > protected > private

二. 基类和派生类对象赋值转换

不同类型对象赋值要类型转换

int i = 0; double d = i;
class Person { public: void Print() { cout << "name:" << _name << endl; cout << "age:" << _age << endl; } protected: string _name = "peter"; // 姓名 int _age = 18; // 年龄 }; class Student : public Person { protected: int _stuid; // 学号 }; class Teacher : public Person { protected: int _jobid; // 工号 };

父类对象不能赋值给子类对象,强转也不行:否则子类特有的成员变量怎么办,给随机值?
父类对象可以赋值给子类的指针、引用(后面说)

int main() { Person p; Student s; // s = p; 错:父类不能赋值给子类 // s = (Student)p; 强转也不行 p = s; Person p1 = s; return 0; }

子类对象可以赋值给父类的对象、指针、引用
子类赋值给父类对象
,语法上特殊处理,没有发生类型转换,而是赋值兼容(切割、切片)
把子类里,父类那部分切出来,拷贝给父类

怎么证明没有发生类型转换?用另一种语法

p2 变成子类中,父类那一部分的别名

把父类的成员变量放成 public

三. 继承中的作用域

域都是编译时,查找规则的概念

定义了一个类,就有类域。子类和父类有独立的类域
类和类中有同名成员,子类成员将屏蔽父类对同名成员的直接访问,这种情况叫隐藏, 也叫重定义。(在子类成员函数中,可以使用父类::父类成员显示访问)
父子类域中,如果是成员函数的隐藏,只需要函数名相同就构成隐藏

现实中尽量不要在继承体系里定义同名成员

class Person { public: void fun() { cout << "Person::func()" << endl; } protected: string _name = "小李子"; // 姓名 int _num = 111; // 身份证号 }; // 隐藏/重定义:子类和父类有同名成员,子类的成员隐藏了父类的成员 class Student : public Person { public: void fun(int) { cout << "Student::func(int)" << endl; } void Print() { cout << "姓名:" << _name << endl; cout << _num << endl; // 999 cout << Person::_num << endl; // 111 } protected: int _num = 999; // 学号 }; int main() { Student s; s.Print(); s.fun(1); //s.fun(); 报错 //编译语法检查时,现在子类找func,找到了,要传参数,但你没传 s.Person::fun(); return 0; }

2个 func 构成什么关系?(a)
a、隐藏/重定义         b、重载         c、重写/覆盖         d、编译报错

同一作用域才能构成重载

四. 派生类的默认成员函数

设计理念:父干父的活,子干子的活

1. 构造函数

按以前的理解,子类有2个成员:_name 和 _id

规定:子类的构造函数中,不能在初始化列表显示初始化父类成员。在构造函数体内可以

class Person { public: Person(const char* name = "peter") : _name(name) { cout << "Person()" << endl; } ~Person() { cout << "~Person()" << endl; } protected: string _name; // 姓名 }; class Student : public Person { public: Student(const char* name = "张三", int id = 0) :_id(0) //,_name(name) 报错: “Student”: 非法的成员初始化:“_name”不是基或成员 { } protected: int _id; }; int main() { Student s; return 0; }

没定义父类对象,但调用了父类的构造函数

规定:子类必须调父类的构造函数,初始化父类成员
        默认:在初始化列表自动调用父类的默认构造函数(父类不提供默认构造函数会报错)
        若父类不提供默认构造函数:

class Person { public: Person(const char* name) : _name(name) { cout << "Person()" << endl; } ~Person() { cout << "~Person()" << endl; } protected: string _name; // 姓名 }; class Student : public Person { public: Student(const char* name = "张三", int id = 0) :Person(name) // 比较特殊,类似于定义一个匿名对象 ,_id(0) { } protected: int _id; }; int main() { Student s; return 0; }

不加19行会报错,19行的写法很特殊

调试发现,在初始化列表先走Person(name),说明:继承的成员声明在自己的成员之前
在初始化列表先写_id也可以,因为初始化顺序是按声明顺序来的

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() { cout << "~Person()" << endl; } protected: string _name; // 姓名 }; class Student : public Person { public: Student(const char* name = "张三", int id = 0) :Person(name) ,_id(0) { } Student(const Student& s) //:_name(s._name) 报错:: “Student”: 非法的成员初始化:“_name”不是基或成员 :Person(s) ,_id(s._id) { } protected: int _id; }; int main() { Student s1; Student s2(s1); return 0; }

31行,父类拷贝构造要传父类对象,在这里只有子类对象,可以直接传:子类对象可以传给父类对象的指针/引用

如果删去31行,不会报错,但有问题:拷贝构造也是构造函数,构造函数在初始化列表不写,默认不会调拷贝构造函数,默认调默认构造函数

3. 赋值重载

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=(const Person& p)" << 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 id = 0) :Person(name) ,_id(0) { } Student(const Student& s) :Person(s) ,_id(s._id) { } Student& operator=(const Student& s) { if (this != &s) { Person::operator=(s); // 构成隐藏,要显示访问 _id = s._id; } return *this; } protected: int _id; }; int main() { Student s1; Student s3("李四", 1); s1 = s3; return 0; }

4. 析构函数

class Person { public: // ...... ~Person() { cout << "~Person()" << endl; } protected: string _name; // 姓名 }; class Student : public Person { public: // ...... ~Student() { //~Person(); 调不到 Person::~Person(); // 有问题 } protected: int _id; }; int main() { Student s1; Student s3("李四", 1); s1 = s3; return 0; }

为什么58行调不到,要指定父类类域?
由于后面多态的原因(具体后面讲),析构函数的函数名被特殊处理了,统一处理成destructor,此时构成隐藏

但此时又有新的问题,总共2个对象,调了4次析构函数

屏蔽59行反而正确了

构造、拷贝、赋值要显示调用
析构函数是个特殊,为保证析构顺序,会自动调

子类对象分开来看,分为父类部分和子类部分

显示调用父类析构,无法保证先子后父的析构顺序
所以子类析构函数完成后,自动调用父类析构,这样就保证了先子后父的析构顺序

为什么要保证先子后父的析构顺序?
因为子类中有可能用到父类成员,父类不可能用到子类

eg:先析构父,子再访问父的成员

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=(const Person& p)" << endl; if (this != &p) _name = p._name; return *this; } ~Person() { cout << "~Person()" << endl; delete _pstr; } protected: string _name; // 姓名 string* _pstr = new string("111111111"); }; class Student : public Person { public: Student(const char* name = "张三", int id = 0) :Person(name) ,_id(0) { } Student(const Student& s) :Person(s) ,_id(s._id) { } Student& operator=(const Student& s) { if (this != &s) { Person::operator=(s); _id = s._id; } return *this; } ~Student() { //Person::~Person(); cout << *_pstr << endl; // 子类中有可能用到父类成员 } protected: int _id; }; int main() { Student s1; Student s3("李四", 1); s1 = s3; return 0; }

如果把 Person::~Person(); 放开:↓

五. 继承与友元

友元关系不能继承,父类友元不能访问子类私有和保护成员

class Student; class Person { friend void Display(const Person& p, const Student& s); protected: string _name; // 姓名 }; class Student : public Person { friend void Display(const Person& p, const Student& s); protected: int _stuNum; // 学号 }; void Display(const Person& p, const Student& s) { cout << p._name << endl; cout << s._stuNum << endl; } int main() { Person p; Student s; Display(p, s); return 0; }

屏蔽掉11行会报错: “Student::_stuNum”: 无法访问 protected 成员(在“Student”类中声明)

六. 继承与静态成员

以前的继承:子类对象中的父类成员,和父类对象成员不是同一个

父类定义了static静态成员,则整个继承体系里面只有一个这样的成员。无论派生出多少个子类,都只有一个static成员实例,在子类中不会单独拷贝一份

eg:↓ 统计Person及子类创建出多少个对象

class Person { public: Person() { ++_count; } //protected: string _name; // 姓名 public: static int _count; // 统计人的个数 }; int Person::_count = 0; class Student : public Person { protected: int _stuNum; // 学号 }; class Graduate : public Student { protected: string _seminarCourse; // 研究科目 }; int main() { Person p; Student s; cout << &p._name << endl; cout << &s._name << endl; cout << &p._count<< endl; cout << &s._count << endl; cout << &Person::_count << endl; cout << &Student::_count << endl; return 0; }

七. 多继承、菱形继承、菱形虚拟继承

实践中可以谨慎用多继承,不要用菱形继承

单继承:一个子类只有一个直接父类时称这个继承关系为单继承

多继承:一个子类有两个或以上直接父类时称这个继承关系为多继承

菱形继承:菱形继承是多继承的一种特殊情况

菱形继承的问题:数据冗余(浪费空间)、二义性(不知道要访问谁)

class Person { public: string _name; // 姓名 int _age; }; class Student : public Person { protected: int _num; //学号 }; class Teacher : public Person { protected: int _id; // 职工编号 }; class Assistant : public Student, public Teacher { protected: string _majorCourse; // 主修课程 }; int main() { Assistant as; as._age = 19; // 报错: 对“_age”的访问不明确 return 0; }

二义性可以被解决,但违背常理

int main() { Assistant as; as.Student::_age = 18; // 指定访问 as.Teacher::_age = 30; return 0; }

祖师爷引入了虚拟继承,解决数据冗余、二义性

class Person { public: string _name; // 姓名 int _age; }; class Student : virtual public Person { protected: int _num; //学号 }; class Teacher : virtual public Person { protected: int _id; // 职工编号 }; class Assistant : public Student, public Teacher { protected: string _majorCourse; // 主修课程 }; int main() { Assistant as; as.Student::_age = 18; as.Teacher::_age = 30; as._age = 19; return 0; }

看着 as 里面有3份,实际是1份

虚拟继承解决数据冗余和二义性的原理

菱形继承:↓

class A { public: int _a; }; class B : public A { public: int _b; }; class C : public A { public: int _c; }; class D : public B, public C { public: int _d; }; int main() { D d; d.B::_a = 1; d.C::_a = 2; d._b = 3; d._c = 4; d._d = 5; return 0; }

菱形虚拟继承:↓

class A { public: int _a; }; class B : virtual public A { public: int _b; }; class C : virtual public A { public: int _c; }; class D : public B, public C { public: int _d; }; int main() { D d; d.B::_a = 1; d.C::_a = 2; d._b = 3; d._c = 4; d._d = 5; d._a = 0; D d1; return 0; }

这样设计有什么用呢?

int main() { D d; d._a = 0; B b; b._a = 2; b._b = 3; B* ptr = &b; ptr->_a++; ptr = &d; ptr->_a++; return 0; }

先取到偏移量,计算 _a 在对象中的地址,再访问

八. 继承和组合

class C { //.... }; // 继承 class D : public C {}; // 组合 class E { private: C _cc; };

继承一般不用 private

继承:白箱复用,父类的内部细节对子类可见;子类对象可以访问父类的 public、protected;父类和子类的耦合度高。改变父类的成员,子类要跟着改

组合:黑箱复用,对象的内部细节不可见;E类只能用 C类的 public 和定义了友元的 protected;耦合度低

实践中多用组合。继承和组合都可以,就用组合;更适合用继承的用继承;实现多态,必须用继承

public继承是 is-a 的关系:每个子类对象一定是父类对象
        eg:教师、学生都是人

组合是 has-a 的关系:E 组合了 C,每个 E对象中都有一个 C对象
        eg:E是车,C是轮胎,每个车都有轮胎

本篇的分享就到这里了,感谢观看,如果对你有帮助,别忘了点赞+收藏+关注
小编会以自己学习过程中遇到的问题为素材,持续为您推送文章

Read more

无人机“黑飞”正式入法:2026年1月1日起违规飞行将面临拘留

无人机"黑飞"正式入法:2026年1月1日起违规飞行将面临拘留 一、新规核心内容 2025年6月27日,十四届全国人大常委会第十六次会议表决通过新修订的《中华人民共和国治安管理处罚法》,明确将无人机"黑飞"列为"妨害公共安全的行为",自2026年1月1日起正式实施。 法律依据:新《治安管理处罚法》第46条规定:"违反有关法律法规关于飞行空域管理规定,飞行民用无人驾驶航空器、航空运动器材,或者升放无人驾驶自由气球、系留气球等升空物体,情节较重的,处五日以上十日以下拘留。" 特别严重情形(如非法穿越边境线):最高可处十日以上十五日以下拘留。 二、"黑飞"的法律定义 **无人机"黑飞"**是指违反《无人驾驶航空器飞行管理暂行条例》等法律法规的无人机飞行活动,具体包括: 1.

By Ne0inhk
LazyLLM 测评 | 低代码颠覆 AI 开发!代码专家智能体进阶模块实战

LazyLLM 测评 | 低代码颠覆 AI 开发!代码专家智能体进阶模块实战

摘要: LazyLLM 是商汤大装置推出的开源低代码框架,作为构建和优化多 Agent 应用的一站式开发框架,覆盖应用搭建、数据准备、模型部署、微调、评测等全流程开发环节,提供丰富的工具支持。其以模块化设计打破传统开发壁垒,通过数据流驱动重构开发逻辑,能让开发者用极简代码实现工业级复杂 AI 应用,摆脱冗余编码束缚,聚焦核心业务场景,降低 AI 应用构建成本并支持持续迭代优化。堪称 AI 开发者的 “效率神器”,其技术普惠理念为 AI 开发领域带来新的实践范式,推动了更高效的开发模式。本文将以Python编程为切入点,带你深入了解LazyLLM框架。 LazyLLM 是构建和优化多 Agent 应用的一站式开发工具,为应用开发过程中的全部环节(包括应用搭建、数据准备、模型部署、模型微调、评测等)提供了大量的工具,协助开发者用极低的成本构建 AI 应用,并可以持续地迭代优化效果。 LazyLLM作为商汤大装置推出的开源低代码框架,简直是AI开发者的“效率神器”

By Ne0inhk
龙虾机器人(OpenClaw)本地部署完全技术指南

龙虾机器人(OpenClaw)本地部署完全技术指南

龙虾机器人(OpenClaw)本地部署完全技术指南 前言:什么是“龙虾机器人”? 在开始部署之前,我们需要明确部署的对象。通常所说的“龙虾机器人”指的是开源项目 OpenClaw(曾用名:Clawdbot、Moltbot)。它由程序员彼得·斯坦伯格开发,是一个开源的、可本地部署的通用型AI代理系统。与ChatGPT等对话式AI不同,OpenClaw被赋予了操作系统的权限:它可以执行终端命令、读写文件、操控浏览器、安装软件,甚至通过MCP协议调用外部工具。 由于其强大的系统操控能力,安全性是部署时需关注的首要问题。官方及社区普遍建议:不要在主力机或存有敏感数据的生产环境直接裸奔部署,最好使用虚拟机、Docker容器或专用硬件(如Mac Mini或AI开发盒子)进行隔离。 第一章:环境准备与核心依赖 在安装OpenClaw之前,必须准备好运行环境。OpenClaw的核心由TypeScript编写,因此Node.js是必不可少的运行环境。此外,根据安装方式的不同,可能还需要Git、Docker或Python环境。 1.1 硬件建议与系统选择 * Linux

By Ne0inhk
Flutter 三方库 arcane_helper_utils 的鸿蒙化适配指南 - 实现具备通用逻辑增强与多维开发脚手架的实用工具集、支持端侧业务开发的效率倍增实战

Flutter 三方库 arcane_helper_utils 的鸿蒙化适配指南 - 实现具备通用逻辑增强与多维开发脚手架的实用工具集、支持端侧业务开发的效率倍增实战

欢迎加入开源鸿蒙跨平台社区:https://openharmonycrossplatform.ZEEKLOG.net Flutter 三方库 arcane_helper_utils 的鸿蒙化适配指南 - 实现具备通用逻辑增强与多维开发脚手架的实用工具集、支持端侧业务开发的效率倍增实战 前言 在进行 Flutter for OpenHarmony 开发时,如何快速处理常见的字符串格式化、色值转换、日期计算或布尔值增强?虽然每一个功能都很小,但如果每个项目都重复造轮子,开发效率将大打折扣。arcane_helper_utils 是一款专注于极致实用的“瑞士军刀”型工具集。本文将探讨如何在鸿蒙端通过这类高内聚的 Utility 集实现极致、丝滑的业务交付。 一、原直观解析 / 概念介绍 1.1 基础原理 该库通过对 Dart 原生类型(Object, String, List, Map, Bool)

By Ne0inhk