C++ Program using function overriding


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


class First { 
  public void msg() { 
    //statement
  }
};

class Second : public First { 
     public void msg() { 
         //statement
     }
};

Example: How to implement function overriding in C++

#include<iostream>
using namespace std;

class First {
public:

   void msg() {
       cout << "First msg called here";
   }
};

class Second : public First {
public:

   void msg() {
       cout << "Second msg called here";
   }
};

int main() {

   Second s = Second();
   s.msg();

   return 0;
}

Output:

Second msg called here