首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

c++中的部类兼容规则(基类对象)

2012-11-22 
c++中的类型兼容规则(基类对象)#includeiostreamusing namespace stdclass Base1{public:void diaplay(

c++中的类型兼容规则(基类对象)

#include<iostream>using namespace std;class Base1{public:void diaplay() const{cout<<"enter Base1::display"<<endl;}};class Base2:public Base1{public:void diaplay() const{  cout<<"enter Base2::display"<<endl;}};class Base3:public Base2{public:void diaplay() const{cout<<"enter Base3::display"<<endl;}};void fun(Base1 *str){str->diaplay();}int main(){Base1 bb1;Base2 bb2;Base3 bb3;fun(&bb1);fun(&bb2);fun(&bb3);return 0;}


类型兼容规则是指在需要类型规则的任何地方,都可以使用公有派生类的对象来替代。在替代之后,派生类对象可以作为基类的对象使用,但只能使用从基类继承的成员。

 

热点排行