• C++_多重继承(多个父类或基类)


    C++_多重继承(多个父类或基类)

    1、简单实现多重继承
    注意:沙发床继承沙发和床

    #include 
    #include 
    #include 
    
    using namespace std;
    
    class Sofa {
    public:
    	void watchTV(void) { cout<<"watch TV"<<endl; }
    };
    
    class Bed {
    public:
    	void sleep(void) { cout<<"sleep"<<endl; }
    };
    
    class Sofabed : public Sofa, public Bed {
    };
    
    int main(int argc, char **argv)
    {
    	Sofabed s;
    	s.watchTV();
    	s.sleep();
    	return 0;
    }
    
    • 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

    2、多重继承造成的二义性的问题
    注意:沙发也有重量的床也有重量,当派生类沙发床想要,设置重量时候不知道改调用哪一个

    #include 
    #include 
    #include 
    
    using namespace std;
    
    class Sofa {
    private:
    	int weight;
    public:
    	void watchTV(void) { cout<<"watch TV"<<endl; }
    	void setWeight(int weight) { this->weight = weight; }
    	int getWeight(void) const { return weight; }
    };
    
    class Bed {
    private:
    	int weight;
    public:
    	void sleep(void) { cout<<"sleep"<<endl; }
    	void setWeight(int weight) { this->weight = weight; }
    	int getWeight(void) const { return weight; }
    };
    
    class Sofabed : public Sofa, public Bed {
    };
    
    int main(int argc, char **argv)
    {
    	Sofabed s;
    	s.watchTV();
    	s.sleep();
    
    	//s.setWeight(100); /* error, 有二义性 */
    	s.Sofa::setWeight(100);
    	
    	return 0;
    }
    
    • 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

    3、解决二义性的问题(引入虚拟继承)
    注意:虚拟继承架构
    在这里插入图片描述
    虚拟继承的内存情况
    在这里插入图片描述

    #include 
    #include 
    #include 
    
    using namespace std;
    
    class Furniture {
    private:
    	int weight;
    public:
    	void setWeight(int weight) { this->weight = weight; }
    	int getWeight(void) const { return weight; }
    };
    
    class Sofa : virtual public Furniture {
    private:
    	int a;
    public:
    	void watchTV(void) { cout<<"watch TV"<<endl; }
    };
    
    class Bed : virtual public Furniture {
    private:
    	int b;
    public:
    	void sleep(void) { cout<<"sleep"<<endl; }
    };
    
    class Sofabed : public Sofa, public Bed {
    private:
    	int c;
    };
    
    int main(int argc, char **argv)
    {
    	Sofabed s;
    	s.watchTV();
    	s.sleep();
    
    	s.setWeight(100);
    	
    	return 0;
    }
    
    • 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
  • 相关阅读:
    T027基于51单片机的智能窗帘窗户控制系统proteus仿真原理图PCB
    DevNet: Deviation Aware Networkfor Lane Detection
    【从入门到起飞】JavaSE—IO工具包(Commons-io,Hutool) (2)
    git安装与使用4.3
    pubsub消息订阅与发布
    10.20复习
    云原生Docker Cgroups资源控制操作
    Java高级面试题(二)-- JVM
    基于FPGA的EMAC模块与FIFO模块:高速数据传输与存储
    Centos安装RabbitMQ超详细(必须收藏)
  • 原文地址:https://blog.csdn.net/weixin_50183638/article/details/126266379