#include <iostream> using namespace std; //基类People class People{ public: People(char *name, int age); void display(); protected: char *m_name; int m_age; }; People::People(char *name, int age): m_name(name), m_age(age){} void People::display(){ cout<<m_name<<"今年"<<m_age<<"岁了,是个无业游民。"<<endl; } //派生类Teacher class Teacher: public People{ public: Teacher(char *name, int age, int salary); void display(); private: int m_salary; }; Teacher::Teacher(char *name, int age, int salary): People(name, age), m_salary(salary){} void Teacher::display(){ cout<<m_name<<"今年"<<m_age<<"岁了,是一名教师,每月有"<<m_salary<<"元的收入。"<<endl; } int main(){ People *p = new People("王志刚", 23); p -> display(); p = new Teacher("赵宏佳", 45, 8200); p -> display(); return 0; }运行结果:
#include <iostream> using namespace std; //基类People class People{ public: People(char *name, int age); virtual void display(); //声明为虚函数 protected: char *m_name; int m_age; }; People::People(char *name, int age): m_name(name), m_age(age){} void People::display(){ cout<<m_name<<"今年"<<m_age<<"岁了,是个无业游民。"<<endl; } //派生类Teacher class Teacher: public People{ public: Teacher(char *name, int age, int salary); virtual void display(); //声明为虚函数 private: int m_salary; }; Teacher::Teacher(char *name, int age, int salary): People(name, age), m_salary(salary){} void Teacher::display(){ cout<<m_name<<"今年"<<m_age<<"岁了,是一名教师,每月有"<<m_salary<<"元的收入。"<<endl; } int main(){ People *p = new People("王志刚", 23); p -> display(); p = new Teacher("赵宏佳", 45, 8200); p -> display(); return 0; }运行结果:
virtual
关键字,将成员函数声明为了虚函数(Virtual Function),这样就可以通过 p 指针调用 Teacher 类的成员函数了,运行结果也证明了这一点(赵宏佳已经是一名老师了,不再是无业游民了)。p->display();
这条语句,当 p 指向不同的对象时,它执行的操作是不一样的。同一条语句可以执行不同的操作,看起来有不同表现方式,这就是多态。int main(){ People p("王志刚", 23); Teacher t("赵宏佳", 45, 8200); People &rp = p; People &rt = t; rp.display(); rt.display(); return 0; }运行结果:
#include <iostream> using namespace std; //军队 class Troops{ public: virtual void fight(){ cout<<"Strike back!"<<endl; } }; //陆军 class Army: public Troops{ public: void fight(){ cout<<"--Army is fighting!"<<endl; } }; //99A主战坦克 class _99A: public Army{ public: void fight(){ cout<<"----99A(Tank) is fighting!"<<endl; } }; //武直10武装直升机 class WZ_10: public Army{ public: void fight(){ cout<<"----WZ-10(Helicopter) is fighting!"<<endl; } }; //长剑10巡航导弹 class CJ_10: public Army{ public: void fight(){ cout<<"----CJ-10(Missile) is fighting!"<<endl; } }; //空军 class AirForce: public Troops{ public: void fight(){ cout<<"--AirForce is fighting!"<<endl; } }; //J-20隐形歼击机 class J_20: public AirForce{ public: void fight(){ cout<<"----J-20(Fighter Plane) is fighting!"<<endl; } }; //CH5无人机 class CH_5: public AirForce{ public: void fight(){ cout<<"----CH-5(UAV) is fighting!"<<endl; } }; //轰6K轰炸机 class H_6K: public AirForce{ public: void fight(){ cout<<"----H-6K(Bomber) is fighting!"<<endl; } }; int main(){ Troops *p = new Troops; p ->fight(); //陆军 p = new Army; p ->fight(); p = new _99A; p -> fight(); p = new WZ_10; p -> fight(); p = new CJ_10; p -> fight(); //空军 p = new AirForce; p -> fight(); p = new J_20; p -> fight(); p = new CH_5; p -> fight(); p = new H_6K; p -> fight(); return 0; }运行结果:
本文链接:http://task.lmcjl.com/news/8633.html