Monday, 25 November 2013

C# Binary Operator Overloading Program-------||||||||||

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OperatorOverloading
{
    class Complex
    {
        double x;
        double y;
        public Complex()
        {
        }
        public Complex(double real, double imag)
        {
            x = real;
            y = imag;
        }
        public static Complex operator +(Complex c1, Complex c2)
        {
            Complex c3 = new Complex();
            c3.x = c1.x + c2.x;
            c3.y = c1.y + c2.y;
            return (c3);
        }
        public void Display()
        {
            Console.Write(x);
            Console.Write(" +j " +y);
            Console.WriteLine();
        }
    }
    class ComplexTest
    {
        static void Main(string[] args)
        {
            Complex a, b, c;
            a = new Complex(2.5,3.5);
            b = new Complex(1.6, 2.7);
            c = a + b;
            Console.Write("a=");
            a.Display();
            Console.Write("b=");
            b.Display();
            Console.Write("c=");
            c.Display();
            Console.ReadKey();


        }
    }
}

Output:

No comments:

Post a Comment