Static Polymorphism:
Function Overloading:
Using the same name for two or more functions is
called function overloading. Each redefinition of a function must use different
types of parameters, sequence of parameters, or a number of parameters. The
type , seqeuence , or number of parameters for
a function is called function signature. Same is briefly explained as
follows.
The number of parameters.
- void add(int a);
- void add( int a, float b);
The type of parameters.
- void display(char a);
- void add(int b);
The sequence of parameters
- void add(int a, char b);
- void add( char a, int b);
The most important thing ‘return type’ is not part of function signature. Therefore
the followign two declarations cannot occur in the same class.
- void add();
- char add();
Program 15:
using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace Csharp
{
class polymorphism
{
public static void Main(string[] args)
{
polymorphism
obj = new polymorphism();
obj.add(2.3F, 3.2F);
obj.add(2, 3);
obj.add(2.6m, 3.2m);
obj.disp('b',
5);
obj.disp(5, 'b');
obj.display('b');
obj.display(5, 'b');
Console.ReadLine();
}
///
/// (a)Type of parameters
///
public void add(int a, int b)
{
int
sum;
sum = a + b;
Console.WriteLine("{0}", sum);
}
public void add(float a, float b)
{
float
sum;
sum = a + b;
Console.WriteLine("{0}", sum);
}
public void add(decimal a, decimal b)
{
decimal
sum;
sum = a + b;
Console.WriteLine("{0}", sum);
}
/// (b)number of parameters
///
///
public void display(int a,char b)
{
Console.WriteLine("{0}{1}", a,b);
}
public void display(char a)
{
Console.WriteLine("{0}", a);
}
///
/// (c)seqeunce of parameters
///
public void disp(int a, char b)
{
Console.WriteLine("{0}{1}", a, b);
}
public void disp(char b, int a)
{
Console.WriteLine("{0}{1}", a, b);
}
}
}
No comments:
Post a Comment