代码: #includeiostreamusing namespace std;class A {int a;public: A():a(5){} virtual void print()const { couta;}};class B: public A { char b;public: B() { b='E'; } void print() const { coutb; }};void show(A x) { x.print(); }int main(){ A
代码:
#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;
}
运行结果:
5EE5E
Process returned 0 (0x0) execution time : 0.063 s
Press any key to continue.
总结: