1
+ #include < iostream>
2
+ #include < string>
3
+ // #include <typeinfo>
4
+
5
+ using namespace std ;
6
+
7
+ // // Type Conversion
8
+ // 1) implicit 2) explicit
9
+
10
+ // Type Conversion Operators in C++
11
+
12
+ // static_cast
13
+ // dynamic_cast
14
+ // const_cast
15
+ // reinterpret_cast
16
+
17
+
18
+
19
+
20
+ class Employee {
21
+ protected:
22
+ int id = 0 ;
23
+ public:
24
+ Employee () = default ;
25
+ Employee (int id) : id(id) {};
26
+
27
+ int getId () { return id; }
28
+ };
29
+
30
+
31
+ class Police : public Employee {
32
+ public:
33
+ Police (int id) : Employee(id) {};
34
+ int getValue () { return id * 100 ; }
35
+ };
36
+
37
+
38
+ class Teacher : public Employee {
39
+ public:
40
+ Teacher (int id) : Employee(id) {};
41
+ string method1 () { return " Sport is health" ; }
42
+ string method2 () { return " Put your mask" ; }
43
+ };
44
+
45
+
46
+ int main ()
47
+ {
48
+ Employee e (1 );
49
+ Police p (2 );
50
+ Teacher t (3 );
51
+
52
+
53
+ Employee e1 = p;
54
+ cout << e1 .getId () << endl;
55
+
56
+ // Police downCast = (Police)e1; // invalid
57
+
58
+ Employee ePtr = p;
59
+ cout << ePtr.getId () << endl;
60
+
61
+
62
+ Police& downCast = (Police&)ePtr;
63
+ cout << downCast.getValue () << endl;
64
+
65
+ }
66
+
67
+
68
+
69
+
70
+
71
+
72
+
73
+
74
+ // int main() {
75
+ //
76
+ // float f = 12.5f;
77
+ // int number = static_cast<int>(f);
78
+ // cout << number << endl;
79
+ //
80
+ // }
81
+
82
+
83
+ // class Int {
84
+ // int x;
85
+ //
86
+ // public:
87
+ // Int(int x): x(x){}
88
+ //
89
+ // explicit operator string() {
90
+ // return to_string(x);
91
+ // }
92
+ // };
93
+ //
94
+ //
95
+ // int main() {
96
+ // Int obj(10);
97
+ // string s = (string)obj;
98
+ //
99
+ // string s1 = static_cast<string>(obj);
100
+ //
101
+ // cout << s << endl;
102
+ // }
103
+
104
+
105
+
106
+
107
+
108
+
109
+
110
+
111
+
112
+
113
+ // class Employee {
114
+ // protected:
115
+ // int id = 0;
116
+ // public:
117
+ // Employee() = default;
118
+ // Employee(int id) : id(id) {};
119
+ //
120
+ // int getId() { return id; }
121
+ // };
122
+ //
123
+ // class Teacher : private Employee {
124
+ // public:
125
+ // Teacher(int id) : Employee(id) {};
126
+ // string method1() { return "Idman saglamliqdi"; }
127
+ // string method2() { return "Maskavi tax"; }
128
+ // };
129
+ //
130
+ //
131
+ // int main()
132
+ // {
133
+ // // Teacher t(10);
134
+ // // Employee e = static_cast<Employee>(t);
135
+ //
136
+ //
137
+ // // // Teacher t2 = static_cast<Teacher>(e); // invalid
138
+ // // Teacher* t3 = static_cast<Teacher*>(&e);
139
+ // // Teacher& t4 = static_cast<Teacher&>(e);
140
+ //
141
+ //
142
+ // // Teacher* t5 = (Teacher*)&e;
143
+ // // cout << t5->getId() << endl;
144
+ // }
145
+
146
+
147
+
148
+
149
+
150
+
151
+
152
+
153
+
154
+
155
+
156
+ // // dynamic_cast
157
+ //
158
+ //
159
+ //
160
+ //
161
+ // class ButtonBase {
162
+ // public:
163
+ // string Content = "";
164
+ // string Location = "";
165
+ //
166
+ // virtual void Click() { cout << "ButtonBase Click\n"; }
167
+ //
168
+ // virtual ~ButtonBase() = 0{};
169
+ // };
170
+ //
171
+ //
172
+ // class Button : public ButtonBase {
173
+ // public:
174
+ // void Click() { cout << "Button Click\n"; }
175
+ // };
176
+ //
177
+ // class CheckBox : public ButtonBase {
178
+ // public:
179
+ // void Click() { cout << "CheckBox Click\n"; }
180
+ // };
181
+ //
182
+ // class RadioButton : public ButtonBase {
183
+ // public:
184
+ // void Click() { cout << "RadioButton Click\n"; }
185
+ // };
186
+ //
187
+ //
188
+ //
189
+ // // dynamic_cast
190
+ //
191
+ // int main()
192
+ // {
193
+ // ButtonBase** buttons = new ButtonBase * []
194
+ // {
195
+ // new Button(),
196
+ // new CheckBox(),
197
+ // new RadioButton(),
198
+ // new CheckBox(),
199
+ // new Button(),
200
+ // new CheckBox(),
201
+ // new RadioButton()
202
+ // };
203
+ //
204
+ // int n = 7;
205
+ //
206
+ // for (size_t i = 0; i < n; i++)
207
+ // {
208
+ // if(dynamic_cast<CheckBox*>(buttons[i]) != NULL)
209
+ // buttons[i]->Click();
210
+ // }
211
+ // }
212
+
213
+
214
+ // polymorphic class - non polymorphic class
215
+
216
+
217
+
218
+
219
+
220
+
221
+
222
+
223
+
224
+ // // const_cast
225
+ //
226
+ //
227
+ // void show(int*& data) {
228
+ // *data = 100;
229
+ // cout << *data << endl;
230
+ // }
231
+ //
232
+ //
233
+ // int main() {
234
+ // const int number = 10;
235
+ // const int* ptr = &number;
236
+ //
237
+ // show(const_cast<int*&>(ptr));
238
+ // cout << *ptr << endl;
239
+ //
240
+ //
241
+ // // int* newPtr = const_cast<int*>(ptr);
242
+ // // *newPtr = 100;
243
+ // //
244
+ // //
245
+ // // // Undefined behavior
246
+ // // cout << number << endl;
247
+ // // cout << *ptr << endl;
248
+ // // cout << *newPtr << endl;
249
+ // }
250
+
251
+
252
+
253
+
254
+
255
+ // class A {
256
+ // public:
257
+ // int value;
258
+ //
259
+ // A(int v) : value(v) {}
260
+ //
261
+ // void show() const {
262
+ // (const_cast<A*>(this))->value = 100;
263
+ //
264
+ // cout << value << endl;
265
+ // }
266
+ // };
267
+ //
268
+ //
269
+ //
270
+ // void main() {
271
+ // A a(10);
272
+ // a.show();
273
+ // cout << a.value << endl;
274
+ // }
275
+
276
+
277
+
278
+
279
+
280
+
281
+
282
+
283
+
284
+
285
+ // reinterpret_cast
286
+
287
+
288
+ //
289
+ // struct Samsung {
290
+ // int a;
291
+ // int b;
292
+ // bool isCancel;
293
+ // char symbol;
294
+ // };
295
+
296
+ //
297
+ // void main()
298
+ // {
299
+ // Samsung s;
300
+ //
301
+ // s.a = 5;
302
+ // s.b = 100;
303
+ // s.isCancel = true;
304
+ // s.symbol = 'A';
305
+ //
306
+ // int* v = reinterpret_cast<int*>(&s);
307
+ // cout << *v << endl;
308
+ //
309
+ // v++;
310
+ //
311
+ // v = reinterpret_cast<int*>(v);
312
+ // cout << *v << endl;
313
+ //
314
+ // v++;
315
+ //
316
+ // bool* b = reinterpret_cast<bool*>(v);
317
+ // cout << *b << endl;
318
+ //
319
+ // b++;
320
+ //
321
+ // char* c = reinterpret_cast<char*>(b);
322
+ // cout << *c << endl;
323
+ // }
0 commit comments