-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDependencyInversionPrinciple.cpp
More file actions
55 lines (44 loc) · 1 KB
/
DependencyInversionPrinciple.cpp
File metadata and controls
55 lines (44 loc) · 1 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
//
// Created by Amit Kumar on 27/09/23
//
#include "vector"
#include "iostream"
#include "utility"
using namespace std;
class TreeIterator {
public:
virtual vector<int> get_friends(int node) const = 0;
};
class Tree : public TreeIterator {
vector<pair<int,int>>edges;
public:
void add_edge(int u, int v) {
edges.emplace_back(u,v);
edges.emplace_back(v,u);
}
vector<int> get_friends(int node) const {
vector<int> result;
for (auto &[u,v]: edges) {
if (u == node) {
result.emplace_back(v);
}
}
return result;
}
};
class TreeTraversal {
public:
static void print_friends(const TreeIterator& tree, int node) {
for (auto &itr: tree.get_friends(node)) {
cout << node << " is connected with " << itr << endl;
}
}
};
int main() {
Tree tree;
tree.add_edge(1,2);
tree.add_edge(1,3);
tree.add_edge(1,4);
TreeTraversal::print_friends(tree, 1);
return 0;
}