Program code:
# Include <iostream> using namespace STD; Class smallplant // small plant {public: // attack force virtual int attackpower () {return 10; // attack power 10 }}; class zombie // zombie {public: // attack power int destorypower () {return 15; // attack power: 15 }}; // void attack (smallplant * P, zombie * z) {If (p-> attackpower ()> Z-> destorypower () {cout <"plants beat botnets! "<Endl;} else {cout <" botnets beat plants! "<Endl ;}/// small plant family generates large plant class bigplant: Public smallplant {public: // attack force int attackpower () {return 20; // attack capability: 15 }}; void main () {smallplant P; // small plant zombie Z; // zombie bigplant P1; // large plants // Plants vs. botnets attack (& P, & Z); attack (& P1, & Z); System ("pause ");}
Execution result:
Application of polymorphism "Plants vs. botnets"