Point: Overloaded Operators : Logic Operator Overload « Operator Overload « C# / CSharp Tutorial

C# / CSharp Tutorial
1. Language Basics
2. Data Type
3. Operator
4. Statement
5. String
6. struct
7. Class
8. Operator Overload
9. delegate
10. Attribute
11. Data Structure
12. Assembly
13. Date Time
14. Development
15. File Directory Stream
16. Preprocessing Directives
17. Regular Expression
18. Generic
19. Reflection
20. Thread
21. I18N Internationalization
22. GUI Windows Forms
23. 2D
24. Design Patterns
25. Windows
26. XML
27. ADO.Net
28. Network
29. Directory Services
30. Security
31. unsafe
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorial
C# / C Sharp
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
C# / CSharp Tutorial » Operator Overload » Logic Operator Overload 
8. 2. 2. Point: Overloaded Operators
using System;
using System.Collections.Generic;
using System.Text;

public struct Point : IComparable {
    private int x, y;
    public Point(int xPos, int yPos) {
        x = xPos;
        y = yPos;
    }
    public static Point operator +(Point p1, Point p2) { return new Point(p1.x + p2.x, p1.y + p2.y)}
    public static Point operator -(Point p1, Point p2) { return new Point(p1.x - p2.x, p1.y - p2.y)}
    public static bool operator ==(Point p1, Point p2) { return p1.Equals(p2)}
    public static bool operator !=(Point p1, Point p2) { return !p1.Equals(p2)}
    public static bool operator <(Point p1, Point p2) { return (p1.CompareTo(p20)}
    public static bool operator >(Point p1, Point p2) { return (p1.CompareTo(p20)}
    public static bool operator <=(Point p1, Point p2) { return (p1.CompareTo(p2<= 0)}
    public static bool operator >=(Point p1, Point p2) { return (p1.CompareTo(p2>= 0)}
    public static Point operator ++(Point p1) { return new Point(p1.x + 1, p1.y + 1)}
    public static Point operator --(Point p1) { return new Point(p1.x - 1, p1.y - 1)}
    public override bool Equals(object o) {
        if (o is Point) {
            if (((Point)o).x == this.x &&
                ((Point)o).y == this.y)
                return true;
        }
        return false;
    }

    public override int GetHashCode() { return this.ToString().GetHashCode()}
    public override string ToString() {
        return string.Format("[{0}, {1}]"this.x, this.y);
    }
    public int CompareTo(object obj) {
        if (obj is Point) {
            Point p = (Point)obj;
            if (this.x > p.x && this.y > p.y)
                return 1;
            if (this.x < p.x && this.y < p.y)
                return -1;
            else
                return 0;
        else
            throw new ArgumentException();
    }
    public static Point Add(Point p1, Point p2) { return p1 + p2; }
    public static Point Subtract(Point p1, Point p2) { return p1 - p2; }
}
class Program {
    static void Main(string[] args) {
        Point ptOne = new Point(100100);
        Point ptTwo = new Point(4040);
        Console.WriteLine("ptOne = {0}", ptOne);
        Console.WriteLine("ptTwo = {0}", ptTwo);
        Console.WriteLine("ptOne + ptTwo: {0} ", ptOne + ptTwo);
        Console.WriteLine("Point.Add(ptOne, ptTwo): {0} ", Point.Add(ptOne, ptTwo));

        Console.WriteLine("ptOne - ptTwo: {0} ", ptOne - ptTwo);
        Console.WriteLine("Point.Subtract(ptOne, ptTwo): {0} ", Point.Subtract(ptOne, ptTwo));

        Point ptThree = new Point(905);
        Console.WriteLine("ptThree = {0}", ptThree);
        Console.WriteLine("ptThree += ptTwo: {0}", ptThree += ptTwo);

        Point ptFour = new Point(0500);
        Console.WriteLine("ptFour = {0}", ptFour);
        Console.WriteLine("ptFour -= ptThree: {0}", ptFour -= ptThree);

        Point ptFive = new Point(1010);
        Console.WriteLine("ptFive = {0}", ptFive);
        Console.WriteLine("++ptFive = {0}", ++ptFive);
        Console.WriteLine("--ptFive = {0}", --ptFive);

        Console.WriteLine("ptOne == ptTwo : {0}", ptOne == ptTwo);
        Console.WriteLine("ptOne != ptTwo : {0}", ptOne != ptTwo);
        Console.WriteLine("ptOne < ptTwo : {0}", ptOne < ptTwo);
        Console.WriteLine("ptOne > ptTwo : {0}", ptOne > ptTwo);
    }
}
8. 2. Logic Operator Overload
8. 2. 1. Complex number with logic operators
8. 2. 2. Point: Overloaded Operators
w___w__w.j___a_v__a2_s___.c_o_m_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.