/*
* File: main.cpp
* Author: yubao
*C++中类型的兼容规则,可以在积累对象出现的场合使用派生类对象进行替代,但是替代后的派生类仅仅发挥积累的俄作用。
* Created on May 30, 2009, 11:23 PM
*/
#include <iostream>
using namespace std;
class B0{
public:
void display()
{
cout<<"B0::display()"<<endl;
}
};
class B1:public B0
{
public:
void display()
{
cout<<"B1::display()"<<endl;
}
};
class D1:public B1
{
public:
void display()
{
cout<<"D1::display()"<<endl;
}
};
void fun(B0 *ptr)
{
ptr->display();
}
/*
*
*/
int main(int argc, char** argv) {
B0 b0;
B1 b1;
D1 d1;
B0 *p;
p=&b0;
fun(p);
p=&b1;
fun(p);
p=&d1;
fun(p);
return 0;
}
C++中类型兼容规则实例
最新推荐文章于 2021-06-28 10:18:04 发布