Method Overloading :
Def : Method overloading is nothing but writing multiple methods with same name and with different arguments.
- To overload methods, you should follow the below rules.
- All of the overloaded methods name should be same.
- Any difference between the method arguments should be maintained. The difference may be in no. of arguments or the data types of arguments.
- Finally, when you call the overloaded method, automatically compiler takes the decision that "which method is to be called", based on the arguments that you are passing.
C# Coding :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MethodOverloadingDemo
{
class OverloadTest
{
public double GetSimpleInterest(double p, double n, double r)
{
double si;
si = (p * n * r) / 100;
return (si);
}
public double GetSimpleInterest(double p, double n)
{
double si;
si = (p * n * 8.45) / 100;
return (si);
}
}
class Program
{
static void Main(string[] args)
{
OverloadTest ot = new OverloadTest();
double result = ot.GetSimpleInterest(9000, 5, 3.5);
Console.WriteLine("Simple Interest is: " + result);
result = ot.GetSimpleInterest(10000, 8);
Console.WriteLine("Simple Interest is: " + result);
Console.Read();
}
}
}
Operator Overloading :
Def : Re-defining the functionality of an operator is nothing but "Operator Overloading". With the support of operator overloading, you can use any of the existing operators with the combination of objects.
For example to add two numbers, you write like this (a and b are the variables).
a + b
But suppose you have two objects declared for "Student" class. Then you can‘t write like this:
s1 + s2
Finally, if you want to use the operator with the combination of objects, you require "Operator Overloading". To implement this feature, write the "Operator method" as follows:
public static operator symbol(arguments)
{
}
If you use the specific operator with the objects, then automatically the above method will be called and the object(s) will be passed as arguments to the above operator method. Generally operator method returns a result object.
C# Coding :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OperatorOverloadingDemo
{
class PersonHeight
{
//data members
private int Feet, Inches;
//methods
public void SetHeight(int Feet, int Inches)
{
this.Feet = Feet;
this.Inches = Inches;
}
public void DisplayHeight()
{
Console.WriteLine(this.Feet + " feet and " + this.Inches + " inc.");
}
//operator method
public static PersonHeight operator - (PersonHeight p1, PersonHeight p2)
{
PersonHeight ph = new PersonHeight();
if (p1.Inches < p2.Inches)
{
p1.Feet--;
p1.Inches += 12;
}
ph.Inches = p1.Inches - p2.Inches;
ph.Feet = p1.Feet - p2.Feet;
return (ph);
}
}
class Program
{
static void Main(string[] args)
{
//create two objects for two persons
PersonHeight p1 = new PersonHeight();
PersonHeight p2 = new PersonHeight();
p1.SetHeight(8, 3);
p2.SetHeight(6, 7);
//display the height of two persons
p1.DisplayHeight();
p2.DisplayHeight();
//create one more object for result
PersonHeight p3 = new PersonHeight();
//use '-' operator
p3 = p1 - p2;
Console.WriteLine("\nThe difference is:");
p3.DisplayHeight();
Console.Read();
}
}
}
0 Comments