top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is function overloading and operator overloading?

+3 votes
243 views

Want your help to understand function overloading and operator overloading. What is the difference between these two?

posted Nov 14, 2013 by anonymous

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

2 Answers

+1 vote

In Function overloading, n number of functions can be created for same class. But the signatures of each function should vary. For example

public class Employee 
{
public void Employee() 
{ }
public void Employee(String Name) 
{ }
}

We had seen function overloading in the previous example. For operator Overloading , we will have look at the example below. We define a class rectangle with two operator overloading methods.

class Rectangle
{
private int Height;
private int Width;
public Rectangle(int w,int h)
{
Width=w;
Height=h;
} }
Public static bool operator >(Rectangle a, Rectangle b)
{
return a.Height > b.Height ;
}
public static bool operator <(Rectangle a,Rectangle b)
{
return a.Height < b.Height ;
} }

Let us call the operator overloaded functions from the method below. When first if condition is triggered, first overloaded function in the rectangle class will be triggered. When second if condition is triggered, second overloaded function in the rectangle class will be triggered.

public static void Main()
{
Rectangle obj1 =new Rectangle();
Rectangle obj2 =new Rectangle();
if(obj1 > obj2){
Console.WriteLine("Rectangle1 is greater than Rectangle2");
} 
if(obj1 < obj2)
{
Console.WriteLine("Rectangle1 is less than Rectangle2");
}}
answer Nov 19, 2014 by Manikandan J
0 votes

Function overloading is multiple definition with different signatures(the parameters should be different) for the same function. The parameter list have to be different in each definition. The compiler will not accept if the return type alone is changed.

Operator overloading is defining a function for a particular operator. The operator loading function can not be overloaded through function overloading.

answer Nov 14, 2013 by Salil Agrawal
Similar Questions
+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.

+1 vote

Please explain with basic example.Am new to programming

...