Binary Operator Overloading
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Text;
namespace
BinaryOperatorOverloading
{
class binop
{
private int n1,n2;
public void getdata()
{
n1=Int32.Parse(Console.ReadLine());
n2=Int32.Parse(Console.ReadLine());
}
public void display()
{
Console.WriteLine("n1={0}\nn2={1}\n",n1,n2);
}
public binop()
{}
public static binop operator +(binop
op1, binop op2)
{
binop obj=new binop();
obj.n1=op1.n1+op2.n1;
obj.n2=op1.n2+op2.n2;
return obj;
}
}
class Program
{
static void Main(string[] args)
{
binop op1 = new binop();
binop op2 = new binop();
binop op = new binop();
Console.WriteLine("Enter
values for the 1st object");
op1.getdata();
Console.WriteLine("Enter
values for the second object");
op2.getdata();
op = op1 + op2;
Console.WriteLine("Before Overloading");
Console.WriteLine("******************");
Console.WriteLine("Value of
1st and 2nd object");
op1.display();
op2.display();
Console.WriteLine("\nAfter
Overloading");
Console.WriteLine("******************");
Console.WriteLine("Value of
resultant object");
op.display();
Console.ReadLine();
}
}
}
Output
Enter values for the 1st object
23
34
Enter values for the second object
12
53
Before Overloading
******************
Value of 1st and 2nd object
n1=23
n2=34
n1=12
n2=53
After Overloading
******************
Value of resultant object
n1=35
n2=87
No comments:
Post a Comment