Virtual functions in C++
If you are not familiar with Inheritance, I recommend you read my short article about inheritance first.
One of the features of Object Oriented Programming is Polymorphism. Virtual functions in C++ allow developers to achieve run-time polymorphism by overwriting methods of a base class.
In my article about inheritance, I showed how we can create classes that inherit from a base (or parent) class. Something similar to the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
class Greeter {
public:
void talk() {
std::cout << "hello" << std::endl;
}
};
class SpanishGreeter : public Greeter {
public:
void talk() {
std::cout << "hola" << std::endl;
}
};