C++ 类的多重继承

1.多继承

多继承是指一个子类继承多个父类。多继承对父类的个数没有限制,继承方式可以是公共继承、保护继承和私有继承,

不写继承方式,默认是private继承


多继承举例:

#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;


/**
 * 定义工人类: Worker
 * 数据成员: m_strName
 * 成员函数: work()
 */
class Worker
{
public:
    Worker(string name)
{
m_strName = name;
cout << "Worker" << endl;
}
virtual ~Worker()
{
cout << "~Worker" << endl;
}
void work()
{
cout << m_strName << endl;
cout << "work" << endl;
}
protected:
string m_strName;
};


/**
 * 定义儿童类: Children
 * 数据成员: m_iAge
 * 成员函数: play()
 */
class Children
{
public:
Children(int age)
{
m_iAge = age;
cout << "Children" << endl;
}
~Children()
{
cout << "~Children" << endl;
}
void play()
{
cout << m_iAge << endl;
cout << "play" << endl;
}
protected:
int m_iAge;
};



/**
 * 定义童工类: ChildLabourer
 * 公有继承工人类和儿童类
 */
class ChildLabourer :public Worker,public Children
{
public:
ChildLabourer(string name, int age):Worker(name),Children(age)
{
cout << "ChildLabourer" << endl;
}


~ChildLabourer()
{
cout << "~ChildLabourer" << endl;
}
};


int main(void)
{
    // 使用new关键字创建童工类对象
Worker * p = new ChildLabourer("jack",14);
    // 通过童工对象调用父类的work()和play()方法
p->work();
   // p->play();
    // 释放
    delete p;
    p = NULL;


return 0;
}

输出结果:

Worker
Children
ChildLabourer
jack
work
~ChildLabourer
~Children
~Worker
由输出结果可以看出,在多继承中,任何父类的指针都可以指向子类的对象,在实例化子类时,先根据继承的顺序依次调用父类的构造函数,然后再调用该子类自己的构造函数;
用delete销毁该基类对象时,如果该基类的析构函数不是虚析构函数,且该基类的继承顺序在第一个,如上面的Worker类,(class ChildLabourer :public Worker,public Children)那么delete 父类的时候只会调用父类Worker的析构函数,系统不会出错,但是如果继承的时候顺序不是在第一位(class ChildLabourer :public Children,public Worker),就会报内存泄露的错误,如下图所示,如果父类的析构函数是虚析构函数,那么销毁的时候会先调用子类的析构函数再调用所有父类的析构函数,注意,此时,子类的父类的析构函数都会被调用!!!


2.多重继承

多重继承特点总结如下:

(1)多重继承与多继承不同,当B类从A类派生,C类从B类派生,此时称为多重继承

(1)当实例化子类时,会首先依次调用所有基类的构造函数,最后调用该子类的构造函数;销毁该子类时,则相反,先调用该子类的析构函数,再依次调用所有基类的析构函数。

(2)无论继承的层级有多少层,只要它们保持着直接或间接的继承关系,那么子类都可以与其直接父类或间接父类构成 is a的关系,并且能够通过父类的指针对直接子类或间接子类进行相应的操作,子类对象可以给直接父类或间接父类的对象或引用赋值或初始化。



在写这一主题的文章之前,在网上找到一篇很非常好的文章C++之继承与多态。就没有必要做重复造轮子的事件了,那就从这篇文章开始吧!

在c++中一个类可以从多个基类中派生(即可以有多个父类),这就是多继承。多继承的方式主要有两种:

1. 简单版本

这里写图片描述 

类C会同时拥有类A和类B的特性(属性和方法,也就是两个类的所有成员)。这种方式很简单这里就不多说,主要讲下面这种方式。


2. 复杂版本


这里写图片描述 

同样的,这个结构中类C也会同时拥有类A和类B的特性,但这就会有一个问题,类B1和B2都继承自A,那么类C的对象会同时包含两个A的对象。这样就会带来很多歧义性。

我们看一个典型的例子“沙发-床”: 

这里写图片描述 
类的关系图

代码:

#pragma once

#include <iostream>
#include <string>

using namespace std;

class Furniture
{
public:
    Furniture(void) : m_weight(0){}
    Furniture(double weight) : m_weight(weight){}
    ~Furniture(void){}

    double GetWeight() const { return m_weight; }
    void SetWeight(double val) { m_weight = val; }

private:
    double m_weight;

};

class Bed : public Furniture
{
public:
    Bed() : Furniture(), m_second(0) {}
    Bed(double weight, int second) : Furniture(weight), m_second(second){}

    void Sleep(int second) 
    {
        m_second = second;
        cout << "休息" << m_second << "秒..."<< endl;
    }


private:
    int m_second;

};

class Sofa : public Furniture
{
public:
    Sofa() : Furniture() {}
    Sofa(double weight) : Furniture(weight){}

    void WatchTV(string  programme)
    {
        cout << "正在看" << programme << "节目..." << endl;
    }
};

class SleepSofa : public Bed, public Sofa
{
public:
    SleepSofa() : Bed(), Sofa() {}
    SleepSofa(double weight, int second) : Bed(weight, second), Sofa(weight) {}

    void FoldOut()
    {
        cout << "展开沙发当床用." << endl;
        Sleep(360);
    }
};
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64

SleepSofa类继承自Bed和Sofa两个类,因此,SleepSofa类拥有这两个类的特性,但在实际编码中会存在如下几个问题。

1.SleepSofa类该如何定义?

Class SleepSofa : public Bed, public Sofa
{
    …
}

  
  
  • 1
  • 2
  • 3
  • 4
  • 5

构造顺序为:Bed sofa sleepsofa (也就是书写的顺序)

2.Bed和Sofa类都继承自Furniture,都有Weight属性也都有GetWeight和SetWeight方法,在SleepSofa类中使用这些属性和方法时,如何确定调用的是哪个类的成员?

我们看一下测试样例:

void Test()
{
    SleepSofa sleepSofa;
    sleepSofa.SetWeight(55);
    double weight = sleepSofa.GetWeight();
}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

这时会有以下错误:

.cpp(76): error C3861: ‘SetWeight’: identifier not found 
error C2385: ambiguous access of ‘GetWeight’

就是说SetWeight和GetWeight是有歧义的。

解决方法: 
(1). 可以使用完全限定名(即加上类的作用域)的方式,比如:

SleepSofa sleepSofa;
sleepSofa.Bed::SetWeight(55);
sleepSofa.Sofa::SetWeight(80);

  
  
  • 1
  • 2
  • 3
  • 4

这时可以看到sleepSofa对象有两个Furniture对象。如下: 

这里写图片描述

(2). 虚继承 
倘若,我们定义一个SleepSofa对象,让我们分析一下它的构造过程:它会构造Bed类和Sofa类,但Bed类和Sofa类都有一个父类,因此Furniture类被构造了两次,这是不合理的,因此,我们引入了虚继承的概念。

class Furniture{……};

class Bed : virtual public Furniture{……}; // 这里我们使用虚继承

class Sofa : virtual public Furniture{……};// 这里我们使用虚继承

class SleepSofa : public Bed, public Sofa {……};

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

这样,Furniture类就只会构造一次,sleepSofa对象只会包含一个Furniture对象。 
我们看一下测试样例:

SleepSofa sleepSofa;
sleepSofa.SetWeight(80);

  
  
  • 1
  • 2
  • 3

这时我们Debug模式可以看到SleepSofa的m_weight值都是80。 

这里写图片描述

这里虽然显示着两个Furniture对象,但其实指向的是同一个对象。我们可以看看它们的地址都是一样的。 

这里写图片描述



总结

  1. 在程序设计中最好不要出现多继承,要有也是继承多个作为接口使用抽象类(只声明需要的功能,没有具体的实现)。因为出现一般的多继承本身就是一种不好的面向对象程序设计。
  2. 在出现版本2的多继承时使用虚继承的方式。 



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值