Calling a Superclass Virtual Function - C++ Class

C++ examples for Class:Virtual Function

Description

Calling a Superclass Virtual Function

Demo Code

#include <iostream>

using namespace std;

class Base {/*from   w  ww.  j av  a 2s .co  m*/
public:
   virtual void foo() {cout << "Base::foo()" << endl;}
};

class Derived : public Base {
public:
   virtual void foo() {cout << "Derived::foo()" << endl;}
};

int main() {

   Derived* p = new Derived();

   p->foo();       // Calls the derived version
   p->Base::foo(); // Calls the base version
}

Related Tutorials