GTU`s Complete solution for the decipline computer sci. & engg. , Computer Engg. and IT
Monday, November 8, 2010
ORATOR OVERLOADING IN C#
Write a program to add two complex number by overloading + operator.
using System;
public class Complex
{
public int real;
public int imaginary;
public Complex(int real, int imaginary)
{
this.real = real;
this.imaginary = imaginary;
}
public static Complex operator +(Complex c1, Complex c2)
{
return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
}
public void show()
{
Console.WriteLine("{0} + {1}i", this.real, this.imaginary);
}
}
class overloadplus
{
static void Main()
{
Complex num1 = new Complex(12, 8);
Complex num2 = new Complex(21, 4);
Complex sum = num1 + num2;
Console.WriteLine("First complex number:");
num1.show();
Console.WriteLine("Second complex number:") ;
num2.show();
Console.WriteLine("The sum of the two numbers:");
sum.show();
}
}
+++++++++++++++OUTPUT+++++++++++++++++++++
First complex number:
12 + 8i
Second complex number:
21 + 4i
The sum of the two numbers:
33 + 12i
************************************************
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment