codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
#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; }
Private
[
?
]
Run code
Submit