Create All kinds of guns.
Design focus:
1 base class as interface
2. Inheritance classes are different classes.
3. Construct a factory class and create different classes as needed. You can input keywords or indexes.
# Pragma once # include
# Include
Using namespace std; // Base classclass Gun {public: virtual string description () {return "Generic Gun" ;}}; // Derived classclass MachineGun: public Gun {public: string description () {return "MachineGun-Gun for fast shoot" ;}}; class LaserGun: public Gun {public: string description () {return "LaserGun-Melt everything with heat" ;}}; class ShockwaveGun: public Gun {public: string description () {return "ShockwaveGun-shock down everything ";}}; class GofFactory_Gun {public: GofFactory_Gun () {} Gun * createGun (const string & type) {if ("MachineGun" = type) {return (new MachineGun );} else if ("LaserGun" = type) {return (new LaserGun);} else if ("ShockwaveGun" = type) {return (new ShockwaveGun );} else {return NULL ;}}; void GofFactory_Gun_Run () {GofFactory_Gun gofGun; Gun * g = gofGun. createGun ("MachineGun"); cout <
Description () <
Description () <
Description () <
Result: