Sunday, February 24, 2013

Difference between method overriding and overloading in C++


Overloading a function in C++ is the ability for functions of the same name to be defined as long as these methods have different signatures (different set of parameters). Method overriding is the ability of the inherited class rewriting the virtual method of the base class.
a) In overloading, there is a relationship between methods available in the same class whereas in overriding, there is relationship between a superclass method and subclass method.
(b) Overloading does not block inheritance from the superclass whereas overriding blocks inheritance from the superclass.
(c) In overloading, separate methods share the same name whereas in overriding, subclass method replaces the superclass.
(d) Overloading must have different method signatures whereas overriding must have same signature.
Eg;
struct base {
   virtual void foo(); 
   void foo(int);      // overloads void foo()
};
struct derived : base {
   void foo();         // overrides void base::foo()
   void foo(int);      // overloads void derived::foo()
                       // unrelated to void::base(int)
};
int main() {
   derived d;
   base & b = d;
   b.foo( 5 );   // calls void base::foo(int)
   b.foo();      // calls the final overrider for void base::foo()
                 // in this particular case void derived::foo()
   d.foo( 5 );   // calls void derived::foo(int)
}

4 comments:

  1. What's Going down i'm new to this, I stumbled upon this I have found It absolutely
    helpful and it has aided me out loads. I'm hoping to give a contribution & help other customers like its aided me. Great job.

    my web site local business internet marketing

    ReplyDelete
  2. Hello, I want to subscribe for this web site to take most
    up-to-date updates, so where can i do it please help.


    Feel free to surf to my web blog; http://muscle-worship.tumblr.com/

    ReplyDelete
  3. I understand this since we are in OC|an OC real estate.

    My blog post ... smart realtor in Orange County reveals tricks

    ReplyDelete
  4. visit our website
    www.kavikatta.com

    ReplyDelete