27. binary_operator_overloading

 using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;


namespace binary_operator_overloading.cs

{

    class math

    {

        public int a, b;

        public math()

        {

            a = b = 0;

        }

        public math(int a, int b)

        {

            this.a = a;

            this.b = b;

        }

        public static math operator +(math m1, math m2)

        {

            math m3 = new math();

            m3.a = m1.a + m2.a;

            m3.b = m1.b + m2.b;

            return m3;

        }

        public static math operator *(math m1, math m2)

        {

            math m3 = new math();

            m3.a = m1.a * m2.a;

            m3.b = m1.b * m2.b;

            return m3;

        }

        public static bool operator ==(math m1, math m2)

        {

            return((m1.a == m2.a) && (m1.b == m2.b));

        }

        public static bool operator !=(math m1, math m2)

        {

            return ((m1.a != m2.a) && (m1.b == m2.b) );

        }

        public static bool operator >(math m1, math m2)

        {

            return (((m1.a > m2.a) && (m1.b > m2.b)));

        }

        public static bool operator <(math m1, math m2)

        {

            return (((m1.a < m2.a) && (m1.b < m2.b)));

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            bool ans;

            math m1 = new math(20, 30);

            math m2 = new math(10, 10);

            math m3 = new math();


            m3 = m1 + m2;

            Console.Out.WriteLine("Addition Operator \n\nA = " + m3.a + "\nB = " + m3.b);


            m3 = m1 * m2;

            Console.Out.WriteLine("Multiplication Operator \n\nA = " + m3.a + "\nB = " + m3.b);


            ans = m2 == m3;

            Console.Out.WriteLine(" == Operator result : " + ans);


            ans = m2 != m3;

            Console.Out.WriteLine(" != Operator result : " + ans);


            ans = m2 < m3;

            Console.Out.WriteLine(" < Operator result : " + ans);


            ans = m2 > m3;

            Console.Out.WriteLine(" > Operator result : " + ans);


            Console.Read();

        }

    }

}

OUTPUT:
Addition Operator

A = 30
B = 40
Multiplication Operator

A = 200
B = 300
 == Operator result : False
 != Operator result : False
 < Operator result : True
 > Operator result : False

 

Comments