Operator Overloading: A Complex Number Class : 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 » operator overload 
8. 1. 1. Operator Overloading: A Complex Number Class
  1. If you overload the == operator, then you need to override Equals(object) and GetHashCode().
  2. The == operator and the Equals(object) methods should function the same way.
  1. When Equals() is overridden, you should also override GetHashCode().
  2. Equals() and GetHashCode should be compatible.
using System;

struct Complex
{
    float real;
    float imaginary;
    
    public Complex(float real, float imaginary)
    {
        this.real = real;
        this.imaginary = imaginary;
    }
    
    public float Real
    {
        get
        {
            return(real);
        }
        set
        {
            real = value;
        }
    }
    
    public float Imaginary
    {
        get
        {
            return(imaginary);
        }
        set
        {
            imaginary = value;
        }
    }
    
    public override string ToString()
    {
        return(String.Format("({0}, {1}i)", real, imaginary));
    }
    
    public static bool operator==(Complex c1, Complex c2)
    {
        if ((c1.real == c2.real&& (c1.imaginary == c2.imaginary))
           return(true);
        else
           return(false);
    }
    
    public static bool operator!=(Complex c1, Complex c2)
    {
        return(!(c1 == c2));
    }
    
    public override bool Equals(object o2)
    {
        Complex c2 = (Complexo2;
        
        return(this == c2);
    }
    
    public override int GetHashCode()
    {
        return(real.GetHashCode() ^ imaginary.GetHashCode());
    }
    
    public static Complex operator+(Complex c1, Complex c2)
    {
        return(new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary));
    }
    
    public static Complex operator-(Complex c1, Complex c2)
    {
        return(new Complex(c1.real - c2.real, c1.imaginary - c2.imaginary));
    }
    
    public static Complex operator*(Complex c1, Complex c2)
    {
        return(new Complex(c1.real * c2.real - c1.imaginary * c2.imaginary,
        c1.real * c2.imaginary + c2.real * c1.imaginary));
    }
    
    public static Complex operator/(Complex c1, Complex c2)
    {
        if ((c2.real == 0.0f&& (c2.imaginary == 0.0f))
           throw new DivideByZeroException("Can't divide by zero Complex number");
        
        float newReal = (c1.real * c2.real + c1.imaginary * c2.imaginary(c2.real * c2.real + c2.imaginary * c2.imaginary);
        float newImaginary = (c2.real * c1.imaginary - c1.real * c2.imaginary(c2.real * c2.real + c2.imaginary * c2.imaginary);
        
        return(new Complex(newReal, newImaginary));
    }
  
}

class MainClass
{
    public static void Main()
    {
        Complex c1 = new Complex(31);
        Complex c2 = new Complex(12);
        
        Console.WriteLine("c1 == c2: {0}", c1 == c2);
        Console.WriteLine("c1 != c2: {0}", c1 != c2);
        Console.WriteLine("c1 + c2 = {0}", c1 + c2);
        Console.WriteLine("c1 - c2 = {0}", c1 - c2);
        Console.WriteLine("c1 * c2 = {0}", c1 * c2);
        Console.WriteLine("c1 / c2 = {0}", c1 / c2);
    }
}
c1 == c2: False
c1 != c2: True
c1 + c2 = (4, 3i)
c1 - c2 = (2, -1i)
c1 * c2 = (1, 7i)
c1 / c2 = (1, -1i)
8. 1. operator overload
8. 1. 1. Operator Overloading: A Complex Number Class
8. 1. 2. Operator Overloading for your own class
8. 1. 3. A better way to overload !, |, and & for TwoDimension.
ww_w.__j__a___va___2__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.