不可或缺 Windows Native (22) - C++: 多重继承, 虚基类
作者:webabcd
介绍
不可或缺 Windows Native 之 C++
- 多重继承
- 虚基类
示例
1、基类 1
CppBase1.h
#pragma once #include <string>
#include "CppBaseVirtual.h" using namespace std; namespace NativeDll
{
// virtual 代表 CppBaseVirtual 是 CppBase1 的虚基类(虚基类是在声明派生类时,指定继承方式时声明的)
class CppBase1 : virtual public CppBaseVirtual
{ protected:
string Name;
float Salary; public:
CppBase1(int number, string name, float salary); };
}
CppBase1.cpp
/*
* 基类 1
*/ #include "pch.h"
#include "CppBase1.h" using namespace NativeDll; CppBase1::CppBase1(int number, string name, float salary) :CppBaseVirtual(number), Name("cppbase1 " + name), Salary(salary)
{ }
2、基类 2
CppBase2.h
#pragma once #include <string>
#include "CppBaseVirtual.h" using namespace std; namespace NativeDll
{
// virtual 代表 CppBaseVirtual 是 CppBase2 的虚基类(虚基类是在声明派生类时,指定继承方式时声明的)
class CppBase2 : virtual CppBaseVirtual
{ protected:
string Name;
int Age; public:
CppBase2(int number, string name, int age); };
}
CppBase2.cpp
/*
* 基类 2
*/ #include "pch.h"
#include "CppBase2.h" using namespace NativeDll; CppBase2::CppBase2(int number, string name, int age) :CppBaseVirtual(number), Name("cppbase2 " + name), Age(age)
{ }
3、虚基类
CppBaseVirtual.h
#pragma once #include <string> using namespace std; namespace NativeDll
{
class CppBaseVirtual
{ private:
int Number; public:
CppBaseVirtual(int number); };
}
CppBaseVirtual.cpp
/*
* 用于演示虚基类
*
* 注:
* 1、虚基类并不是在声明基类时声明的,而是在声明派生类时,指定继承方式时声明的
* 2、一个基类可以在生成一个派生类时作为虚基类,也可以在生成一个派生类时不作为虚基类
*/ #include "pch.h"
#include "CppBaseVirtual.h" using namespace NativeDll; CppBaseVirtual::CppBaseVirtual(int number) :Number(number)
{ }
4、派生类
CppDerived.h
#pragma once #include <string>
#include "CppBase1.h"
#include "CppBase2.h" using namespace std; namespace NativeDll
{
class CppDerived : public CppBase1, public CppBase2
{ public:
CppDerived(int number, string name, float salary, int age); string Show(); };
}
CppDerived.cpp
/*
* 派生类
*
*
* 在多重继承的情况下:
* 1、如果 A 是 B C D E 的基类,F 同时继承了 B C D E,那么实例化 F 时会保存 4 份 A 成员
* 2、如果 A 是 B 的基类,A 是 C D E 的虚基类(虚基类会使得在继承间接共同基类时只保留一份成员),F 同时继承了 B C D E,那么实例化 F 时会保存 2 份 A 成员(从 C D E 的路径上保留一份,从 B 的路径上保留一份)
*
*
* 本例中:
* 1、CppBaseVirtual 是 CppBase1 和 CppBase2 的虚基类
* 2、CppDerived 继承了 CppBase1 和 CppBase2(多重继承)
* 3、此种情况,实例化 CppDerived 只会保留一份 CppBaseVirtual 成员(因为 CppBaseVirtual 是 CppBase1 和 CppBase2 的虚基类)
* 4、C++ 编译器只会执行最后的派生类(CppDerived)对虚基类(CppBaseVirtual)的构造函数的调用,而忽略虚基类的直接派生类(CppBase1 和 CppBase2)对虚基类的构造函数的调用,这就保证了虚基类的数据成员不会被多次初始化
*/ #include "pch.h"
#include "CppDerived.h"
#include "cppHelper.h" using namespace NativeDll; // 在无虚基类的情况下,派生类的构造函数中只需负责对其直接基类初始化
// 如果有虚基类,且虚基类中定义了带参数的构造函数,且没有定义默认构造函数,那么派生类不仅要负责对其直接基类进行初始化,还要负责对虚基类初始化
CppDerived::CppDerived(int number, string name, float salary, int age) :CppBaseVirtual(number), CppBase1(number, name, salary), CppBase2(number, name, age)
{ } string CppDerived::Show()
{
// 关于多重继承的二义性(ambiguous),如果派生类同时继承的多个基类有相同的成员,则调用这些成员时需显式指定其基类
return CppBase1::Name + " " + float2string(Salary) + " " + int2string(Age); // 另外:当然,如果派生类与多个基类有相同成员的话,那么基类中的这些与派生类相同的成员都会被隐藏掉(即派生类中的成员会覆盖基类中的成员)
}
5、示例
CppClass6.h
#pragma once #include <string> using namespace std; namespace NativeDll
{
class CppClass6
{
public:
string Demo();
};
}
CppClass6.cpp
/*
* 多重继承(multiple inheritance), 虚基类(virtual base class)
*/ #include "pch.h"
#include "CppClass6.h"
#include "CppDerived.h" using namespace NativeDll; string CppClass6::Demo()
{
CppDerived derived(, "webabcd", 100.0f, );
string result = derived.Show(); // cppbase1 webabcd 100.00 35 return result;
}
OK
[源码下载]
不可或缺 Windows Native (22) - C++: 多重继承, 虚基类的更多相关文章
- 不可或缺 Windows Native (21) - C++: 继承, 组合, 派生类的构造函数和析构函数, 基类与派生类的转换, 子对象的实例化, 基类成员的隐藏(派生类成员覆盖基类成员)
[源码下载] 不可或缺 Windows Native (21) - C++: 继承, 组合, 派生类的构造函数和析构函数, 基类与派生类的转换, 子对象的实例化, 基类成员的隐藏(派生类成员覆盖基类成 ...
- 不可或缺 Windows Native (20) - C++: 友元函数, 友元类
[源码下载] 不可或缺 Windows Native (20) - C++: 友元函数, 友元类 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 友元函数 友元类 示例演 ...
- 不可或缺 Windows Native 系列文章索引
[源码下载] 不可或缺 Windows Native 系列文章索引 作者:webabcd 1.不可或缺 Windows Native (1) - C 语言: hello c 介绍不可或缺 Window ...
- 不可或缺 Windows Native (23) - C++: 虚函数
[源码下载] 不可或缺 Windows Native (23) - C++: 虚函数 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 虚函数 示例1.基类CppHuman ...
- C++学习之路—继承与派生(三):多重继承与虚基类
(根据<C++程序设计>(谭浩强)整理,整理者:华科小涛,@http://www.cnblogs.com/hust-ghtao转载请注明) 多重继承是指一个派生类有两个或多个基类.例如,有 ...
- 不可或缺 Windows Native (25) - C++: windows app native, android app native, ios app native
[源码下载] 不可或缺 Windows Native (25) - C++: windows app native, android app native, ios app native 作者:web ...
- 不可或缺 Windows Native (24) - C++: 运算符重载, 自定义类型转换
[源码下载] 不可或缺 Windows Native (24) - C++: 运算符重载, 自定义类型转换 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 运算符重载 自 ...
- 不可或缺 Windows Native (19) - C++: 对象的动态创建和释放, 对象的赋值和复制, 静态属性和静态函数, 类模板
[源码下载] 不可或缺 Windows Native (19) - C++: 对象的动态创建和释放, 对象的赋值和复制, 静态属性和静态函数, 类模板 作者:webabcd 介绍不可或缺 Window ...
- 不可或缺 Windows Native (18) - C++: this 指针, 对象数组, 对象和指针, const 对象, const 指针和指向 const 对象的指针, const 对象的引用
[源码下载] 不可或缺 Windows Native (18) - C++: this 指针, 对象数组, 对象和指针, const 对象, const 指针和指向 const 对象的指针, con ...
随机推荐
- asp.net/html清理页面缓存的方法
(1) MVC BaseController: Controller内 protected override void Initialize(System.Web.Routing.RequestC ...
- KnockoutJS 3.X API 第三章 计算监控属性(1) 使用计算监控属性
计算监控属性(Computed Observables) 如果你有一个监控属性firstName,和另一个lastName,你要显示的全名?可以使用计算监控属性来实现-它依赖于一个或多个其他监控属性, ...
- Java基础-接口.编写2个接口:InterfaceA和InterfaceB;在接口InterfaceA中有个方法void printCapitalLetter();在接口InterfaceB中有个方法void printLowercaseLetter();然 后写一个类Print实现接口InterfaceA和InterfaceB,要求 方法 实现输出大写英文字母表的功能,printLowerca
#34.编写2个接口:InterfaceA和InterfaceB:在接口InterfaceA中有个方法void printCapitalLetter():在接口InterfaceB中有个方法void ...
- 通过Class获取标签,兼容的几种思路
在js中通过document.getElementsByClassName()在低版本IE浏览器中不兼容.然后我写了几种方案,大家可以参考参考. html代码 <!DOCTYPE html> ...
- 【SQL】姗姗来迟的SQL Server 安装图解
逆天今天被人拉过去装了个数据库...顺便就记录一下安装过程吧,一直想发一直没时间弄,今天就发下安装过程吧,2012和2008安装过程一样的 [注意安装细节] 安装过程中如果出现问题参 ...
- 【WP 8.1开发】自定义(RAW)通知的使用
继续前面的话题,还是推送通知.上一篇文章中遗留了RAW通知的推送没有给各位演示,特特地留到现在,不为别的,只为这个RAW通知有点意思,玩起来会比较有意思.官方文档将RAW通知译为“原始通知”,这里还是 ...
- 学习WPF——使用Font-Awesome图标字体
图标字体介绍 在介绍图标字体之前,不得不介绍图标格式ICON ICON是一种图标格式,我们操作系统中各种应用程序都包含一个图标 比如QQ程序的图标是一个可爱的企鹅,我的电脑是一个显示器图标 ----- ...
- 关于orapwd命令entries参数的探究
今日早上看Oracle官方文档<Administrator's Guide>时,在密码文件章节,关于orapwd命令entries参数的说明如下: This argument specif ...
- 探秘Tomcat(一)——Myeclipse中导入Tomcat源码
前言:有的时候自己不知道自己是井底之蛙,这并没有什么可怕的,因为你只要蜷缩在方寸之间的井里,无数次的生活轨迹无非最终归结还是一个圆形:但是可怕的是有一天你不得不从井里跳出来生活,需要重新审视井以外的生 ...
- 推荐20款基于 jQuery & CSS 的文本效果插件
jQuery 和 CSS 可以说是设计和开发行业的一次革命.这一切如此简单,快捷的一站式服务.jQuery 允许你在你的网页中添加一些真正令人惊叹的东西而不用付出很大的努力,要感谢那些优秀的 jQue ...