Compare commits

..
6 Commits
Author SHA1 Message Date
portersky 5993dd1dc8 feat(cmat): Completed Ex61 Part 2 2026-10-08 10:16:23 +02:00
portersky 4e2e5f72a2 feat(cmat): Completed Ex61 Part 1 2026-10-08 10:08:48 +02:00
portersky 058179b643 feat(cmat): Completed Ex51 2026-10-07 15:13:55 +02:00
porterskyandQwen 3.8 27B a65e84e796 feat(cmat): pass hints to Ex42 observers
Extend the Ex42 observer pattern with Hint objects so each
observer receives the reason for the update instead of
querying the subject:

- Subject::notify() and Observer::update() now take a
  const Hint& argument.
- Add LightHint and WaitHint carrying the traffic light
  state and WAIT button state, defined in Pelican.h.
- Pole dispatches on hint type using dynamic_cast.
- The Pole constructor sets its initial lights directly
  from the Pelican's current state, since no notification
  is sent at startup. Stopgap until the initial update
  is handled properly.

Co-authored-by: Qwen 3.8 27B <qwen3.8_27b-q3_k_xl@lmstudio.local>
2026-10-07 11:31:16 +02:00
porterskyandQwen 3.8 27B 11b96e4410 docs(materials): update S3 905 material
Co-authored-by: Qwen 3.8 27B <qwen3.8_27b-q3_k_xl@lmstudio.local>
2026-10-06 16:26:54 +02:00
porterskyandQwen 3.8 27B 6e0e45110a feat(cmat): implement Ex41 observer pattern
Refactor the Pelican crossing exercise to use the Observer
pattern: Pelican acts as Subject and each Pole observes it,
replacing direct calls through the owned Pole pointer.

Co-authored-by: Qwen 3.8 27B <qwen3.8_27b-q3_k_xl@lmstudio.local>
2026-10-06 16:26:53 +02:00
22 changed files with 556 additions and 497 deletions
+1 -1
View File
@@ -4,7 +4,7 @@
class Observer {
public:
// TODO: declare the pure virtual function update()
virtual void update() = 0;
};
#endif
+6 -11
View File
@@ -3,29 +3,25 @@
#include "PelicanTimer.h"
#include "Pole.h"
Pelican::Pelican(int x, int y)
: pressed(false), RED_TIME(4), YELLOW_TIME(2), GREEN_TIME(3), FLASH_TIME(5), myPole(new Pole(x, y)),
Pelican::Pelican()
: pressed(false), RED_TIME(4), YELLOW_TIME(2), GREEN_TIME(3), FLASH_TIME(5),
state(GREEN_IDLE) {
myTimer = new PelicanTimer(*this, &Pelican::timerExpired);
myPole->setTrafficLights(getState());
myPole->setWaitLight(isPressed());
notify();
}
Pelican::~Pelican() { delete myPole; }
Pelican::~Pelican() { }
void Pelican::press(void) {
if (state == GREEN_IDLE) {
state = YELLOW_STATE;
myTimer->timeoutRequest(YELLOW_TIME);
pressed = true;
myPole->setTrafficLights(getState());
myPole->setWaitLight(isPressed());
} else if (state != RED_STATE) {
pressed = true;
myPole->setTrafficLights(getState());
myPole->setWaitLight(isPressed());
}
notify();
}
void Pelican::timerExpired(void) {
@@ -60,6 +56,5 @@ void Pelican::timerExpired(void) {
break;
}
myPole->setTrafficLights(getState());
myPole->setWaitLight(isPressed());
notify();
}
+4 -4
View File
@@ -7,12 +7,14 @@
#ifndef PELICAN_H
#define PELICAN_H
#include "Subject.h"
class PelicanTimer;
class Pole;
class Pelican {
class Pelican : public Subject {
public:
Pelican(int x, int y);
Pelican();
~Pelican();
// call when the WAIT button has been pressed
@@ -39,8 +41,6 @@ class Pelican {
// indicates whether the WAIT light should be on
bool pressed;
Pole *myPole;
// Timer that indicates when the lights should change
PelicanTimer *myTimer;
+7 -1
View File
@@ -1,8 +1,9 @@
#include "Pole.h"
#include "Screen.h"
Pole::Pole(int x, int y) : waitLight(new Light("WAIT", x + 6, y + 12, Screen::WHITE)) {
Pole::Pole(int x, int y, Pelican& pelican) : waitLight(new Light("WAIT", x + 6, y + 12, Screen::WHITE)), pelican(pelican) {
using namespace Screen; // we will discuss namespaces in the course
pelican.attach(this);
lights[RED_LIGHT] = new Light("*", x + 11, y + 3, RED);
lights[RED_MAN] = new Light("*<-=", x + 6, y + 4, RED);
@@ -57,3 +58,8 @@ void Pole::setTrafficLights(Pelican::PelicanState state) {
}
void Pole::setWaitLight(bool pressed) { waitLight->set(pressed ? Light::ON : Light::OFF); }
void Pole::update() {
setTrafficLights(pelican.getState());
setWaitLight(pelican.isPressed());
}
+6 -3
View File
@@ -3,22 +3,25 @@
#define POLE_H
#include "Light.h"
#include "Observer.h"
#include "Pelican.h"
class Pole {
class Pole : public Observer {
public:
Pole(int x, int y);
Pole(int x, int y, Pelican&);
~Pole();
void setTrafficLights(Pelican::PelicanState state);
void setWaitLight(bool pressed);
void update();
private:
enum LightName { RED_LIGHT, YELLOW_LIGHT, GREEN_LIGHT, GREEN_MAN, RED_MAN };
Light *lights[RED_MAN + 1];
Light *waitLight;
Pelican& pelican;
};
#endif
+8 -4
View File
@@ -1,8 +1,12 @@
#include "Subject.h"
#include "Observer.h"
// TODO: Implement the Subject::attach() member function that adds an Observer to the set
void Subject::attach(Observer* observer) {
m_observers.insert(observer);
}
// TODO: Implement the Subject::notify() member function that calls all the update functions
void Subject::notify() {
for (auto* observer : m_observers) {
observer->update();
}
}
+3 -3
View File
@@ -8,11 +8,11 @@ class Observer;
class Subject {
public:
// TODO: Declare the attach() function that adds an Observer to the set
void attach(Observer* observer);
protected:
// TODO: Declare the notify() function that calls all the update functions
void notify();
private:
// TODO: Define a set of Observer pointers
std::set<Observer*> m_observers;
};
#endif
+4 -1
View File
@@ -1,13 +1,16 @@
#include "Keyboard.h"
#include "Pelican.h"
#include "Pole.h"
#include "TimerSet.h"
#include <cctype> // provides toupper()
int main() {
Keyboard kbd;
Pelican thePelican(30, 0);
Pelican thePelican;
Pole pole1(20, 0, thePelican);
Pole pole2(40, 0, thePelican);
char c = 0;
+12 -10
View File
@@ -1,10 +1,12 @@
#ifndef OBSERVER_H
#define OBSERVER_H
class Observer {
public:
virtual void update() = 0;
};
#endif
#ifndef OBSERVER_H
#define OBSERVER_H
class Hint;
class Observer {
public:
virtual void update(const Hint&) = 0;
};
#endif
+57 -55
View File
@@ -1,55 +1,57 @@
#include "Pelican.h"
#include "PelicanTimer.h"
Pelican::Pelican() : pressed(false), RED_TIME(4), YELLOW_TIME(2), GREEN_TIME(3), FLASH_TIME(5), state(GREEN_IDLE) {
myTimer = new PelicanTimer(*this, &Pelican::timerExpired);
}
void Pelican::press(void) {
if (state == GREEN_IDLE) {
state = YELLOW_STATE;
myTimer->timeoutRequest(YELLOW_TIME);
pressed = true;
notify();
} else if (state != RED_STATE) {
pressed = true;
notify();
}
}
void Pelican::timerExpired(void) {
switch (state) {
case RED_STATE:
state = YELLOW_FLASHING;
myTimer->timeoutRequest(FLASH_TIME);
break;
case YELLOW_FLASHING:
state = GREEN_TIMING;
myTimer->timeoutRequest(GREEN_TIME);
break;
case GREEN_TIMING:
if (pressed) {
state = YELLOW_STATE;
myTimer->timeoutRequest(YELLOW_TIME);
} else {
state = GREEN_IDLE;
}
break;
case GREEN_IDLE:
// should never happen
break;
case YELLOW_STATE:
state = RED_STATE;
pressed = false;
myTimer->timeoutRequest(RED_TIME);
break;
}
notify();
}
#include "Pelican.h"
#include "PelicanTimer.h"
#include "Subject.h"
Pelican::Pelican() : pressed(false), RED_TIME(4), YELLOW_TIME(2), GREEN_TIME(3), FLASH_TIME(5), state(GREEN_IDLE) {
myTimer = new PelicanTimer(*this, &Pelican::timerExpired);
}
void Pelican::press(void) {
if (state == GREEN_IDLE) {
state = YELLOW_STATE;
myTimer->timeoutRequest(YELLOW_TIME);
pressed = true;
} else if (state != RED_STATE) {
pressed = true;
}
notify(LightHint(state));
notify(WaitHint(pressed));
}
void Pelican::timerExpired(void) {
switch (state) {
case RED_STATE:
state = YELLOW_FLASHING;
myTimer->timeoutRequest(FLASH_TIME);
break;
case YELLOW_FLASHING:
state = GREEN_TIMING;
myTimer->timeoutRequest(GREEN_TIME);
break;
case GREEN_TIMING:
if (pressed) {
state = YELLOW_STATE;
myTimer->timeoutRequest(YELLOW_TIME);
} else {
state = GREEN_IDLE;
}
break;
case GREEN_IDLE:
// should never happen
break;
case YELLOW_STATE:
state = RED_STATE;
pressed = false;
myTimer->timeoutRequest(RED_TIME);
break;
}
notify(LightHint(state));
notify(WaitHint(pressed));
}
+63 -51
View File
@@ -1,51 +1,63 @@
// ======================================================
//
// Represents a Pelican crossing
//
// ======================================================
#ifndef PELICAN_H
#define PELICAN_H
#include "Subject.h"
class PelicanTimer;
class Pelican : public Subject {
public:
Pelican();
// call when the WAIT button has been pressed
void press(void);
// states that a Pelican can be in
enum PelicanState { RED_STATE, YELLOW_FLASHING, GREEN_TIMING, GREEN_IDLE, YELLOW_STATE };
bool isPressed() const { return pressed; }
PelicanState getState() const { return state; }
private:
// state that this Pelican is in
PelicanState state;
// called by PelicanTimer when the lights need to change
void timerExpired(void);
const unsigned int RED_TIME;
const unsigned int YELLOW_TIME;
const unsigned int GREEN_TIME;
const unsigned int FLASH_TIME;
// indicates whether the WAIT light should be on
bool pressed;
// Timer that indicates when the lights should change
PelicanTimer *myTimer;
private:
// prevent general use of copy ctor and operator=
Pelican(const Pelican &);
Pelican &operator=(const Pelican &);
};
#endif
// ======================================================
//
// Represents a Pelican crossing
//
// ======================================================
#ifndef PELICAN_H
#define PELICAN_H
#include "Subject.h"
class PelicanTimer;
class Pelican : public Subject {
public:
Pelican();
// call when the WAIT button has been pressed
void press(void);
// states that a Pelican can be in
enum PelicanState { RED_STATE, YELLOW_FLASHING, GREEN_TIMING, GREEN_IDLE, YELLOW_STATE };
bool isPressed() const { return pressed; }
PelicanState getState() const { return state; }
private:
// state that this Pelican is in
PelicanState state;
// called by PelicanTimer when the lights need to change
void timerExpired(void);
const unsigned int RED_TIME;
const unsigned int YELLOW_TIME;
const unsigned int GREEN_TIME;
const unsigned int FLASH_TIME;
// indicates whether the WAIT light should be on
bool pressed;
// Timer that indicates when the lights should change
PelicanTimer *myTimer;
private:
// prevent general use of copy ctor and operator=
Pelican(const Pelican &);
Pelican &operator=(const Pelican &);
};
class LightHint : public Hint {
public:
LightHint(Pelican::PelicanState s) : state(s) { }
Pelican::PelicanState state;
};
class WaitHint : public Hint {
public:
WaitHint (bool p) : pressed(p) {}
bool pressed;
};
#endif
+80 -68
View File
@@ -1,68 +1,80 @@
#include "Pole.h"
#include "Screen.h"
Pole::Pole(int x, int y, Pelican &p) : waitLight(new Light("WAIT", x + 6, y + 12, Screen::WHITE)), myPelican(p) {
myPelican.attach(*this);
using namespace Screen; // we will discuss namespaces in the course
lights[RED_LIGHT] = new Light("*", x + 11, y + 3, RED);
lights[RED_MAN] = new Light("*<-=", x + 6, y + 4, RED);
lights[YELLOW_LIGHT] = new Light("*", x + 11, y + 5, YELLOW);
lights[GREEN_LIGHT] = new Light("*", x + 11, y + 7, GREEN);
lights[GREEN_MAN] = new Light("*|-<", x + 6, y + 7, GREEN);
write(" _____ ", x, ++y, GRAY);
write(" /___ /| ", x, ++y, GRAY);
write(" | | | ", x, ++y, GRAY);
write(" | |/| ", x, ++y, GRAY);
write(" |____| | ", x, ++y, GRAY);
write(" | |/| ", x, ++y, GRAY);
write(" | | | ", x, ++y, GRAY);
write(" |____|/ ", x, ++y, GRAY);
write(" _||__ ", x, ++y, GRAY);
write(" /___ /| ", x, ++y, GRAY);
write(" | | | ", x, ++y, GRAY);
write(" | | | ", x, ++y, GRAY);
write(" |____|/ ", x, ++y, GRAY);
write(" || ", x, ++y, GRAY);
write(" || ", x, ++y, GRAY);
write(" || ", x, ++y, GRAY);
write(" || ", x, ++y, GRAY);
write(" || ", x, ++y, GRAY);
write(" || ", x, ++y, GRAY);
write("========||===========", x, ++y, GRAY);
write(" / || / ", x, ++y, GRAY);
write("_ /_________ /_ ", x, ++y, GRAY);
write("/ / ", x, ++y, GRAY);
update();
}
Pole::~Pole() {
for (int i = 0; i < sizeof lights / sizeof(lights[0]); ++i)
delete lights[i];
delete waitLight;
}
void Pole::setTrafficLights(Pelican::PelicanState state) {
static const Light::LightState LightStates[Pelican::YELLOW_STATE + 1][RED_MAN + 1] = {
// red yellow green green man red man
{Light::ON, Light::OFF, Light::OFF, Light::ON, Light::OFF}, // red
{Light::OFF, Light::FLASH, Light::OFF, Light::FLASH, Light::OFF}, // y flash
{Light::OFF, Light::OFF, Light::ON, Light::OFF, Light::ON}, // g time
{Light::OFF, Light::OFF, Light::ON, Light::OFF, Light::ON}, // g idle
{Light::OFF, Light::ON, Light::OFF, Light::OFF, Light::ON} // yellow
};
for (int i = RED_LIGHT; i <= RED_MAN; i++) {
lights[i]->set(LightStates[state][i]);
}
}
void Pole::setWaitLight(bool pressed) { waitLight->set(pressed ? Light::ON : Light::OFF); }
void Pole::update() {
setTrafficLights(myPelican.getState());
setWaitLight(myPelican.isPressed());
}
#include "Pole.h"
#include "Pelican.h"
#include "Screen.h"
#include <typeinfo>
Pole::Pole(int x, int y, Pelican &p) : waitLight(new Light("WAIT", x + 6, y + 12, Screen::WHITE)), myPelican(p) {
myPelican.attach(*this);
using namespace Screen; // we will discuss namespaces in the course
lights[RED_LIGHT] = new Light("*", x + 11, y + 3, RED);
lights[RED_MAN] = new Light("*<-=", x + 6, y + 4, RED);
lights[YELLOW_LIGHT] = new Light("*", x + 11, y + 5, YELLOW);
lights[GREEN_LIGHT] = new Light("*", x + 11, y + 7, GREEN);
lights[GREEN_MAN] = new Light("*|-<", x + 6, y + 7, GREEN);
write(" _____ ", x, ++y, GRAY);
write(" /___ /| ", x, ++y, GRAY);
write(" | | | ", x, ++y, GRAY);
write(" | |/| ", x, ++y, GRAY);
write(" |____| | ", x, ++y, GRAY);
write(" | |/| ", x, ++y, GRAY);
write(" | | | ", x, ++y, GRAY);
write(" |____|/ ", x, ++y, GRAY);
write(" _||__ ", x, ++y, GRAY);
write(" /___ /| ", x, ++y, GRAY);
write(" | | | ", x, ++y, GRAY);
write(" | | | ", x, ++y, GRAY);
write(" |____|/ ", x, ++y, GRAY);
write(" || ", x, ++y, GRAY);
write(" || ", x, ++y, GRAY);
write(" || ", x, ++y, GRAY);
write(" || ", x, ++y, GRAY);
write(" || ", x, ++y, GRAY);
write(" || ", x, ++y, GRAY);
write("========||===========", x, ++y, GRAY);
write(" / || / ", x, ++y, GRAY);
write("_ /_________ /_ ", x, ++y, GRAY);
write("/ / ", x, ++y, GRAY);
setTrafficLights(myPelican.getState());
setWaitLight(myPelican.isPressed());
}
Pole::~Pole() {
for (int i = 0; i < sizeof lights / sizeof(lights[0]); ++i)
delete lights[i];
delete waitLight;
}
void Pole::setTrafficLights(Pelican::PelicanState state) {
static const Light::LightState LightStates[Pelican::YELLOW_STATE + 1][RED_MAN + 1] = {
// red yellow green green man red man
{Light::ON, Light::OFF, Light::OFF, Light::ON, Light::OFF}, // red
{Light::OFF, Light::FLASH, Light::OFF, Light::FLASH, Light::OFF}, // y flash
{Light::OFF, Light::OFF, Light::ON, Light::OFF, Light::ON}, // g time
{Light::OFF, Light::OFF, Light::ON, Light::OFF, Light::ON}, // g idle
{Light::OFF, Light::ON, Light::OFF, Light::OFF, Light::ON} // yellow
};
for (int i = RED_LIGHT; i <= RED_MAN; i++) {
lights[i]->set(LightStates[state][i]);
}
}
void Pole::setWaitLight(bool pressed) { waitLight->set(pressed ? Light::ON : Light::OFF); }
void Pole::update(const Hint& hint) {
const WaitHint* waitHintPtr;
waitHintPtr = dynamic_cast<const WaitHint*>(&hint);
const LightHint* lightHintPtr;
lightHintPtr = dynamic_cast<const LightHint*>(&hint);
if (waitHintPtr != nullptr) {
setWaitLight(waitHintPtr->pressed);
}
if (lightHintPtr != nullptr) {
setTrafficLights(lightHintPtr->state);
}
}
+28 -28
View File
@@ -1,28 +1,28 @@
#ifndef POLE_H
#define POLE_H
#include "Light.h"
#include "Observer.h"
#include "Pelican.h"
class Pole : public Observer {
public:
Pole(int x, int y, Pelican &p);
~Pole();
void setTrafficLights(Pelican::PelicanState state);
void setWaitLight(bool pressed);
void update();
private:
Pelican &myPelican;
enum LightName { RED_LIGHT, YELLOW_LIGHT, GREEN_LIGHT, GREEN_MAN, RED_MAN };
Light *lights[RED_MAN + 1];
Light *waitLight;
};
#endif
#ifndef POLE_H
#define POLE_H
#include "Light.h"
#include "Observer.h"
#include "Pelican.h"
class Pole : public Observer {
public:
Pole(int x, int y, Pelican &p);
~Pole();
void setTrafficLights(Pelican::PelicanState state);
void setWaitLight(bool pressed);
void update(const Hint& hint);
private:
Pelican &myPelican;
enum LightName { RED_LIGHT, YELLOW_LIGHT, GREEN_LIGHT, GREEN_MAN, RED_MAN };
Light *lights[RED_MAN + 1];
Light *waitLight;
};
#endif
+12 -12
View File
@@ -1,12 +1,12 @@
#include "Subject.h"
#include "Observer.h"
void Subject::attach(Observer &obs) { myObservers.insert(&obs); }
void Subject::notify() const {
for (ObsList::const_iterator iter = myObservers.begin(); iter != myObservers.end(); ++iter) {
(*iter)->update();
}
}
#include "Subject.h"
#include "Observer.h"
void Subject::attach(Observer &obs) { myObservers.insert(&obs); }
void Subject::notify(const Hint& hint) const {
for (ObsList::const_iterator iter = myObservers.begin(); iter != myObservers.end(); ++iter) {
(*iter)->update(hint);
}
}
+25 -20
View File
@@ -1,20 +1,25 @@
#ifndef SUBJECT_H
#define SUBJECT_H
class Observer;
#include <set>
class Subject {
public:
typedef std::set<Observer *> ObsList;
void attach(Observer &obs);
void notify() const;
private:
ObsList myObservers;
};
#endif
#ifndef SUBJECT_H
#define SUBJECT_H
class Observer;
#include <set>
class Hint {
public:
virtual ~Hint() = default;
};
class Subject {
public:
typedef std::set<Observer *> ObsList;
void attach(Observer &obs);
void notify(const Hint&) const;
private:
ObsList myObservers;
};
#endif
+36 -24
View File
@@ -1,24 +1,36 @@
#include "TimerSet.h"
#include "Timer.h"
#include <iostream>
TimerSet::TimerSet() : previousTime(time(NULL)) {}
void TimerSet::addATimer(Timer *t) { timers.push_back(t); }
void TimerSet::pollAllTimers() {
std::time_t timeNow;
if (time(&timeNow) > previousTime) {
previousTime = timeNow;
for (auto it = timers.begin(); it != timers.end(); it++) {
(*it)->tick();
}
}
}
TimerSet &TimerSet::Instance() {
// Meyer's Singleton implementation
static TimerSet pInstance;
return pInstance;
}
#include "TimerSet.h"
#include "Timer.h"
#include <iostream>
#include <thread>
TimerSet::TimerSet() : previousTime(time(NULL)), pollingThread([this]{poll10Hz();}) {
pollingThread.detach();
}
void TimerSet::addATimer(Timer *t) { timers.push_back(t); }
void TimerSet::pollAllTimers() {
std::time_t timeNow;
if (time(&timeNow) > previousTime) {
previousTime = timeNow;
for (auto it = timers.begin(); it != timers.end(); it++) {
(*it)->tick();
}
}
}
TimerSet &TimerSet::Instance() {
// Meyer's Singleton implementation
static TimerSet pInstance;
return pInstance;
}
void TimerSet::poll10Hz() {
using namespace std::chrono_literals;
while (true) {
auto const pause = 100ms;
pollAllTimers();
std::this_thread::sleep_for(pause);
}
}
+33 -29
View File
@@ -1,29 +1,33 @@
#ifndef TIMERSET_H
#define TIMERSET_H
#include <ctime>
#include <vector>
class Timer;
class TimerSet {
public:
void pollAllTimers();
void addATimer(Timer *);
private:
TimerSet();
std::vector<Timer *> timers;
std::time_t previousTime;
// Meyer's singleton
public:
static TimerSet &Instance();
private:
// prevent copying and assignment of the singleton
TimerSet(const TimerSet &);
TimerSet &operator=(const TimerSet &);
};
#endif
#ifndef TIMERSET_H
#define TIMERSET_H
#include <ctime>
#include <vector>
#include <thread>
class Timer;
class TimerSet {
public:
void pollAllTimers();
void addATimer(Timer *);
private:
TimerSet();
std::vector<Timer *> timers;
std::time_t previousTime;
std::thread pollingThread;
// Meyer's singleton
public:
static TimerSet &Instance();
private:
// prevent copying and assignment of the singleton
TimerSet(const TimerSet &);
TimerSet &operator=(const TimerSet &);
void poll10Hz();
};
#endif
+27 -32
View File
@@ -1,32 +1,27 @@
#include "Keyboard.h"
#include "Pelican.h"
#include "Pole.h"
#include "TimerSet.h"
#include <cctype> // provides toupper()
int main() {
Keyboard kbd;
Pelican thePelican;
Pole pole1(20, 0, thePelican);
Pole pole2(40, 0, thePelican);
char c = 0;
while (std::toupper(c) != 'Q') { // loop until 'Q' is pressed
while (!kbd.isCharAvail()) {
TimerSet::Instance().pollAllTimers(); // no key press to process, poll the timers
}
c = kbd.getChar(); // get the character
if (c == ' ') {
// it's a space, so press the WAIT button
thePelican.press();
}
}
return 0;
}
#include "Keyboard.h"
#include "Pelican.h"
#include "Pole.h"
#include "TimerSet.h"
#include <cctype> // provides toupper()
int main() {
Keyboard kbd;
Pelican thePelican;
Pole pole1(20, 0, thePelican);
Pole pole2(40, 0, thePelican);
char c = 0;
while (std::toupper(c) != 'Q') { // loop until 'Q' is pressed
c = kbd.getChar(); // get the character
if (c == ' ') {
// it's a space, so press the WAIT button
thePelican.press();
}
}
return 0;
}
+44 -37
View File
@@ -1,37 +1,44 @@
#include "Person.h"
#include "PersonException.h"
#include "YoungPerson.h"
#include <iostream>
using std::cout;
Person::Person(std::string name, int age) : name(name), age(age) {
if (age < 0) {
throw PersonException("negative age");
}
cout << "Person ctor\n";
Count++;
}
Person::Person(const Person &) {
cout << "Copy ctor\n";
Count++;
}
Person::~Person() {
cout << "Dtor\n";
Count--;
}
void Person::setSalary(int sal) {
if (sal < 0) {
throw PersonException("negative salary");
}
salary = sal;
}
int Person::GetCount() { return Count; }
int Person::Count = 0;
#include "Person.h"
#include "PersonException.h"
#include "YoungPerson.h"
#include <cstddef>
#include <iostream>
#include <memory>
using std::cout;
std::shared_ptr<Person> Person::GetPerson(std::string name, int age) {
if (age < 18) return std::make_shared<YoungPerson>(name, age);
return std::make_shared<Person>(name, age);
}
Person::Person(std::string name, int age) : name(name), age(age) {
if (age < 0) {
throw PersonException("negative age");
}
cout << "Person ctor\n";
Count++;
}
Person::Person(const Person &) {
cout << "Copy ctor\n";
Count++;
}
Person::~Person() {
cout << "Dtor\n";
Count--;
}
void Person::setSalary(int sal) {
if (sal < 0) {
throw PersonException("negative salary");
}
salary = sal;
}
int Person::GetCount() { return Count; }
int Person::Count = 0;
+31 -28
View File
@@ -1,28 +1,31 @@
#ifndef Person_h
#define Person_h
#include <string>
class Person {
public:
virtual ~Person();
static int GetCount();
Person(std::string name, int age);
void setSalary(int sal);
private:
Person(const Person &);
std::string name;
int age;
int salary;
static int Count;
};
#endif
#ifndef Person_h
#define Person_h
#include <memory>
#include <string>
class Person {
public:
virtual ~Person();
static int GetCount();
Person(std::string name, int age);
void setSalary(int sal);
static std::shared_ptr<Person> GetPerson(std::string name, int age);
private:
Person(const Person &);
std::string name;
int age;
int salary;
static int Count;
};
#endif
+69 -75
View File
@@ -1,75 +1,69 @@
#include <exception>
#include <string>
#include <vector>
#include "Person.h"
#include "PersonException.h"
#include <iostream>
using std::cin;
using std::cout;
using std::string;
using std::exception;
int main() {
std::vector<Person *> people;
string namePrompt = "Enter name: ";
string name;
string agePrompt = "Enter age: ";
int age;
string salaryPrompt = "Enter salary: ";
int salary;
cout << namePrompt;
cin >> name;
while (name != "q") {
try {
cout << agePrompt;
cin >> age;
if (!cin) {
cin.clear();
string dummy;
cin >> dummy;
throw PersonException("invalid age");
}
Person *personPtr = new Person(name, age);
cout << salaryPrompt;
cin >> salary;
if (!cin) {
cin.clear();
string dummy;
cin >> dummy;
throw PersonException("invalid salary");
}
personPtr->setSalary(salary);
people.push_back(personPtr);
}
catch (exception &e) {
cout << "PersonException thrown: " << e.what() << '\n';
}
cout << "There are " << Person::GetCount() << " Person(s), " << people.size() << " in vector\n";
cout << namePrompt;
cin >> name;
}
// delete all the person(s) in the vector
for (std::vector<Person *>::iterator iter = people.begin(); iter != people.end(); ++iter) {
delete *iter;
}
cout << "There are " << Person::GetCount() << " Person(s) left dangling\n";
return 0;
}
#include <exception>
#include <memory>
#include <string>
#include <vector>
#include "Person.h"
#include "PersonException.h"
#include <iostream>
using std::cin;
using std::cout;
using std::string;
using std::exception;
int main() {
std::vector<std::shared_ptr<Person>> people;
string namePrompt = "Enter name: ";
string name;
string agePrompt = "Enter age: ";
int age;
string salaryPrompt = "Enter salary: ";
int salary;
cout << namePrompt;
cin >> name;
while (name != "q") {
try {
cout << agePrompt;
cin >> age;
if (!cin) {
cin.clear();
string dummy;
cin >> dummy;
throw PersonException("invalid age");
}
auto personPtr = Person::GetPerson(name, age);
cout << salaryPrompt;
cin >> salary;
if (!cin) {
cin.clear();
string dummy;
cin >> dummy;
throw PersonException("invalid salary");
}
personPtr->setSalary(salary);
people.push_back(personPtr);
}
catch (exception &e) {
cout << "PersonException thrown: " << e.what() << '\n';
}
cout << "There are " << Person::GetCount() << " Person(s), " << people.size() << " in vector\n";
cout << namePrompt;
cin >> name;
}
return 0;
}
Binary file not shown.