Operator Overloading

using System;

using System.Windows.Forms;

 

namespace overload1

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            VectorMatrix A = new VectorMatrix(new double [] { 2, 3, 4 });

            VectorMatrix B = new VectorMatrix(new double [] { 8, 7, 6 });

            VectorMatrix C = A * B;

 

            for (int i = 0; i < C.VectorSize; i++)

            {

                label1.Text += C.position[i] + " "//implicit casting from double to string

            }

 

        }

 

      

    }

 

    public class VectorMatrix

    {

      

        public double[] position;

        public int VectorSize;

 

        public VectorMatrix(params double[] x)

        {

            VectorSize = x.Length;

            position = new double [x.Length];

            for (int i = 0; i < x.Length; i++)

                position[i] = x[i];

        }

 

        //operator "*" overloading

        public static VectorMatrix operator * (VectorMatrix A, VectorMatrix B)

        {

            double []X = new double [A.VectorSize];

            VectorMatrix C = new VectorMatrix(X);

 

             for (int i = 0; i < A.VectorSize; i++)

                 C.position[i] = A.position[i] * B.position[i];

 

             return C;

        }

 

    }

}

 



Source