-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVirtualFunction.cpp
199 lines (159 loc) · 4.52 KB
/
VirtualFunction.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//At last you will find virtual function example.
//Before procedding to virtual function let us see the below example of Function Overriding.
//Function Overriding example
#include<iostream>
using namespace std;
class Fun
{
public:
void play()
{
cout<<"Playing in Fun1"<<endl;
}
};
class Fun_2:public Fun
{
public:
void play()
{
cout<<"Playing in Fun_2"<<"\n";
}
};
class Fun_3:public Fun
{
public:
void play()
{
cout<<"Playing in Fun_3"<<endl;
}
};
int main()
{
Fun f1;
Fun_2 f2;
Fun_3 f3;
f1.play();
f2.play();
f3.play();
}
/*
In the above example in main function you have created different object for different classes.
Alternate way is to create a base class pointer and pointing to derived class objects.
But, when base class pointer contains the address of the derived class object, always executes the base class function.
Below is the example
*/
#include<iostream>
using namespace std;
class Fun
{
public:
void play()
{
cout<<"Playing in Fun1"<<endl;
}
void run()
{
cout<<"Running in Fun1"<<endl;
}
};
class Fun_2:public Fun
{
public:
void play()
{
cout<<"Playing in Fun_2"<<"\n";
}
};
class Fun_3:public Fun
{
public:
void play()
{
cout<<"Playing in Fun_3"<<endl;
}
};
int main()
{
Fun *ptr;
Fun f1;
Fun_2 f2;
Fun_3 f3;
f1.play();
ptr = &f1;
ptr->play();
ptr = &f2;
ptr->run();
ptr = &f2;
ptr->play();
ptr = &f3;
ptr->play();
}
/*In the above example it only execute the base class function.
now we are going to implement the virtual function in base class to achieve run time polymorphism.
Virtual function tells the compiler that this function should be overridden by any derived class that wishes to provide its own signature.
So appropriate function to be called is determined by the type of the object being referred to, rather than the type of the pointer or reference used to access it.
*/
/*
Before implementing virtual function I've a question?
-----------------------------------
In C++, when you have a base class with multiple derived classes, and each derived class has a function with the same name as the one in the base class,
you can create objects of different derived classes(as we have already seen in above example) and call the function with each object.
then what is the need of Virtual Function?
--------------------------------
----Answer----
-----
Static Binding: Without virtual functions, the function called is determined at compile-time based on the type of the pointer or reference.
This means that if you have a base class pointer pointing to a derived class object, calling a function will always invoke the base class's implementation, regardless of the actual type of the object.
Polymorphism: Virtual functions enable polymorphic behavior, where the appropriate function to be called is determined at runtime based on the actual type of the object. This allows for more dynamic and flexible behavior, as different derived classes can provide their own implementations of the same function.
Code Reusability and Flexibility: Virtual functions facilitate code reuse and promote a more modular design. They allow base class pointers to be used to work with objects of derived classes, providing a common interface for interacting with objects of different types.
Dynamic Dispatch: Virtual functions enable dynamic dispatch, meaning that the function to be called is determined at runtime based on the type of the object being referred to. This enables more dynamic and runtime-specific behavior, which is especially useful in scenarios where the actual type of objects may vary during program execution.
-----
*/
//Virtual Function example
#include<iostream>
using namespace std;
class Fun
{
public:
virtual void play()
//void play()
{
cout<<"Playing in Fun1"<<endl;
}
void run()
{
cout<<"Running in Fun1"<<endl;
}
};
class Fun_2:public Fun
{
public:
void play()
{
cout<<"Playing in Fun_2"<<"\n";
}
};
class Fun_3:public Fun
{
public:
void play()
{
cout<<"Playing in Fun_3"<<endl;
}
};
int main()
{
Fun *ptr;
Fun f1;
Fun_2 f2;
Fun_3 f3;
// f1.play();
// ptr = &f1;
// ptr->play();
ptr = &f2;
ptr->run();
ptr = &f2;
ptr->play();
ptr = &f3;
ptr->play();
}