[ create a new paste ] login | about

Link: http://codepad.org/k3gn1FW0    [ raw code | output | fork ]

C++, pasted on Apr 6:
    #include <iostream>
    #include <string>
    #include <stdint.h>

    using namespace std;
    
    class Gun 
    {
    public:
       Gun() : _bullets(0) {}
       virtual ~Gun() {}
       void fire() {cout << "bang bang" << endl; _bullets--;}
       void load(const uint16_t bullets) {_bullets = bullets;}
       const int bullets() const {return _bullets;}
       
       static const uint16_t MAX_BULLETS = 17;
       
    protected:
       int _bullets;
     };
     
    class Soldier 
    {
    public:
       Soldier(const string &name, const Gun &gun) : _name(name), _gun(gun) {}
       virtual ~Soldier() {}
       const string& name() const;
       Gun& gun() {return _gun;}
    
    protected:
       string _name;
       Gun _gun;
    };
    
     
    int main (int argc, char const *argv[])
    {
       Gun gun; // initialize
       string name("Foo");
       Soldier soldier(name, gun);
    
       soldier.gun().load(Gun::MAX_BULLETS);
       
       for(size_t i = 0; i < Gun::MAX_BULLETS; ++i)
       {
         soldier.gun().fire();
         cout << "I have " << soldier.gun().bullets() << " left!" << endl;
       }
      return 0;
    }


Output:
bang bang
I have 16 left!
bang bang
I have 15 left!
bang bang
I have 14 left!
bang bang
I have 13 left!
bang bang
I have 12 left!
bang bang
I have 11 left!
bang bang
I have 10 left!
bang bang
I have 9 left!
bang bang
I have 8 left!
bang bang
I have 7 left!
bang bang
I have 6 left!
bang bang
I have 5 left!
bang bang
I have 4 left!
bang bang
I have 3 left!
bang bang
I have 2 left!
bang bang
I have 1 left!
bang bang
I have 0 left!


Create a new paste based on this one


Comments: