virtual多态
yichen
posted @ 2014年3月09日 14:39
in Essay
, 332 阅读
#include <iostream>
using namespace std;
class Student
{
public:
virtual void print()
{
cout << "A student!" << endl;
}
};
class Gstudent:public Student
{
public:
virtual void print()
{
cout << "G student!" << endl;
}
};
int main()
{
Student s1;
Student *ps;
Gstudent s2;
s1.print();
s2.print();
s2.Student::print();
ps = &s1;
ps->print();
ps = &s2;
ps->print();
ps->Student::print();
return 0;
}
1