c++ 多继承


#include <iostream>
#include<string>
#include<exception>
#include"ClassBase.h"
#include"ClassDerived.h"
 

using namespace std;

class Base1Class
{
public:
    Base1Class(const string s) {}
    virtual void  method1() { cout << "from base1"<<endl; };

};

class Base2Class
{
public:
    Base2Class(const string s) {}
    virtual void  method1() { cout << "from base2" << endl; };
};

class DerivedClass :public Base1Class,public  Base2Class
{

    using Base1Class::Base1Class;
    using Base2Class::Base2Class; // 

public:
    // this is must be added,without  this,the instance can not  detect which to choose
    DerivedClass(const string s) :Base1Class(s), Base2Class(s) {};


};


int main()
{
    DerivedClass d("s");

    DerivedClass* dp = new DerivedClass("sss00");

    Base1Class* b1s = dp;

    b1s->method1();
    
    Base2Class* b2s = dp;

    b2s->method1();


    auto dp1 = make_shared<DerivedClass>("nn");

    shared_ptr<Base1Class> b1 = dp1;

    b1->method1();

    shared_ptr<Base2Class> b2 = dp1;

    b2->method1();


   

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值