-1

I am studying c plus plus course in my college and i am unable to differentiate between overriding and overloading of a function can anyone please help me.

Abdul Rafay
  • 102
  • 2
  • 10
  • 1
    This is not the same at all if I understand what you ask. Overriding : redefining the function in children classes (polymorphism, inheritance). Overloading : expose in a single class many function with the same name but different signatures (parameters list). Hope it will help you. – AFract Mar 16 '15 at 16:14
  • 1
    See related: http://stackoverflow.com/questions/11912022/differentiate-between-function-overloading-and-function-overriding?rq=1 – EdChum Mar 16 '15 at 16:15
  • one point that i didnt see in the answers yet: Overriding lets you define different methods with the same name. They can have different signatures and when calling, the right version of the method is choosen. On the other hand, when overriding, the overriding method must have the same signature. – 463035818_is_not_an_ai Mar 16 '15 at 16:22

4 Answers4

3

Here are two different overloads of foo:

void foo(int);
void foo(char);

Here B::bar is a function override:

class A {
public:
    virtual void bar();
};

class B : public A {
public:
    void bar() override;
};
Emil Laine
  • 41,598
  • 9
  • 101
  • 157
1

Overriding means, giving a different definition of an existing function with same parameters, and overloading means adding a different definition of an existing function with different parameters.

Example:

#include <iostream>
class base{

    public:
    virtual void show(){std::cout<<"I am base";} //this needs to be virtual to be overridden in derived class
    void show(int x){std::cout<<"\nI am overloaded";} //this is overloaded function of the previous one

    };

class derived:public base{

    public:
    void show(){std::cout<<"I am derived";}  //the base version of this function is being overridden

    };


int main(){
    base* b;
    derived d;
    b=&d;
    b->show();  //this will call the derived version
    b->show(6); // this will call the base overloaded function
    }

Output:

I am derived
I am overloaded
Jahid
  • 21,542
  • 10
  • 90
  • 108
1

overloading means functions with same name having different parameters , it does not really depend whether you are using procedural language or object oriented language you can do overloading. well as far as over riding is concerned it means we are explicitly defining a function that exist in base class in derived class . obviously you need object oriented language to perform over riding as it is done between base and derived classes.

Maaz Rehman
  • 674
  • 1
  • 7
  • 20
1

Overloading means declaring more than one function with the same name in the same scope. They must have different parameter types, and the suitable overload is chosen at compile time based on the arguments' static types.

void f(int);
void f(double);

f(42);   // selects the "int" overload
f(42.0); // selects the "double" overload

Overriding means that a derived class declares a function that matches a virtual function declared in the base class. Calling the function via a pointer or reference to the base class will select the override at run-time, based on the object's dynamic type.

struct Base {
    virtual void f();
};
struct Derived : Base {
    void f() override;  // overrides Base::f
};

Base * b = new Base;     // dynamic type is Base
Base * d = new Derived;  // dynamic type is Derived

b->f();   // selects Base::f
d->f();   // selects Derived::f
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644