Euro coins lying on euro banknotes
Photo by Avij from Wikimedia Commons, public domain, cropped

Object-Oriented Programming

The Money Class

Press Calculate.

How it works

The class keeps a sum in three fields: the sign, the whole part and the cents; they are shown under the result. The arithmetic and comparison operators are overloaded, so sums are added, multiplied and compared like numbers, and a plain number is first turned into a sum. A result is rounded to cents, and exactly half a cent goes to the even neighbour, as Math.Round does in C#: $200.55 at the rate of 1.2 is 167.125 euros and becomes €167.12. Dollars and Euros inherit from Money and differ in the currency sign and the exchange rate.

Checks from my lab works, calculated again on this page
Expression In the lab On this page

From the course

  • Semester 5
  • Lab works 1–4: classes, polymorphism, inheritance, events
  • Written in C#

Useful resources

  1. Operator overloading the C# reference from Microsoft