Demonstrates overloading the addition operator for two class objects : Operator Overloading « Class Interface « C# / C Sharp






Demonstrates overloading the addition operator for two class objects

Demonstrates overloading the addition operator for two class objects
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/

//
// Plus.cs -- demonstrates overloading the addition operator for two
//            class objects.
//
//            Compile this program with the following command line:
//                C:>csc Plus.cs
//
namespace nsOverload
{
    using System;
    
    public class PlusclsMain
    {
        static public void Main ()
        {
            clsPoint point1 = new clsPoint (12, 28, "This is part");
            clsPoint point2 = new clsPoint (42, 64, " of a string");
            clsPoint point3 = point1 + point2;
            Console.WriteLine ("Results for point3:");
            Console.WriteLine ("\tPoint is at " + point3);
            Console.WriteLine ("\tstr = " + point3.str);
        }
    }
    class clsPoint
    {
        public clsPoint () { }
        public clsPoint (int x, int y, string str)
        {
            m_cx = x;
            m_cy = y;
            this.str = str;
        }
        private int m_cx = 0;
        private int m_cy = 0;
        public int cx
        {
            get {return (m_cx);}
            set {m_cx = value;}
        }
        public int cy
        {
            get {return (m_cy);}
            set {m_cy = value;}
        }
      
      public string str = "";

        static public clsPoint operator +(clsPoint pt1, clsPoint pt2)
        {
            clsPoint point = new clsPoint();
            point.cx = pt1.cx + pt2.cx;
            point.cy = pt1.cy + pt2.cy;
            point.str = pt1.str + pt2.str;
            return (point);
        }

        public override string ToString()
        {
            return ("(" + m_cx + "," + m_cy + ")");
        }
    }
}


           
       








Related examples in the same category

1.An example of operator overloadingAn example of operator overloading
2.More operator overloadingMore operator overloading
3.Overload addition for object + object, and for object + intOverload addition for object + object, and 
   for object + int
4.Overload the + for object + object, object + int, and int + objectOverload the + for object + object, 
   object + int, and int + object
5.Overload shift operatorOverload shift operator
6.Overload true and fase for ThreeDOverload true and fase for ThreeD
7.A better way to overload !, | and & for ThreeD. This version automatically enables the && and || operatorsA better way to overload !, | and & for ThreeD. 
   This version automatically enables the && and || operators
8.illustrates operator overloadingillustrates operator overloading
9.overloaded operator + takes two fractionsoverloaded operator + takes two fractions
10.Overloaded operator: whether two Fractions are equalOverloaded operator: whether two Fractions are equal
11.Overload != operatorOverload != operator
12.Operator Overloading:An ExampleOperator Overloading:An Example
13.Sorting and Searching:Overloading Relational OperatorsSorting and Searching:Overloading Relational Operators