Virtual keyword use in C++

c++, polymorphism, virtual-functions

Solution

Nothing. Just to help remind you what functions are virtual or not.

Problem

I understand that C++ implements runtime polymorphism thorugh virtual functions and that virtual keyword is inherited but I don't see use of virtual keyword in derived class. e.g. In below case even if you dropped virtual keyword in derived class still ptr->method() call goes to derived::method. So what extra this virtual keyword is doing in derived class? ``` #include<iostream> using namespace std; class base { public: virtual void method() { std::cout << std::endl << "BASE" << std::endl; } }; class derived: public base { public: virtual void method() { std::cout << std::endl << "DERIVED" << std::endl; } }; int main() { base* ptr = new derived(); ptr->method(); return 9; } ```

Original source