-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate.cpp
53 lines (47 loc) · 1.09 KB
/
state.cpp
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
/**
* \file
* \brief
*
* \todo
*/
/*
State is like an enum, but we can define different behaviour for each state.
We could add another class,
Sad, which did something entirely different when asked to talk().
*/
#include <iostream>
//-------------------------------------------------------------------------------------------------
class IMood
{
public:
virtual ~IMood() { }
virtual void talk() = 0;
};
//-------------------------------------------------------------------------------------------------
class Happy : public IMood
{
public:
void talk()
{
std::cout << "Happy mood!" << std::endl;
}
};
//-------------------------------------------------------------------------------------------------
class Sad : public IMood
{
public:
void talk()
{
std::cout << "Sad mood!" << std::endl;
}
};
//-------------------------------------------------------------------------------------------------
int main()
{
Happy happy_mood;
happy_mood.talk();
Sad sad_mood;
sad_mood.talk();
return 0;
}
//-------------------------------------------------------------------------------------------------