= Currency = ''Part of'' OperatorOverloadingCategory == Description == A currency object featuring some operator overloading. == Example == {{{ #!d import std.stdio; import std.string; import std.math; char[] leadingZeros(byte num, byte digits) { char[] buffer; byte diff; buffer = toString(num); if (buffer.length < digits) { diff = digits - buffer.length; for(int i=0; i < diff; i++) buffer = "0" ~ buffer; } return buffer; } class Currency { /* Read property ("get-ter") */ double value() {return intValue;} /* Write property ("set-ter") */ double value(double v) {return intValue = v;} this(double v) { version(TEST) writefln("An instance of the Currency object is created.\n"); intValue = v; } this() { this(0); } char[] toString() { return std.string.toString(intValue); } /* operator overloading */ Currency opAdd(Currency m, Currency n) { Currency t; t.value = m.value + n.value; return t; } Currency opAddAssign(Currency m) { intValue += m.value; return this; } private { double intValue; } } void main() { Currency c = new Currency(); writefln("\n\t\t\t\t\t\t%s", c.toString); writef("Change the value:\t\t\t\t"); c.value = 100.75; writefln("%s", c.toString); Currency c2 = new Currency(26.12); writefln("\t\t\t\t\t\t%s", c2.toString); writef("Change the value:\t\t\t\t"); c2.value = 6.25; writefln("%s", c2.toString); writef("Change the value (operator overloading):\t"); c2 += c; /* requires operator overloading */ writefln("%s\n", c2.toString); } }}} == Example batch file == {{{ @echo off set pgm=CurrencyExample dmd %pgm%.d %pgm%.exe pause erase %pgm%.obj erase %pgm%.map }}} == Output == {{{ An instance of the Currency object is created. 0 Change the value: 100.75 An instance of the Currency object is created. 26.12 Change the value: 6.25 Change the value (operator overloading): 107 }}} == Tests == * Tested with DMD 0.173. * Comiles and runs on Windows 2000.