| |
|
Function or Method Overload Ways with Example Question Posted on 03 Feb 2017 Home >> OOPS >> CLASS >> Function or Method Overload Ways with Example |
If any class which have multiple methods with same name but have different parameters then they are said overloaded. In Function Overloading we use the same name for different functions to do same or different operations on same class.
We used this where we have to perform any operation with some different number of arguments.
There are mainly two ways function overload or method overload:-
1.By changing number of Arguments.
2.By having different types of argument.
Now example of above two ways are given below
(1)By changing number of Arguments.
int sum (int x, int y)
{
///some operations
}
int sum(int x, int y, int z)
{
///some operations
}
2.By having different types of argument.
int sum(int x,int y)
{
///some operations
}
double sum(double x,double y)
{
///some operations
}
Now take a Full example how to define and call overloading methods
using System;
namespace oopsfuntionoverload
{
class funOverloadexam
{
public string namevalue;
//Now start overloaded functions
public void setFunctionName(string var1)
{
name = var1;
}
public void setFunctionName(string var2, string var1)
{
name = var2 + "" + var1;
}
public void setFunctionName(string var3, string var2, string var1)
{
name = var3 + "" + var2 + "" + var1;
}
//Entry point
static void Main(string[] args)
{
funOverloadexam obj = new funOverloadexam();
obj.setFunctionName("barack");
obj.setFunctionName("barack "," obama ");
obj.setFunctionName("barack ","hussian","obama");
}
}
} | |
|
|
|
|