Wednesday 31 August 2022

What is the Difference between function overloading and function overriding?

 

Function Overloading

 

It is a technique in object-oriented programming when two or more functions have the same name, but different parameters.

 

When a function name is overloaded with multiple assignments, then it is said to be function overloading.

 



 

Example:-

#include <iostream>

using namespace std;

void sum(int x, int y)

{

                cout << "Addition is…" << (x + y);

}

 

void sum(double x, double y)

{

                cout << endl << " Addition is… " << (x + y);

}

 

int main()

{

                sum(10, 20);

                sum(10.25, 20.35);

 

                return 0;

}

 

Output:-

Addition is = 30

Addition is = 30.6

 

Function Overriding

It is a technique when the base class and derived class have same member functions with same return-type, and same arguments list, then it is said to be function overriding.

 

#include <iostream>

using namespace std;

 

class BaseClass

{

public : void show()

{

              cout << "Base Function called" << endl;

}

};

 

class DerivedClass : public BaseClass

{

public : void show()

{

              cout << "Derived Function called" << endl;

}

};

 

int main()

{

              DerivedClass dc;

              dc.show();

              return 0;

}

 

Output:-

Derived Function called

 

Difference between function overloading and function overriding

 

Function Overloading

Function Overriding

Function Overloading is  a compile-time polymorphism.

Function overriding is a run-time polymorphism.

It helps to increase the readability of the program.

It is generally used to grant the specific implementation of the method which is already provided by its parent class or superclass.

It occurs generally within the class.

It is performed in two classes with inheritance relationships.

Function overloading may or may not require inheritance.

Function overriding always needs inheritance.

In function overloading, function must have the same name and different signatures.

In function overriding, function must have the same name and same signature.

In function overloading, the return type can or cannot be the same, but we just have to change the parameter.

In function overriding, the return type must be the same or co-variant.

The function must have different number of parameters. If both the function have the same number of parameters, then their type must be different.

Both the function must have the same number of parameters with the same type.

 

TCCI provides best teaching in various programming courses through different learning method/media as per students convince.

 

For More Information:                                          

Call us @ 9825618292

Visit us @ http://tccicomputercoaching.com  

 

No comments:

Post a Comment