-
Notifications
You must be signed in to change notification settings - Fork 28
/
Static_Dynamic_Binding_III.cpp
85 lines (61 loc) · 2.32 KB
/
Static_Dynamic_Binding_III.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <bits/stdc++.h>
using namespace std;
/*
static and dynamic binding III
Polymorphism Rule
-If a member in inheritance hierarchy has virtual function then, the hierarchy is of Polymorphic type
-If a function is virtual in a class and if this class has been derived, then this function is a virtual function
in derived class as well
RULE TO REMEBER:
-Once a virtual, continues to be virtual in any specialization (inheritance hierarchy)
-But, non-virtual function can be made virtual at a point and from that point onwards it continues to be virtual down the hierarchy
*/
class A {
public:
void f() {
cout << "A::f()" << endl; //non-virtual
}
virtual void g() {
cout << "A::g()" << endl; //virtual
}
void h() {
cout << "A::h()" << endl; //non-virtual
}
};
class B: public A {
public:
//overriding functions
void f() {
cout << "B::f()" << endl; //non-virtual
}
void g() {
cout << "B::g()" << endl; //virtual (still virtual because it's inheriting from A
}
virtual void h() {
cout << "B::h()" << endl; //virtual (I made it virtual here)
}
};
class C : public B {
public:
void f() {
cout << "C::f()" << endl; //non-virtual
}
void g() {
cout << "C::g()" << endl; //virtual
}
void h() {
cout << "C::h()" << endl; //virtual (becomes virtual as it inherits from B)
}
};
int main() {
B* q = new C; //upcast
A *p = q; //upcast
p->f(); //Since f() is non-virtual A(p is of type A), So compiler statically binds it to A::f() (type of pointer p was A*)
p->g(); //Since g() is virtual function in A(p is of type A), so late binding (dynamic binding) is done to C::g() (p was pointing to object of C)
p->h(); ////Since h() is non-virtual in A(p is of type A), So compiler statically binds it to A::h() (type of pointer p was A*)
///////
q->f(); //Since f() is non-virtual in B(q is of type B), So compiler statically binds it to B::f() (type of pointer q was B*)
q->g(); //Since g() is virtual in B(q is of type B), so late binding (dynamic binding) is done to C::g() (q was pointing to object of C)
q->h(); ////Since h() is virtual in B(q is of type B), So compiler dynamically binds it to C::h() (q was pointing to object C)
return 0;
}