From the course: Nail Your C++ Interview

Unlock the full course today

Join today to access over 24,000 courses taught by industry experts.

Virtual functions in C++

Virtual functions in C++ - C++ Tutorial

From the course: Nail Your C++ Interview

Virtual functions in C++

- [Instructor] Virtual functions allow for late binding in C++. The efficiency and power of C++ has mainly been due to its compile time binding. But if virtual functions allow us to define a function in a base class, then in the derived classes, create different implementations. For example, if we were to create a door class, we already have established that a door must open and close, but what if we wrote the open function that included doors swing or hinge movement. A sliding door would not be able to use that implementation of open and we would either need to create a new program, new class, or implement a custom function. If the parent class has an implementation for a function and the child class needs to use a different implementation, they can. This is called overriding. Virtual functions are also a great example of polymorphism. Building on our previous video game example, suppose we were asked to create a…

Contents