Virtual functions in derived classes in C++

C++Server Side ProgrammingProgramming

Virtual functions in C++ use to create a list of base class pointers and call methods of any of the derived classes without even knowing the kind of derived class object. Virtual functions are resolved late, at the runtime.

If A virtual function in a base class declared as once a member function, it becomes virtual in every class derived from that base class. So, use of the keyword virtual is not necessary in the derived class while declaring redefined versions of the virtual base class function.

Example Code

using namespace std;
class B {
      virtual void s() {
         cout<<" In Base \n";
class D: public B {
      void s() {
         cout<<"In Derived \n";
int main(void) {
   D d; // An object of class D
   B *b= &d; // A pointer of type B* pointing to d
   b->s(); // prints"D::s() called"
   return 0;


In Derived
Published on 04-Apr-2019 10:31:05