Operator overloading in C#.

Overloading is one of the best concept introduced in OOPs and it also supports Operator overloading. Sometime we don’t want actual functionality of operator in that case we need to overload that operator. For example in case of ‘+’ operator I want to multiple the result with 2 that is:

double opp1=5, opp2=10, result=0;

Actual result:

result = opp1 + opp2;  // 15

I want:

result = opp1 + opp2; // 30

class OperatorOverLoading
    {
        static void Main()
        {
            Operators opp1, opp2, result;
            Console.WriteLine(“Please enter two values: “);

            // user input
            opp1 = new Operators(Convert.ToDouble(Console.ReadLine()));
            opp2 = new Operators(Convert.ToDouble(Console.ReadLine()));

            // + operator overloading, (first sum two values and then double this value)
            result = opp1 + opp2;
            result.Show(“Result for operator ‘+’ :”);

            // + operator overloading, (first sum two values and then double this value)
            result = opp1 – opp2;
            result.Show(“Result for operator ‘-‘ :”);

            // + operator overloading, (first sum two values and then double this value)
            result = opp1 * opp2;
            result.Show(“Result for operator ‘*’ :”);

            // + operator overloading, (first sum two values and then double this value)
            result = opp1 / opp2;
            result.Show(“Result for operator ‘/’ :”);
        }
    }

    class Operators
    {
        // variable declaration
        double opp1, result;

        // default constructor
        public Operators()
        {
        }

        // parameterized constructor
        public Operators(double x)
        {
            opp1 = x;
        }

        // method that will display final result
        public void Show(string msg)
        {
            Console.WriteLine(msg + result);
            Console.ReadLine();
        }

        // method that overloads + operator’s actual functionality
        public static Operators operator +(Operators x, Operators y)
        {
            Operators op = new Operators();
            op.result = (x.opp1 + y.opp1) * 2;
            return op;
        }

        // method that overloads – operator’s actual functionality
        public static Operators operator -(Operators x, Operators y)
        {
            Operators op = new Operators();
            op.result = (x.opp1 – x.opp1) * 2;
            return op;
        }

        // method that overloads * operator’s functionality
        public static Operators operator *(Operators x, Operators y)
        {
            Operators op = new Operators();
            op.result = (x.opp1 * y.opp1) * 2;
            return op;
        }

        // method that overloads / operator’s functionality
        public static Operators operator /(Operators x, Operators y)
        {
            Operators op = new Operators();
            op.result = (x.opp1 / y.opp1) * 2;
            return op;
        }
    }

For more details: http://www.msdotnet.co.in/2013/07/operator-overloading-in-c.html?utm_source=feedburner&utm_medium=email&utm_campaign=Feed%3A+msdotnet+%28Microsoft+.NET%29

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s