Function overloading, as opposed to operator overloading, is still not possible in C. C has no facility to type check data types and use different functions for each. This cannot be worked around in C other than by writing a function per case and making an appropriate name change. Then you need to write a specific type identification case statement and let that make a final call to the correct function. This will, inefficiently, imitate function overloading as it is available in C++.
2006-09-06 04:31:26
·
answer #1
·
answered by Anonymous
·
0⤊
0⤋
me yes here
using System;
class Complex
{
private int x;
private int y;
public Complex()
{
}
public Complex(int i, int j)
{
x = i;
y = j;
}
public void ShowXY()
{
Console.WriteLine(\"{0} {1}\",x,y);
}
public static Complex operator -(Complex c)
{
Complex temp = new Complex();
temp.x = -c.x;
temp.y = -c.y;
return temp;
}
}
class MyClient
{
public static void Main()
{
Complex c1 = new Complex(10,20);
c1.ShowXY(); // displays 10 & 20
Complex c2 = new Complex();
c2.ShowXY(); // displays 0 & 0
c2 = -c1;
c2.ShowXY(); // diapls -10 & -20
}
}
public static return_type operator op (Type1 t1, Type2 t2)
{
//Statements
}
A concrete example is given below
using System;
class Complex
{
private int x;
private int y;
public Complex()
{
}
public Complex(int i, int j)
{
x = i;
y = j;
}
public void ShowXY()
{
Console.WriteLine(\"{0} {1}\",x,y);
}
public static Complex operator +(Complex c1,Complex c2)
{
Complex temp = new Complex();
temp.x = c1.x+c2.x;
temp.y = c1.y+c2.y;
return temp;
}
}
class MyClient
{
public static void Main()
{
Complex c1 = new Complex(10,20);
c1.ShowXY(); // displays 10 & 20
Complex c2 = new Complex(20,30);
c2.ShowXY(); // displays 20 & 30
Complex c3 = new Complex();
c3 = c1 + c2;
c3.ShowXY(); // dislplays 30 & 50
}
}
Operators Overloadability
+, -, *, /, %, &, |, <<, >> All C# binary operators can be overloaded.
+, -, !, ~, ++, --, true, false All C# unary operators can be overloaded.
==, !=, <, >, <= , >= All relational operators can be overloaded,
but only as pairs.
&&, || They can't be overloaded
() (Conversion operator) They can't be overloaded
+=, -=, *=, /=, %= These compound assignment operators can be
overloaded. But in C#, these operators are
automatically overloaded when the respective
binary operator is overloaded.
=, . , ?:, ->, new, is, as, sizeof These operators can't be overloaded
2006-09-06 00:28:30
·
answer #3
·
answered by Joe_Young 6
·
0⤊
2⤋
It is not possible. But u can achieve behaviour of printf();
By using variable arguments. defined in vararg.h
prototype of printf as like: int printf(char *format,...);
Ellipses indicate fun() can receive any no.of any type of args!
ok naa!
2006-09-06 01:07:08
·
answer #4
·
answered by Sateesh 2
·
0⤊
1⤋