【摘要】 虚函数实现多态
#include <iostream>using namespace std; //基类Peopleclass People{public: virtual void display(); //声明为虚函数};void People::display(){ cout<<“无业游民。”<<endl;} //派生类Teacherclass Teacher: p…
虚函数实现多态
#include <iostream>
using namespace std;
//基类People
class People{
public:
virtual void display(); //声明为虚函数
};
void People::display(){
cout<<"无业游民。"<<endl;
}
//派生类Teacher
class Teacher: public People{
public:
virtual void display(); //声明为虚函数
};
void Teacher::display(){
cout<<"是一名教师"<<endl;
}
int main(){
People *p = new People();
p -> display();
p = new Teacher();
p -> display();
return 0;
}
© 版权声明文章版权归作者所有,未经允许请勿转载。THE END
喜欢就支持一下吧
相关推荐