自考新教材-p272_3

源程序:

#include <iostream>
using namespace std;

class Base
{
public:
virtual void display() = 0;

};
class Derived1 :public Base
{
public:
void display()
{
cout << "Derived1 called" << endl;
}
};

class Derived2 :public Base
{
public:
void display()
{
cout << "Derived2 called" << endl;
}
};

void fun(Base *p)
{
p->display();
}

int main()
{
Derived1 b1;
Derived2 b2;
Base *p = &b1;
fun(p);
p = &b2;
fun(p);
system("pause");
return 1;
}

 

运行结果:

 

posted @ 2020-02-04 22:58  bobo哥  阅读(137)  评论(0编辑  收藏  举报