1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
#include <iostream>
#include <string>
/* Program to simulate flowcharts, adding console applications.
Flowcharts are composed of cells, which are interconnected.
Cells are either linked to other Cells by yes or no, or they
do not have links. The former are called LinkCell, the later
are PassCell. PassCells display a message, then the program
either 'passes' to another Cell or exists.*/
using namespace std;
// Generic Cell class
class Cell {
protected:
string msg;
public:
string getMsg() { return msg; }
virtual void flow() = 0;
};
// Linked Cell class
class LinkCell : public Cell {
private:
string val;
Cell* yLink;
Cell* nLink;
public:
LinkCell( const string&, Cell*, Cell* );
void setVal();
string getVal(){ return val; }
void flow();
};
LinkCell::LinkCell ( const string& setMsg, Cell *yParam, Cell *nParam ) {
this->msg = setMsg;
this->yLink = yParam;
this->nLink = nParam;
}
void LinkCell::setVal(){
cout << this->getMsg() << " [y/n] ";
cin >> this->val;
while (( this->getVal()!="y" ) && ( this->getVal()!="n" )){
cout << "Please enter y or n: ";
cin >> val;
}
}
void LinkCell::flow(){
setVal();
if ( this->getVal()=="y" ) { this->yLink->flow(); }
if ( this->getVal()=="n" ) { this->nLink->flow(); }
}
// EndCell class
class EndCell : public Cell {
public:
EndCell( const string& );
void flow();
};
EndCell::EndCell ( const string& setMsg ) {
this->msg = setMsg;
}
void EndCell::flow() {
cout << this->getMsg() << endl;
}
// PassCell class
class PassCell : public Cell {
Cell *link;
public:
PassCell( const string&, Cell* );
void flow();
};
PassCell::PassCell ( const string& setMsg, Cell *param ) {
this->msg = setMsg;
this->link = param;
}
void PassCell::flow() {
cout << this->getMsg();
this->link->flow();
}
int main() {
PassCell wait_year( "Wait another year", &over_eighteen );
LinkCell over_eighteen( "Are they both over 18", &next_step, &wait_year;)
// ignore that next_step is not defined
over_eighteen.flow();
return 0;
}
|