-
Notifications
You must be signed in to change notification settings - Fork 7
/
virtualFunctions.cpp
84 lines (68 loc) · 2.06 KB
/
virtualFunctions.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
#include<bits/stdc++.h>
using namespace std;
// Classes A and B are parent & child classes respectively without using virtual functions
class A {
public:
void print() {
cout << "In print function of class A\n";
}
};
class B: public A {
public:
void print() {
cout << "In print function of class B\n";
}
};
// Classes C and D are parent & child classes respectively using virtual functions
class C {
public:
/*
This virtual keyword tells the compiler that late binding(binding at runtime) should
take place for this function
Working concept behind virtual function link:
https://www.youtube.com/watch?v=Z_FiER8aAqM&list=PLLYz8uHU480j37APNXBdPz7YzAi4XlQUF&index=45&ab_channel=C%2B%2BbySaurabhShuklaSir
*/
virtual void print() {
cout << "In print function of class C\n";
}
};
class D: public C {
public:
void print() {
cout << "In print function of class D\n";
}
};
int main() {
/*
Base Class Pointer:
--> Base class pointer can point to the objects of any of it's descendant class(es)
--> The converse is not true (Child pointer can't point to parent's objects)
IMP: Binding of function call with function definition happens at Compile Time only
*/
cout << "Before using virtual function..\n";
A *p1, a;
B b;
p1 = &b;
a.print();
b.print();
/*
The main issue with overriding occurs over here.
--> You feel that since p1 contains address of an object of class B, it should bind with the print()
method of class B
--> However, this does not happen in reality. Binding happens at compile time.
--> The function call at this moment does not know the address it will contain. Cause addresses are
assigned at runtime
--> So it checks for the datatype of p1 which is of type A and binds it there
--> Thus, print() function of class A is called
This problem in hand paves way for the need of virtual function
*/
p1->print();
// Using virtual functions
cout << "\nAfter using virtual function..\n";
C *p2, c;
D d;
p2 = &d;
c.print();
d.print();
p2->print();
}