@@ -15,4 +15,253 @@ operator OP (type_of_X, type_of_Y)
1515- It give access to only once specific function.
1616### 1.3. Class
1717- Friend Class : I trust this class to see my private data
18- - It give access to all member functions of another class.
18+ - It give access to all member functions of another class.
19+
20+ ## 2. Class Relationship
21+ ### 2.1. Composition
22+ - **Part-of, strong ownership**
23+ - The member is part of the class
24+ - The member can only belong to one class at a time
25+ - The member has its existence managed by the class
26+ - The member does not know about the existence of the class
27+
28+ - UML:
29+ ```cpp
30+ +------------------------+
31+ | Car |
32+ +------------------------+
33+ | - engine : Engine |
34+ +------------------------+
35+ ♦
36+ │
37+ │
38+ +------------------------+
39+ | Engine |
40+ +------------------------+
41+ | + Engine() |
42+ | + ~Engine() |
43+ +------------------------+
44+
45+ class Engine
46+ {
47+ public:
48+ Engine() { std::cout << "Engine created\n"; }
49+ ~Engine() { std::cout << "Engine destroyed\n"; }
50+ };
51+
52+ class Car
53+ {
54+ private:
55+ Engine engine; // composition
56+
57+ public:
58+ Car() { std::cout << "Car created\n"; }
59+ ~Car() { std::cout << "Car destroyed\n"; }
60+ };
61+ ```
62+
63+ ## 2.2. Aggregations
64+ - ** Has-a, weak ownership**
65+ - The member is part of the class
66+ - The member can belong to more than one class at a time
67+ - The member does not have its existence managed by the class
68+ - The member does not know about the existence of the class
69+
70+ - UML:
71+ ``` cpp
72+ +-----------------------------+
73+ | Department |
74+ +-----------------------------+
75+ | - teacher : Teacher* |
76+ +-----------------------------+
77+ ◇
78+ │
79+ │
80+ +-----------------------------+
81+ | Teacher |
82+ +-----------------------------+
83+ | - name : std::string |
84+ +-----------------------------+
85+
86+ class Teacher
87+ {
88+ public:
89+ std::string name;
90+ Teacher(const std::string& n) : name(n) {}
91+ };
92+
93+ class Department
94+ {
95+ private:
96+ Teacher* teacher; // aggregation
97+
98+ public:
99+ Department(Teacher* t) : teacher(t) {}
100+ };
101+ ```
102+
103+ ## 2.3. Associations
104+ - ** Uses-a, loose relationship**
105+ - The associated member is otherwise unrelated to the class
106+ - The associated member can belong to more than one class at a time
107+ - The associated member does not have its existence managed by the class
108+ - The associated member may or may not know about the existence of the class
109+ - UML
110+ ``` cpp
111+ +---------------------+ +---------------------+
112+ | Doctor |-----------------| Patient |
113+ +---------------------+ +---------------------+
114+ | + treat(p:Patient&) | | - name : string |
115+ +---------------------+ +---------------------+
116+
117+ class Patient
118+ {
119+ public:
120+ std::string name;
121+ };
122+
123+ class Doctor
124+ {
125+ public:
126+ void treat(Patient& p)
127+ {
128+ std::cout << "Treating " << p.name << "\n";
129+ }
130+ };
131+ ```
132+
133+ ## 2.4. Dependency
134+ - ** Denpends-on**
135+ - One class uses another class to perform a task.
136+ - It is temporarily created, used, and then destroyed, or passed into a member function from an external source.
137+ - UML
138+ ``` cpp
139+ +---------------------+
140+ | Car |
141+ +---------------------+
142+ | + start() |
143+ +---------------------+
144+ - - - - - - - - - - - - - - - - - - >
145+ +---------------------+
146+ | Logger |
147+ +---------------------+
148+ | + log(msg:string) |
149+ +---------------------+
150+ class Logger
151+ {
152+ public:
153+ void log(const std::string& msg)
154+ {
155+ std::cout << msg << std::endl;
156+ }
157+ };
158+
159+ class Car
160+ {
161+ public:
162+ void start()
163+ {
164+ Logger logger; // dependency
165+ logger.log("Car started");
166+ }
167+ };
168+
169+ ```
170+ ## 2.5. Container
171+ - The class one class provides a container to hold multiple objects of another type
172+ - UML
173+ ``` cpp
174+ #include < vector>
175+
176+ +-----------------------------+
177+ | Library1 |
178+ +-----------------------------+
179+ | - books : vector<string> |
180+ +-----------------------------+
181+ ♦
182+ │
183+ │
184+ +-----------------------------+
185+ | string |
186+ +-----------------------------+
187+ class Library1
188+ {
189+ private:
190+ std::vector< std::string > books; // copy values
191+ };
192+
193+ +-----------------------------+
194+ | Library2 |
195+ +-----------------------------+
196+ | - teachers : vector<T*> |
197+ +-----------------------------+
198+ ◇
199+ │
200+ │
201+ +-----------------------------+
202+ | Teacher |
203+ +-----------------------------+
204+ class Library2
205+ {
206+ private:
207+ std::vector<Teacher* > teachers; // store pointers
208+ };
209+
210+ ```
211+
212+ ## 2.6. Inheritance
213+ - ** Is-a**
214+ ``` cpp
215+ +----------------------+
216+ | Animal |
217+ +----------------------+
218+ | + eat() |
219+ +----------------------+
220+ △
221+ │
222+ │
223+ +----------------------+
224+ | Dog |
225+ +----------------------+
226+ | + bark() |
227+ +----------------------+
228+ class Animal
229+ {
230+ public:
231+ void eat() { std::cout << "Eating\n"; }
232+ };
233+
234+ class Dog : public Animal
235+ {
236+ public:
237+ void bark() { std::cout << "Woof\n"; }
238+ };
239+ ```
240+
241+ ## 2.7 Embedded a.k.a Nested/Inner Class
242+ - Type-level containment
243+ ``` cpp
244+ +----------------------+
245+ | Car |
246+ +----------------------+
247+ | |
248+ +----------------------+
249+ (+)
250+ │
251+ │
252+ +----------------------+
253+ | Engine |
254+ +----------------------+
255+ | + start() |
256+ +----------------------+
257+
258+ class Car
259+ {
260+ public:
261+ class Engine
262+ {
263+ public:
264+ void start();
265+ };
266+ };
267+ ```
0 commit comments