抽象类在多继承中的应用

工程上的多继承
被实际开发经验抛弃的多继承
工程开发中真正意义上的多继承是几乎不被使用的
多重继承带来的代码复杂性远多于其带来的便利
多重继承对代码维护性上的影响是灾难性的
在设计方法上,任何多继承都可以用单继承代替
多继承中的二义性和多继承不能解决的问题
在这里插入图片描述

class Parent{
public:
	int getA(){
		m_a = 10;
		cout << "this is Parent getA()\n";
		return m_a;
	}
private:
	int m_a;
};
class Interface1{
public:
	virtual double add(double, double) = 0;
};
class Interface2{
public:
	virtual double mul(double, double) = 0;
};
class Child :public Parent, public Interface1, public Interface2{
	double add(double a, double b){
		cout << "this is Child add()\n";
		return a + b;
	}
	double mul(double a, double b){
		cout << "this is Child mul()\n";
		return a * b;
	}
};
Main:
	Interface1* t1;
	Interface2* t2;
	Child c;
	c.getA();//this is Parent getA()
	t1 = &c;
	t1->add(10, 20);//this is Child add()
	t2 = &c;
	t2->mul(12, 13);//this is Child mul()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值