1.继承的复习

1.1 类型转换

编译器认为访问范围缩小是安全的。

1.2 子类的构造与析构

子类中对基类构造函数初始化只能写在初始化表里,不能写在函数体中。

阻断继承。

1.3 子类的拷贝构造与拷贝赋值

2. 多重继承、钻石继承和虚继承

  • 多重继承

  一个类可以同时从多个基类继承实现代码。

  

  

  示例代码:

 #include <iostream>

 using namespace std;

 class Phone{
public:
Phone(string const& no):m_no(no){
cout << "Phone构造" << this << endl;
}
~Phone(void)
{
cout << "Phone析构" << this << endl;
}
void call (string const& no)
{
cout << m_no << "呼叫" << no << endl;
}
private:
string m_no;
}; class Player{
public:
Player(string const& media):m_media(media)
{
cout << "Player构造" << this << endl;
}
~Player(void)
{
cout << "Player析构" << this << endl;
}
void play(string const& clip)
{
cout << m_media << "播放" << clip << endl;
}
private:
string m_media;
}; class Computer{
public:
Computer(string const& os):m_os(os)
{
cout << "Computer构造" << this << endl;
}
~Computer(void)
{
cout << "Computer析构" << this << endl;
}
void run(string const& app)
{
cout << "在" << m_os << "上运行" << app << endl;
}
private:
string m_os;
}; class SmartPhone:public Phone, public Player, public Computer{
public:
SmartPhone (string const& no, string const& media, string const& os):Phone(no), Player(media), Computer(os){}
private:
}; int main(void)
{
SmartPhone sp("", "MP3/MP4/3GP", "Andriod");
sp.call("");
sp.play("我还年轻");
sp.run("JBG大战Victor Wang");
Phone* pPhone = &sp; //访问范围缩小,不会报错
cout << "&sp = " << &sp << endl;
cout << "pPhone = " << pPhone << endl;
Player* pPlayer = &sp;
cout << "pPlayer = " << pPlayer << endl;
Computer* pComputer = &sp;
cout << "pComputer = " << pComputer << endl;
/*
SmartPhone* pSmart = static_cast<SmartPhone*> (pComputer);
cout << "pSmart = " << pSmart << endl;
*/ /*
SmartPhone* pSmart = (SmartPhone*) pComputer;
cout << "pSmart = " << pSmart << endl;
*/ SmartPhone* pSmart = reinterpret_cast <SmartPhone*> (pComputer);
cout << "pSmart = " << pSmart << endl; return ;
}

  

  名字冲突问题:

  1.

 class C: public A, public B{
public:
using A::foo;
using B::foo; };

  2.

 /*
c.A::foo();
c.B::foo(100);
*/

  3.

 class C: public A, public B{
public:
/*
using A::foo;
using B::foo;
*/
void foo(int f, int x)
{
if(f == )
A::foo();
else if(f == )
B::foo(x);
}
};
  • 钻石继承

     一个子类继承自多个基类,而这些基类有源自共同的祖先,这样的继承结构成为钻石继承(菱形继承)。

  

  

  

  • 虚继承

  

  示例代码:

 #include <iostream>

 using namespace std;

 class A{
public:
A(int data):m_data(data)
{
cout << "A构造" << this << endl;
}
protected:
int m_data;
}; class B: virtual public A {
public:
B(int data): A(data)
{
cout << "B构造" << this << endl;
}
void set(int data)
{
cout << "B:" << &m_data << endl;
m_data = data;
}
}; class C: virtual public A {
public:
C(int data): A(data)
{
cout << "C构造" << this << endl;
}
int get (void) const
{
cout << "C:" << &m_data << endl;
return m_data;
}
}; class D: public B, public C{
public:
D(int data):B(data), C(data), A(data) {}
}; int main(void)
{
D d();
d.set();
cout << d.get() << endl;
cout << sizeof(d) << endl; return ;
}

虚继承是为了解决钻石继承的问题。

C++_day8_ 多重继承、钻石继承和虚继承的更多相关文章

  1. C++ 多继承和虚继承的内存布局(转)

    转自:http://www.oschina.net/translate/cpp-virtual-inheritance 警告. 本文有点技术难度,需要读者了解C++和一些汇编语言知识. 在本文中,我们 ...

  2. C++在单继承、多继承、虚继承时,构造函数、复制构造函数、赋值操作符、析构函数的执行顺序和执行内容

    一.本文目的与说明 1. 本文目的:理清在各种继承时,构造函数.复制构造函数.赋值操作符.析构函数的执行顺序和执行内容. 2. 说明:虽然复制构造函数属于构造函数的一种,有共同的地方,但是也具有一定的 ...

  3. 转载:C++ 多继承和虚继承的内存布局

    C++ 多继承和虚继承的内存布局[已翻译100%] 英文原文:Memory Layout for Multiple and Virtual Inheritance 标签: <无> run_ ...

  4. C++对象模型:单继承,多继承,虚继承

    什么是对象模型 有两个概念可以解释C++对象模型: 语言中直接支持面向对象程序设计的部分.对于各种支持的底层实现机制. 类中成员分类 数据成员分为静态和非静态,成员函数有静态非静态以及虚函数 clas ...

  5. C++ 继承之虚继承与普通继承的内存分布

    仅供互相学习,请勿喷,有观点欢迎指出~ class A { virtual void aa(){}; }; class B : public virtual A { ]; //加入一个变量是为了看清楚 ...

  6. C++对象模型:单继承,多继承,虚继承,菱形虚继承,及其内存布局图

    C++目前使用的对象模型: 此模型下,nonstatic数据成员被置于每一个类的对象中,而static数据成员则被置于类对象之外,static和nonstatic函数也都放在类对象之外(通过函数指针指 ...

  7. C++ 的多继承与虚继承

    C++之多继承与虚继承   1. 多继承 1.1 多继承概念 一个类有多个直接基类的继承关系称为多继承 多继承声明语法 class 派生类名 : 访问控制 基类名1, 访问控制 基类名2, ... { ...

  8. 2014 0416 word清楚项目黑点 输入矩阵 普通继承和虚继承 函数指针实现多态 强弱类型语言

    1.word 如何清除项目黑点 选中文字区域,选择开始->样式->全部清除 2.公式编辑器输入矩阵 先输入方括号,接着选择格式->中间对齐,然后点下面红色框里的东西,组后输入数据   ...

  9. C++:钻石继承与虚继承

    QUESTION:什么是钻石继承? ANSWER:假设我们已经有了两个类Father1和Father2,他们都是类GrandFather的子类.现在又有一个新类Son,这个新类通过多继承机制对类Fat ...

随机推荐

  1. 【Idea】-NO.163.Idea.2 -【How to show the horizontal scroll?】

    Style:Mac Series:Java Since:2018-09-10 End:2018-09-10 Total Hours:1 Degree Of Diffculty:5 Degree Of ...

  2. Go语言 切片长度和容量

    package main import "fmt" func main() { s := []int{2, 3, 5, 7, 11, 13} printSlice(s) // Sl ...

  3. Module 3 - Azure - Web Apps

     Module 3 - 微软云 Azure - Web Apps 1. Create new Web application in the Azure Portal Azure Portal -> ...

  4. Mockito常用方法及示例

    Mockit是一个开源mock框架,官网:http://mockito.org/,源码:https://github.com/mockito/mockito 要使用Mockit,首先需要在我们工程中引 ...

  5. Oracle 11g R2性能优化 SQL TRACE

    作为Oracle官方自带的一种基本性能诊断工具,SQL Trace可以用来评估当前正在运行的SQL语句的效率,同时为该语句生成统计信息等,并保存这些信息到指定路径下的跟踪文件(trace)当中.SQL ...

  6. 递归函数 Vue ElementUI

    对树形菜单的递归操作,首先应该对树形菜单数据进行整理,优化成自己需要的类型 比如Vue + ElementUI的动态侧边栏数据 export function routerRoleToPretty ( ...

  7. js某一元素在数组中的索引

    第一种:数组遍历 function search(arr,dst){ var i = arr.length; while(i-=1){ if (arr[i] == dst){ return i; } ...

  8. extjs +String2 +Spring 下的分页 以及返回json格式错误的问题

    首先,分页  很简单. 1前台extjs数据源, var shipMgrStore = Ext.create('Ext.data.Store', { model: 'App.ShipMgr.model ...

  9. 洛谷 K短路(魔法猪学院)

    A*+迪杰特斯拉... 第十一个点卡爆 不管了 #include<iostream> #include<algorithm> #include<cstring> # ...

  10. html5 css练习 画廊 元素旋转

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...