top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Differentiate between overloading and overriding with a simple program in c++?

0 votes
405 views
Differentiate between overloading and overriding with a simple program in c++?
posted Oct 18, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Function overloading is the concept by which the same function name is used with the different set of parameters. In other words, different function signatures are used to overload a function. For example:
void sum(int x, int y) { cout<< "Result" << x +y; }
void sum(float x, float y) { cout << "Result" << x +y; }
Here both the function names are same but taking different types of arguments and giving the different results.

Function overriding is different concept used to provide same interface but different actions based on different types of objects. Function overloading is achieved in C++ using the same function name by prepending the keyword "virtual" with function prototype.
For example:
Define a virtual function within the base class as following virtual fun(int x, int y) { }. Inherit the base class in derived class and re-define this inherited virtual function in derived class. When this function is called using the base class object then base class version is called and when derived class object is used to call the same function, derived class version of the function is executed. This dynamic approach is achieved by using virtual pointer table internally.

answer Oct 19, 2016 by Harshita
Similar Questions
+1 vote

Please explain with basic example.Am new to programming

+1 vote

void fun1(int x, int y) { } and void fun1(int const x, int const y) { } can't be overloaded while
void fun1(int *x, int *y){ } and void fun1(int const *x , int const *y) { } can be overloaded. I compiled both the samples and found first set of functions can't be overloaded while the next two functions can be overloaded. I could not understand the reason behind it.

...