标签:
#include <iostream> class Person{ public: virtual void say(){ std::cout<<"person"<<std::endl; } }; class Student:public Person{ public: void say(){ std::cout<<"student"<<std::endl; } }; int main(){ Person per; Student stud; per.say(); stud.say(); //传递对象 per = stud; per.say(); //指针 Person *pPtr = &stud; pPtr->say(); //引用 Person &pRef = stud; pRef.say(); }
1. 基类中用virtual关键字表明方法
2. 采用父类的引用和指针调用多态方法,父类对象本身不能实现多态
标签:
原文地址:http://www.cnblogs.com/java-cjt/p/4261014.html