Discuss Function overloading and function overriding in Object Oriented programming.
Function overloading means the use of the same function name to create another function that perform a variety of different tasks in a single program. The functions would perform different operations depending on the argument list in the function call. The correct function to be invoked is selected by seeing the number and type of the arguments but not the function type.
For example an overloaded function add() handles different types of data as shown below:
int add(int a, int b);
double add(double a, int b);
long add(long a, long b, int c);
Function overriding is used in class programming to describe virtual function redefinition by a derived class. Function overriding is useful when we want to have multiple derived classes implementing a base class functions. We can put the common code to all of the derived classes in the base class function, and then in each of the derived class functions, we can add the code specific to each one, and then just invoke the parent method. It differs from function overloading in the sense that all aspects of the parameters should be same when an overridden function is redefined in the derived class.
Thanks
Mukesh Rajput
Post A Comment:
0 comments: