find and solve || findandsolve.com
Please wait.....

Welcome back.






Before Read Terms of use

Operator Overloading in C#


C# - Operator Overloading


You can redefine or overload many of the built-in operators available in C #. So programmers can also use operators with user defined types. Overloaded operators work with keyword names with special names and then symbolize the operator as the defining symbol. Similar to any other function, an overload operator has a type of return and a parameter list.

For example,

public static DemoBox operator +(DemoBox b, DemoBox  c)
{
    DemoBox box = new DemoBox ();
    box.length = b.length + c.length;
    box.breadth = b.breadth + c.breadth;
    box.height = b.height + c.height;
    return box;
}

The above function implements an additional operator (+) for the user defined class box. This adds the attribute of the two box objects and returns the result box item.

Implementing the Operator Overloading



using System;

namespace OperatorOvlApplication
{
    class DemoBox
    {
        private double length;   // Length of a demobox
         private double breadth;  // Breadth of a demobox
         private double height;   // Height of a demobox

        public double getVolume()
        {
            return length * breadth * height;
        }
        public void setLength(double len)
        {
            length = len;
        }
      public void setBreadth(double bre)
        {
            breadth = bre;
        }
        public void setHeight(double hei)
        {
            height = hei;
        }

        // Add two Box objects,Overload + operator 
        public static DemoBox operator +(DemoBox b, DemoBox c)
        {
            DemoBox box = new DemoBox ();
            box.length = b.length + c.length;
            box.breadth = b.breadth + c.breadth;
            box.height = b.height + c.height;
            return box;
        }
    }
    class DemoTester
    {
        static void Main(string[] args)
        {
            DemoBox DemoBox1 = new DemoBox();   // Declare Box1 of type DemoBox
            DemoBoxDemoBox2 = new DemoBox();   // Declare Box2 of type DemoBox
            DemoBoxDemoBox3 = new DemoBox();   // Declare Box3 of type DemoBox
            double volume = 0.0;    // Store the volume of a DemoBox here

            // box 1 specification
            DemoBox1.setLength(6.0);
            DemoBox1.setBreadth(7.0);
            DemoBox1.setHeight(5.0);

            // box 2 specification
            DemoBox2.setLength(12.0);
            DemoBox2.setBreadth(13.0);
            DemoBox2.setHeight(10.0);

            // volume of DemoBox 1
            volume = DemoBox1.getVolume();
            Console.WriteLine("Volume of Box1 : {0}", volume);

            // volume of DemoBox 2
            volume = DemoBox2.getVolume();
            Console.WriteLine("Volume of Box2 : {0}", volume);

            // Add two object as follows:
            DemoBox3 DemoBox1DemoBox2 ;

            // volume of DemoBox 3
            volume = DemoBox3 .getVolume();
            Console.WriteLine("Volume of Box3 : {0}", volume);
            Console.ReadKey();
        }
    }
}

OUTPUT:
Volume of Box1 : 210
Volume of Box2 : 1560
Volume of Box3 : 5400

Overloadable and Non-Overloadable Operators




+, -, !, ~, ++, -- :

This is  unary operators and these take one operand and can be overloaded.


+, -, *, /, %

This is binary operators and these take one operand and can be overloaded.


==, !=, <, >, <=, >=

The comparison operators can be overloaded.


&&, ||

The conditional logical operators cannot be overloaded directly.

+=, -=, *=, /=, %=

The assignment operators cannot be overloaded.

=, ., ?:, ->, new, is, sizeof, typeof

These operators cannot be overloaded.

Example


using System;

namespace DemoOperatorOveload
{
    class DemoBox
    {
        private double length;    // Length of a DemoBox
        private double breadth;   // Breadth of a DemoBox
         private double height;    // Height of a DemoBox

        private double getVolume()
        {
            return length * breadth * height;
        }
        public void setLength(double len)
        {
            length = len;
        }
        public void setBreadth(double bre)
        {
            breadth = bre;
        }
        public void setHeight(double hei)
        {
            height = hei;
        }

        // Overload + operator to add two Box objects.
        public static DemoBox operator +(DemoBox b, DemoBox c)
        {
            DemoBox box = new DemoBox ();
            box.length = b.length + c.length;
            box.breadth = b.breadth + c.breadth;
            box.height = b.height + c.height;
            return box;
        }
        public static bool operator ==(DemoBox lhs, DemoBox rhs)
        {
            bool status = false;
            if (lhs.length == rhs.length && lhs.height == rhs.height
               && lhs.breadth == rhs.breadth)
            {

                status = true;
            }
            return status;
        }
        public static bool operator !=(DemoBox lhs, DemoBox rhs)
        {
            bool status = false;

            if (lhs.length != rhs.length || lhs.height != rhs.height ||
               lhs.breadth != rhs.breadth)
            {

                status = true;
            }
            return status;
        }
        public static bool operator <(DemoBox lhs, DemoBox oBox rhs)
        {
            bool status = false;

            if (lhs.length < rhs.length && lhs.height < rhs.height
               && lhs.breadth < rhs.breadth)
            {

                status = true;
            }
            return status;
        }
        public static bool operator >(DemoBox lhs, DemoBox rhs)
        {
            bool status = false;

            if (lhs.length > rhs.length && lhs.height >
               rhs.height && lhs.breadth > rhs.breadth)
            {

                status = true;
            }
            return status;
        }
        public static bool operator <=(DemoBox lhs, DemoBox rhs)
        {
            bool status = false;

            if (lhs.length <= rhs.length && lhs.height
               <= rhs.height && lhs.breadth <= rhs.breadth)
            {

                status = true;
            }
            return status;
        }
        public static bool operator >=(DemoBox  lhs, DemoBox rhs)
        {
             bool status = false;

            if (lhs.length >= rhs.length && lhs.height
               >= rhs.height && lhs.breadth >= rhs.breadth)
            {

                 status = true;
            }
              return status;
        }
        public override string ToString()
        {
            return String.Format("({0}, {1}, {2})", length, breadth, height);
        }
    }
    class Tester
    {
        static void Main(string[] args)
        {
            DemoBox  DemoBox1 = new DemoBox  ();   // Declare DemoBox1 of type Box
            DemoBox  DemoBox2 = new DemoBox  ();   // Declare DemoBox2 of type Box
            DemoBox  DemoBox3 = new DemoBox  ();   // Declare DemoBox3 of type Box
            DemoBox  DemoBox4 = new DemoBox  ();
            double volume = 0.0;    // Store the volume of a box here

            // box 1 specification
            DemoBox1.setLength(6.0);
            DemoBox1.setBreadth(7.0);
            DemoBox1.setHeight(5.0);

            // box 2 specification
            DemoBox2.setLength(12.0);
            DemoBox2.setBreadth(13.0);
            DemoBox2.setHeight(10.0);

            //displaying the Boxes using the overloaded ToString():
            Console.WriteLine("DemoBox 1: {0}", DemoBox1.ToString());
            Console.WriteLine("DemoBox 2: {0}", DemoBox2.ToString());

            // volume of DemoBox 1
            volume = DemoBox1.getVolume();
            Console.WriteLine("Volume of DemoBox 1 : {0}", volume);

            // volume of DemoBox 2
            volume = DemoBox2.getVolume();
            Console.WriteLine("Volume of DemoBox 2 : {0}", volume);

            // Add two object as follows:
            DemoBox3 = DemoBox1 + DemoBox2;
            Console.WriteLine("DemoBox 3: {0}", DemoBox3.ToString());

            // volume of DemoBox 3
            volume = DemoBox3.getVolume();
            Console.WriteLine("Volume of DemoBox 3 : {0}", volume);

            //comparing the boxes
            if (DemoBox1 > DemoBox2)
                Console.WriteLine("DemoBox1 is greater than DemoBox2");
            else
                Console.WriteLine("DemoBox1 is not greater than DemoBox2");

            if (DemoBox1 < DemoBox2)
                Console.WriteLine("DemoBox1 is less than DemoBox2");
              else
                Console.WriteLine("DemoBox1 is not less than DemoBox2");

            if(DemoBox1 >= DemoBox2)
                Console.WriteLine("DemoBox1 is greater or equal to DemoBox2");
             else
                Console.WriteLine("DemoBox1 is not greater or equal to DemoBox2");

            if(DemoBox1 <= DemoBox2)
                Console.WriteLine("DemoBox1 is less or equal to DemoBox2");
             else
                Console.WriteLine("DemoBox1 is not less or equal to DemoBox2");

            if(DemoBox1 != DemoBox2)
                Console.WriteLine("DemoBox1 is not equal to DemoBox2");
            else
                Console.WriteLine("DemoBox1 is not greater or equal to DemoBox2");
            DemoBox4 = DemoBox3;

            if(DemoBox3 == DemoBox4)
                Console.WriteLine("DemoBox3 is equal to DemoBox4");
              else
                Console.WriteLine("DemoBox3 is not equal to DemoBox4");

            Console.ReadKey();
        }
    }
}


Sundar  Neupane

Sundar Neupane

I like working on projects with a team that cares about creating beautiful and usable interfaces.

Comments



Report Response