I am currently working through the Educative course Grokking the Coding Interview. While it is a very good course and does explain the algorithms very well, it does not always explain the code.
A few times I have seen the use of virtual
functions, as a dynamic function (to be clear, I mean functions that require the instantiation of an object in order to be called). From reading up on virtual
functions, I gather that they are used to achieve some OOP principles such as run time polymorphism, or just generally improving the maintainability of some code. In the case of these algorithms questions, that seems to be completely unnecessary. In fact, I am able to just delete the virtual
keyword and the code runs all the same.
My question is: Why might the author be using virtual
to define these functions?
Here is an example of the course's author's use of virtual functions:
using namespace std;
#include <iostream>
#include <queue>
#include <vector>
class MedianOfAStream {
public:
priority_queue<int> maxHeap; // containing first half of numbers
priority_queue<int, vector<int>, greater<int>> minHeap; // containing second half of numbers
virtual void insertNum(int num) {
if (maxHeap.empty() || maxHeap.top() >= num) {
maxHeap.push(num);
} else {
minHeap.push(num);
}
// either both the heaps will have equal number of elements or max-heap will have one
// more element than the min-heap
if (maxHeap.size() > minHeap.size() + 1) {
minHeap.push(maxHeap.top());
maxHeap.pop();
} else if (maxHeap.size() < minHeap.size()) {
maxHeap.push(minHeap.top());
minHeap.pop();
}
}
virtual double findMedian() {
if (maxHeap.size() == minHeap.size()) {
// we have even number of elements, take the average of middle two elements
return maxHeap.top() / 2.0 + minHeap.top() / 2.0;
}
// because max-heap will have one more element than the min-heap
return maxHeap.top();
}
};
int main(int argc, char *argv[]) {
MedianOfAStream medianOfAStream;
medianOfAStream.insertNum(3);
medianOfAStream.insertNum(1);
cout << "The median is: " << medianOfAStream.findMedian() << endl;
medianOfAStream.insertNum(5);
cout << "The median is: " << medianOfAStream.findMedian() << endl;
medianOfAStream.insertNum(4);
cout << "The median is: " << medianOfAStream.findMedian() << endl;
}
Again, from everything I've read, I really just don't see the point. And, running this exact code without the virtual
keyword works just fine. My thinking would be that this is some sort of best practice in C++ land.
Thanks for any explanation!