polymorphism - Virtual keyword use in C++ -
i understand c++ implements runtime polymorphism thorugh virtual functions , virtual keyword inherited don't see use of virtual keyword in derived class.
e.g. in below case if dropped virtual keyword in derived class still ptr->method() call goes derived::method. virtual keyword 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; }
nothing. remind functions virtual or not.
Comments
Post a Comment