C++ Program using virtual function


In this program, You will learn how to implement virtual function in C++.


virtual void msg(){ }


Example: How to implement virtual function in C++

Run it Now
#include<iostream>
using namespace std;

class First {
public:

   virtual void msg() {
       cout << "Welcome";
   }
};

class Second : public First {
public:

   void msg() {
       cout << "Hello";
   }
};

int main() {

   First *f;
   Second s;
   f = &s;
   f->msg();

   return 0;
}

Output:

Hello