-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommand.cpp
More file actions
80 lines (65 loc) · 1.32 KB
/
Command.cpp
File metadata and controls
80 lines (65 loc) · 1.32 KB
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
//
// Created by osher on 14/02/2022.
//
#include <iostream>
#include <cstring>
using namespace std;
class Student {
char *_name;
public:
Student(const char *name) {
_name = new char[strlen(name) + 1];
strcpy(_name, name);
}
void sleep() {
cout << "I'm sleeping!! (" << _name << ")" << endl;
}
void eat() {
cout << "Yummy (" << _name << ")" << endl;
}
};
class Command {
public:
virtual void execute() = 0;
};
class SleepCommand : public Command {
Student *_student;
public:
void execute() {
_student->sleep();
}
SleepCommand(Student *student) {
_student = student;
}
};
class EatCommand : public Command {
Student *_student;
public:
EatCommand(Student *student) {
_student = student;
}
void execute() {
_student->eat();
}
};
class Parent {
Command **_commands;
public:
Parent(Student *student) {
_commands = new Command *[2];
_commands[0] = new SleepCommand(student);
_commands[1] = new EatCommand(student);
}
void makeStudentSleep() {
_commands[0]->execute();
}
void makeStudentEat() {
_commands[1]->execute();
}
};
int main() {
Student student("Israel");
Parent p(&student);
p.makeStudentEat();
p.makeStudentSleep();
}