-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab 8.cpp
99 lines (93 loc) · 1.6 KB
/
lab 8.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
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
#include "stdafx.h"
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
class base {
public:
virtual void show() = 0; //pure virt func
base(int value) {
this->value = value;
}
protected:
int value;
};
class a :public base {
public:
a(int value) : base(value) {}
void show() {
cout << "class A value: " << value << endl;
}
};
class b :public base {
public:
b(int value) : base(value) {}
void show() {
cout << "class B value: " << value << endl;
}
};
class c :public base {
public:
c(int value) : base(value) {}
void show() {
cout << "class C value: " << value << endl;
}
};
class functor {
public:
virtual base* operator()(int value) = 0;
};
class funcA :public functor {
public:
base* operator()(int value) {
return (new a(value));
}
};
class funcB :public functor {
base* operator()(int value) {
return (new b(value));
}
};
class funcC :public functor {
base* operator()(int value) {
return (new c(value));
}
};
int main()
{
map<string, functor*> fab;
vector<base*> vcl;
funcA aa;
funcB bb;
funcC cc;
fab["A"] = &aa;
fab["B"] = &bb;
fab["C"] = &cc;
int N;
cin >> N;
string command;
cin >> command;
for (int i = 0; i < N; i++) {
if (command == "create") {
string cl;
int value;
cin >> cl >> value;
functor *fc = fab[cl];
if (fc == NULL) {
cout << "Only A B C classes availible" << endl;
}
else
{
base *bcl = (*fc)(value);
vcl.push_back(bcl);
}
}
if (command == "showall") {
for_each(vcl.begin(), vcl.end(), [](base *lb) {lb->show(); });
}
cin >> command;
}
return 0;
}