Hi.....
I know the concept of overloading, but I want to know how many way overloading possible ? Please explain with example ?
Thanks......
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Datta KharadPosted Dec 10, 2011, 2:23 AM
Way of Overloading:-
1) Different types of parameters(arguments).
2) Based on different Data types.
3) Different Order of parameters(arguments).
Example:-
1) Example of Different types of parameters(arguments).
obj.Sum(10,20);
obj.SUm(15,25,35); //three numbers are integer type but they are different.
2) Example of Based on different Data types.
obj.Sum(10,20.50); //10 is int and 20.50 is float
3) Example of Different Order of parameters(arguments).
obj.Sum(10,20.50);
obj.Sum(20.50,10); //Change order of numbers.
Vineet Kumar SainiPosted Dec 12, 2011, 12:35 PM
SenthilkumarPosted Dec 10, 2011, 8:31 AM
The poster vineet mentioned he knows the concept of overloading.
Let me explain about the ways of overloading...
1) Method overloading
2)Operator overloading
3)Constructor overloading
Thanks,
Senthil.
VulpesPosted Dec 10, 2011, 8:08 AM
Datta KharadPosted Dec 10, 2011, 6:38 AM
Vineet Kumar SainiPosted Dec 10, 2011, 5:54 AM
Vineet Kumar SainiPosted Dec 10, 2011, 2:11 AM
I know overloading concept but I want to know how many way possible for overloading ? Please explain ......
Thanks...
Satyapriya NayakPosted Dec 10, 2011, 2:08 AM
Overloading - is the concept of using one function or class in different ways by changing the signature of its parameters. The signature changes in number of parameters,type of parameters and order of parameters.
In order to achieve overloading, there may be several techniques applied. There are different types of overloading like Operator Overloading, Function Overloading etc.
Function overloading may be achieved by changing the order of parameters in a function, by changing the types passed in the function, and also by changing the number of parameters passed in a function.
C# Example
//Define a method below
public void fnProcess(int x, double y)
{
......
}
//change the order of parameters
public void fnProcess(double x, int y) {
//.......Some code in C#
}
Please refer the below link
http://www.codeproject.com/KB/cs/OperatorOverLd.aspx
http://www.interviewcorner.com/answer/1753/
Thanks
Datta KharadPosted Dec 10, 2011, 1:58 AM
Method Overloading:-
Method overloading allows the programmer to define many methods with the same name but with a different set of parameters. Each combination of parameter types is known as a signature of the method. When a call is made to one of these overloaded methods, the compiler automatically determines which of the methods should be used according to the arguments used in the call and the available method signatures.
Way of Overloading:-
1) Different types of parameters(arguments).
2) Based on different Data types.
3) Different Order of parameters(arguments).
One of the greatest advantages of method overloading is the improvement that it provides to code readability and maintainability. In languages that do not support this technique, or that of optional operands, a new method must be created for every possible combination of parameters. For example, in the ANSI C programming language to truncate a value you would use trunc, truncf or truncl according to the data type being rounded. In C#, method overloading allows you to always call Math.Truncate. This becomes even more useful when a change in the requirements of the program means that data types change. Unlike with the older languages, the C# truncate method would require no code modification.
When using method overloading, each version of a method should perform the same general function using different data types or numbers of parameters. Although it is possible to create two methods with the same name that perform completely different tasks, this just reduces the quality of your code.
Creating an Overloaded Method:-
Creating an overloaded method is achieved by simply adding two or more methods of the same name to a class. The methods can be normal or static. As long as the method signatures differ, the code will compile correctly. In the following example, we will create a method of adding number which has twos methods.
using System;
namespace ProgramCall
{
class Class1
{
public int Sum(int A, int B)
{
return A + B;
}
public float Sum(int A, float B)
{
return A + B;
}
}
class Class2 : Class1
{
public int Sum(int A, int B, int C)
{
return A + B + C;
}
}
class MainClass
{
static void Main()
{
Class2 obj = new Class2();
//Here we called overloaded method with same name but different paramerers.
Console.WriteLine(obj.Sum(10, 20));
Console.WriteLine(obj.Sum(10, 15.70f));
Console.WriteLine(obj.Sum(10, 20, 30));
Console.Read();
}
}
}
Note:-
compiler identifies which overloaded method to execute based on number of arguments and their data types during compilation it self. Hence method overloading is an example for compile time polymorphism.