-
Notifications
You must be signed in to change notification settings - Fork 0
/
Runtime-Polymorphism.cpp
165 lines (128 loc) · 3.38 KB
/
Runtime-Polymorphism.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// 📂 Run time polymorphism
/*
What is run time polymorphism?
It is a late binding and dynamic polymorphism.
late binding or dynamic polymorphism is a binding mechanism that occurs during
runtime. This means that the method that is called is determined when the
program is running, not when it is compiled.
Upcasting Concept:
Parent *p = new Child();
Upcasting is using the Super/Parent class's reference or pointer to refer to a
Sub/Child class's object.
Downcasting Concept:
Child *c = new Parent();
Downcasting is using the Sub/Child class's reference or pointer to refer to a
Super/Parent class's object.
Function Overriding:
The process of defining a function with the same name and parameters as a
function in a base class, in a derived class.
In short, A feature that allows us to use a function in the child class that is
already present in its parent class.
What is virtual keyword:
1. Yeh ek late bindind karne ka tarika hai
2. Yeh ek run time par decision lene tarika hai
3. Yeh ek tarika hai function ko virtual bnane par usko compile time par
seriously na le ka
Note:
1. Virtual function can always be written in parent class.
2. Without virtual early binding, static binding
3. With virtual late binding, dynamic binding
*/
// 📂 No Function Overriding Example
#include <iostream>
using namespace std;
class Boy {
public:
void behave() { cout << "Behaving like a Boy!" << endl; }
};
class Bus : public Boy {
public:
void behave() { cout << "In bus Behaving like passenger!" << endl; }
};
class School : public Boy {
public:
void behave() { cout << "In School Behaving like student!" << endl; }
};
void behave(Boy *boy) { boy->behave(); }
int main() {
// Upcasting
Boy *boy = new Bus();
behave(boy);
boy = new School();
behave(boy);
delete boy;
return 0;
}
/*
OUTPUT:
Behaving like a Boy!
Behaving like a Boy!
*/
// 📂 Function Overriding Example
#include <iostream>
using namespace std;
class Boy {
public:
virtual void behave() { cout << "Behaving like a Boy!" << endl; }
};
class Bus : public Boy {
public:
void behave() override { cout << "In bus Behaving like passenger!" << endl; }
};
class School : public Boy {
public:
void behave() override { cout << "In School Behaving like student!" << endl; }
};
void behave(Boy *boy) { boy->behave(); }
int main() {
// Upcasting
Boy *boy = new Bus();
behave(boy);
boy = new School();
behave(boy);
delete boy;
return 0;
}
/*
OUTPUT:
In bus Behaving like passenger!
In School Behaving like student!
*/
// 📂 Function Overriding Example with no memory leakage
#include <iostream>
using namespace std;
class Boy {
public:
virtual void behave() { cout << "Behaving like a Boy!" << endl; }
virtual ~Boy() { cout << "Boy DTOR called" << endl; }
};
class Bus : public Boy {
public:
void behave() override { cout << "In bus Behaving like passenger!" << endl; }
~Bus() { cout << "Bus DTOR called" << endl; }
};
class School : public Boy {
public:
void behave() override { cout << "In School Behaving like student!" << endl; }
~School() { cout << "School DTOR called" << endl; }
};
void behave(Boy *boy) { boy->behave(); }
int main() {
// Upcasting
Boy *boy = new Bus();
behave(boy);
delete boy;
boy = new School();
behave(boy);
delete boy;
return 0;
}
/*
OUTPUT:
In bus Behaving like passenger!
Bus DTOR called
Boy DTOR called
In School Behaving like student!
School DTOR called
Boy DTOR called
*/