Operator Overloading:An Example : 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.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# Book
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Class Interface » Operator OverloadingScreenshots 
Operator Overloading:An Example
Operator Overloading:An Example

/*
A Programmer's Introduction to C# (Second Edition)
by Eric Gunnerson

Publisher: Apress  L.P.
ISBN: 1-893115-62-3
*/
// 25 - Operator Overloading\An Example
// copyright 2000 Eric Gunnerson
using System;
struct RomanNumeral
{
    public RomanNumeral(int value)
    {
        this.value = value;
    }
    public override string ToString()
    {
        return(value.ToString());
    }
    public static RomanNumeral operator -(RomanNumeral roman)
    {
        return(new RomanNumeral(-roman.value));
    }
    public static RomanNumeral operator +(
    RomanNumeral    roman1,
    RomanNumeral    roman2)
    {
        return(new RomanNumeral(
        roman1.value + roman2.value));
    }
    
    public static RomanNumeral operator ++(
    RomanNumeral    roman)
    {
        return(new RomanNumeral(roman.value + 1));
    }
    int value;
}
public class OperatorOverloadingAnExample
{
    public static void Main()
    {
        RomanNumeral    roman1 = new RomanNumeral(12);
        RomanNumeral    roman2 = new RomanNumeral(125);
        
        Console.WriteLine("Increment: {0}", roman1++);
        Console.WriteLine("Addition: {0}", roman1 + roman2);
    }
}

           
       
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.Demonstrates overloading the addition operator for two class objectsDemonstrates overloading the addition operator for two class objects
10.overloaded operator + takes two fractionsoverloaded operator + takes two fractions
11.Overloaded operator: whether two Fractions are equalOverloaded operator: whether two Fractions are equal
12.Overload != operatorOverload != operator
13.Sorting and Searching:Overloading Relational OperatorsSorting and Searching:Overloading Relational Operators
java2s.com  |  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.