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

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Class Interface » Operator OverloadingScreenshots 
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 (1228"This is part");
            clsPoint point2 = new clsPoint (4264" 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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.