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 demonstrates the concept of virtual functions and polymorphism in C++ programming.
//Virtual Function #include<iostream> using namespace std; class vaccine { public: virtual void putVaccine() { cout<<"Covid Vaccine"<<endl; } }; class covaxin:public vaccine { public: void putVaccine() { cout<<"Put Covaxin Vaccine"<<endl; } }; class covishield:public vaccine { public: void putVaccine() { cout<<"Put Covishield Vaccine"<<endl; } }; int main() { covaxin cx; covishield cs; vaccine *o=NULL; o=&cx; o->putVaccine(); o=&cs; o->putVaccine(); return 0; }
Put Covaxin Vaccine Put Covishield VaccineTo download raw file Click Here
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions