-
Notifications
You must be signed in to change notification settings - Fork 21
/
Type_Casting.cpp
61 lines (42 loc) · 1.29 KB
/
Type_Casting.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
#include <iostream>
using namespace std;
/*
Dynamic Binding Part I
Type Casting
-(Implicit) Casting between unrelated classes is not possible
-upcasting is allowed in inheritance
-downcasting is not allowed in inheritance
*/
class A {
public:
int dataA_;
};
class B : public A {
public:
int dataB_;
};
int main() {
A a;
B b;
A *pa = NULL;
B *pb = NULL;
void *pv = NULL;
pa = pb; //pb casted to pa (UPCAST) allowed
//pb = pa; //ERROR : downcast not allowed
pv = pa; //Allowed : BUT, pa is converted to void* (So, pa loses the type)
pv = pb; //Allowed : BUT, pb is converted to void* (So, pb loses the type)
// pa = pv; //ERROR : Cannot convert void* to A*
// pb = pv; //ERROR : Cannot convert void* to B*
a.dataA_ = 2;
b.dataB_ = 3;
b.dataA_ = 5;
pa = &a;
pb = &b;
cout << pa->dataA_ << endl; //2
cout << pb->dataA_ << " " << pb->dataB_ << endl; //3 5
pa = &b; //UP casting A* type to B* type. Allowed (Because Class B has object of A as well in its orientation)
cout << pa->dataA_ << endl; //3
//cout << pa->dataB_ << endl; //ERROR : class A has no member named dataB_
//i.e. pa is a pointer of A* type and class A has no member called dataB_
return 0;
}