虚函数多态性小练习
#include <iostream.h>
class A
{
public:
virtual void print()
{
cout<<"A::print()"<<endl;
}
};
class B:public A
{
public:
virtual void print()
{
cout<<"B::print()"<<endl;
}
};
class C:public B
{
public:
virtual void print()
{
cout<<"C::print()"<<endl;
}
};
void print(A a)
{
a.print();
}
void print(A *pa)
{
pa->print();
}
void printS(A &a)
{
a.print();
}
void main()
{
A a,*pa,*pb,*pc;
B b;
C c;
pa=&a;
pb=&b;
pc=&c;
a.print();
b.print();
c.print();
pa->print();
pb->print();
pc->print();
print(a);
print(b);
print(c);
print(pa);
print(pb);
print(pc);
printS(a);
printS(b);
printS(c);
printS(*pa);
printS(*pb);
printS(*pc);
}
Fight fight fight ! 你有你的奇迹 !
Fight fight fight ! Just to be yourself !