Simple console application using operator overloading
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplicationSample
{
class ComplexNo
{
double real; //real Part;
double imag; //Imaginary Part
public ComplexNo()
{ }
public ComplexNo(double x, double y)
{
real = x;
imag = y;
}
//
Specify which operator to overload (+),
// the
types that can be added (two Complex objects),
// and
the return type (Complex).
public static ComplexNo operator +(ComplexNo c1, ComplexNo c2)
{
ComplexNo c3 = new ComplexNo();
c3.real = c1.real + c2.real;
c3.imag = c1.imag + c2.imag;
return c3;
}
public void display()
{
Console.WriteLine("{0} +
{1}i", real, imag);
Console.ReadKey();
}
}
class TestComplex
{
static void Main()
{
ComplexNo num1 = new ComplexNo(2, 3);
ComplexNo num2 = new ComplexNo(3, 4);
// Add
two Complex objects by using the overloaded + operator.
ComplexNo sum = num1 + num2;
// Print
the numbers and the sum by using the overridden
Console.Write("First
complex number: ");
num1.display();
Console.Write("Second
complex number: ");
num2.display();
Console.Write("The sum of
the two numbers: ");
sum.display();
// Keep
the console window open in debug mode.
Console.WriteLine("Press any
key to exit.");
Console.ReadKey();
}
}
}
https://www.youtube.com/channel/UCKLRUr6U5OFeu7FLOpQ-FSw/videos
0 comments
Post a Comment