Gunslinger.cpp Gunslinger.cpp // Gunslinger.cpp #include "Gunslinger.h" #include using namespace std; GunSlinger::GunSlinger(){}; GunSlinger::GunSlinger(string newName){ name = newName; }...

Instructions and the original code are in the zip file


Gunslinger.cpp Gunslinger.cpp // Gunslinger.cpp #include "Gunslinger.h" #include  using namespace std; GunSlinger::GunSlinger(){}; GunSlinger::GunSlinger(string newName){     name = newName; } State GunSlinger::getState() {     return state; } string GunSlinger::getName() {     return name; } void GunSlinger::setState(State newState){     state = newState; } void GunSlinger::shot(){     setState(dead); } void GunSlinger::shoot(){     setState(shooting);     target->shot(); } void GunSlinger::blink(){     setState(blinking); } void GunSlinger::setTarget(GunSlinger* newTarget){     target = newTarget;     setState(duel); } Gunslinger.h // Gunslinger.h #pragma once #include using namespace std; enum State { duel, blinking, shooting, dead }; class GunSlinger { public: GunSlinger(); GunSlinger(string newName); State getState(); void setState(State newState); string getName(); void setTarget(GunSlinger* newTarget); void shot(); void shoot(); void blink(); private: GunSlinger* target; State state; string name; }; INSTRUCTIONS.txt ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // // This file should contain the student's instructions to the marker. // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // // Insert you compilation instructions below e.g. : // - platform/IDE/compiler used (mandatory). // - specific command-line compiler instruction (only if applicable) // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // // Insert other relevant notes below (not necessary): // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // // QUESTION 5 [15 marks] // // Command-line compilation: g++ std=c++11 *.cpp // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// The GunSlinger class (see Gunslinger.h/GunSlinger.cpp) represents gunslinger characters in a Western movie that are shown on the screen by playing a certain movie clip depending on the state of the character (realized by the Camera class - see Camera.h/Camera.cpp). The code as it is now can connect a camera to a Gunslinger and display a clip according to the state of the Gunslinger object, but it must call showClip() in order to display. Implement an observer pattern implemented using the given Observer class (see Observer.h/Observer.cpp) and Subject classe (see Subject.h/Subject.cpp) such that the camera objects will be automatically notified and updated when the gunslingers’ state is changed without showClip() having to be called by the main function. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // // PART 1 [5 marks] // // Make Gunslinger an observable class. // // Alter the GunSlinger class to make it an observable (i.e. subject) class, and include notification of the observer at appropriate // place(s) in the altered GunSlinger class. // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // // Part 2 [5 marks] // // Make Camera an observer class. // // The Camera class shows the GunSlinger on the screen according to the value of the state of the Gunslinger (the clip playing // process is here simplified as a textual console output description of the scene). Alter this Camera class as an observer class // that gets properly notified when a GunSlinger observable object is notifying its state change. // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // // Part 3 [5 marks] // // Provide a main function. // // Alter the existing DuelDriver.cpp file containing a main() function that creates two GunSlinger observable objects, then creates // two Camera observer objects and attaches one camera observer to each Gunslinger observable object. Then use the above-defined // classes to accomplish the following scenario written on the console output by the camera observers: // // Lee vanCleef: stands, tickly fingers // John Wayne: stands, tickly fingers // Lee vanCleef: sweating, blinks // John Wayne: lightning-fast shot // Lee vanCleef: knees bend, falls dead // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // // END OF QUESTION 5 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Observer.cpp Observer.cpp // Observer.cpp #include "Observer.h" Observer::Observer(){ }; Observer::~Observer(){ }; Observer.h // Observer.h #pragma once class Subject; class Observer { public: ~Observer(); virtual void Update() = 0; protected: Observer(); }; Subject.cpp Subject.cpp // Subject.cpp #include "Subject.h"                                   #include "Observer.h" Subject::Subject(){                                       _observers = new list; }; Subject::~Subject(){                                       delete _observers; }; void Subject::Attach(Observer* o){                         _observers->push_back(o); }; void Subject::Detach(Observer* o){                        _observers->remove(o); }; void Subject::Notify(){                                    list::iterator i = _observers->begin();     for (; i != _observers->end(); ++i)         (*i)->Update(); }; Subject.h // Subject.h #pragma once #include using namespace std; class Observer; class Subject { public: virtual void Attach(Observer* o); virtual void Detach(Observer* o); virtual void Notify(); Subject(); ~Subject(); private: list *_observers; }; Camera.cpp Camera.cpp // Camera.cpp #include "Camera.h" #include "Gunslinger.h" #include  using namespace std; Camera::Camera(){}; Camera::Camera(GunSlinger* newGunSligner){     shooter = newGunSligner; }; void Camera::showClip(){     switch (shooter->getState()) {     case duel:         cout < shooter->getName() <>< endl;         break;=""     case blinking:="">< shooter->getName() <>< endl;         break;=""     case dead:="">< shooter->getName() <>< endl;         break;=""     case shooting:="">< shooter->getName() <>< endl;         break;=""     }="" };="" camera.h="" camera.h="" #pragma="" once="" #include="" "gunslinger.h"="" class="" camera="" {="" public:="" camera();="" camera(gunslinger*="" newgunsligner);="" void="" showclip();="" private:="" gunslinger*="" shooter;="" };="" dueldriver.cpp="" dueldriver.cpp=""  dueldriver.cpp="" #include "gunslinger.h"                                         ="" #include "camera.h"=""> using namespace std; int main(){     GunSlinger* JohnWayne = new GunSlinger("John Wayne");            GunSlinger* LeeVanCleef = new GunSlinger("Lee vanCleef");     Camera* c1 = new Camera(LeeVanCleef);                            Camera* c2 = new Camera(JohnWayne);     LeeVanCleef->setTarget(JohnWayne);     c1->showClip();     JohnWayne->setTarget(LeeVanCleef);     c2->showClip();     LeeVanCleef->blink();                                            c1->showClip();     JohnWayne->shoot();     c2->showClip();     c1->showClip();     delete JohnWayne;     delete LeeVanCleef;     delete c1;     delete c2;     int i; cin >> i; };
Apr 20, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here