A virtual function is a member function which is declared within a base class and is re-defined(Overriden) by a derived class. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class’s version of the function.
The program defines a base class Base with a virtual method fun(), which prints the message "Function of Base Class".
#include<iostream> using namespace std; //Virtual Function in C++ Programming /* virtual Function is function is declared in base class and redefined in derived class. Ability for the runtime polymorphism */ class Base { public: virtual void fun() { cout<<"Function of Base Class"<<endl; } }; class Derived:public Base { public: void fun() { cout<<"Function of Derived Class"<<endl; } }; int main() { /*Derived o; o.fun();*/ Derived o; Base *p=&o; p->fun(); return 0; }
Function of Derived ClassTo download raw file Click Here
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions