#include<iostream> using namespace std; class A { int a; public: A():a(5){} virtual void print()const { cout<<a;} }; class B: public A { char b; public: B() { b='E'; } void print() const { cout<<b; } }; void show(A &x) { x.print(); } int main() { A d1,*p; B d2; p=&d2; d1.print(); d2.print(); p->print(); show(d1); show(d2); return 0; }
运行结果:
原文地址:http://blog.csdn.net/ljd939952281/article/details/46313161