Monday, June 23, 2008

C++ Overriding

1 comment
What is overriding in C++?
  • Redefining a base class function in the derived class to have our own implementation is referred as overriding.
  • Often confused with overloading which refers to using the same function name but with a different signature. However in overriding the function signature is the same.

EXAMPLE: Demonstrate the usage of overriding

#include <iostream>
using namespace std;

class Base {
public:
virtual void myfunc() {
cout << "Base::myfunc .." << endl;
}
};

class Derived : public Base {
public:
void myfunc() {
cout << "Derived::myfunc .." << endl;
}
};

void main()
{
Derived d;
d.myfunc();
}

OUTPUT:-
Derived::myfunc ..

If You Enjoyed This, Take 5 Seconds To Share It

1 comment:

  1. I think no need to declare function virtual here.

    ReplyDelete