-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAggregation.cpp
68 lines (50 loc) · 1.42 KB
/
Aggregation.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
#include <iostream>
#include <vector>
#include <string>
class Teacher
{
private:
std::string m_name;
public:
Teacher(std::string name) : m_name {name} {}
std::string get_name() { return m_name; }
};
class Department
{
private:
std::vector<Teacher> m_teachers;
public:
Department() {}
void add(Teacher teacher) { m_teachers.push_back(teacher); }
std::vector<Teacher> get_teachers() { return m_teachers; }
friend std::ostream& operator<<(std::ostream& output, Department &department);
};
std::ostream& operator<<(std::ostream& output, Department &department)
{
std::vector<Teacher> teachers = department.get_teachers();
for (int i = 0; i < teachers.size(); ++i)
{
Teacher i_teacher = teachers[i];
output << i_teacher.get_name() << " ";
}
return output;
}
int main()
{
// Create a teacher outside the scope of the Department
Teacher t1{ "Bob" };
Teacher t2{ "Frank" };
Teacher t3{ "Beth" };
{
// Create a department and add some Teachers to it
Department department{}; // create an empty Department
department.add(t1);
department.add(t2);
department.add(t3);
std::cout << department << std::endl;
} // department goes out of scope here and is destroyed
std::cout << t1.get_name() << " still exists!\n";
std::cout << t2.get_name() << " still exists!\n";
std::cout << t3.get_name() << " still exists!\n";
return 0;
}