首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

《挑战30天C++入门极限》图文例解C++类的多重继承与虚拟继承

//站点:www.cndev-lab.com //所有稿件均有版权,如要转载,请务必著名出处和作者 #include <iostream> using namespace std; class Vehicle { public: Vehicle(int weight = 0) { Vehicle::weight = weight; } void SetWeight(int weight) { cout<<"重新设置重量"<<endl; Vehicle::weight = weight; } virtual void ShowMe() = 0; protected: int weight; }; class Car:public Vehicle//汽车 { public: Car(int weight=0,int aird=0):Vehicle(weight) { Car::aird = aird; } void ShowMe() { cout<<"我是汽车!"<<endl; } protected: int aird; }; class Boat:public Vehicle//船 { public: Boat(int weight=0,float tonnage=0):Vehicle(weight) { Boat::tonnage = tonnage; } void ShowMe() { cout<<"我是船!"<<endl; } protected: float tonnage; }; class AmphibianCar:public Car,public Boat//水陆两用汽车,多重继承的体现 { public: AmphibianCar(int weight,int aird,float tonnage) :Vehicle(weight),Car(weight,aird),Boat(weight,tonnage) //多重继承要注意调用基类构造函数 { } void ShowMe() { cout<<"我是水陆两用汽车!"<<endl; } }; int main() { AmphibianCar a(4,200,1.35f);//错误 a.SetWeight(3);//错误 system("pause"); }   上面的代码从表面看,看不出有明显的语发错误,但是它是不能够通过编译的。这有是为什么呢?   这是由于多重继承带来的继承的模糊性带来的问题。   先看如下的图示:

01
您找到你想要的搜索结果了吗?
是的
没有找到

php面向对象第十四篇 接口 interface

接口: 第一个问题? 什么是多重继承? 这的多重继承的意思是说: 一个子类继承多个父类,就是多重继承了。 那为什么一个子类不能继承多个父类呢? 多重继承缺点:我成为觉得(继承)它的核心是子类继承父类,子类没有父类所具备的方法与属性的前提下。 多重继承子类继承多个父类是吧,如果两个父类都具备相同的方法,子类想要调用调用哪一个啊,兄弟。。 继承一个父类就不一样了呀。 接口的实现核心是什么? 因为接口是没有方法体的,所以实现了相当于想法变成了现实把。 记住,实现与覆盖虽然差不多,但是还是有一些不一样的,比如覆盖是覆盖具有方法体的,实现是覆盖没有方法体的啊(想法变成现实)。

01
领券