Test Your Knowledge: Exercises

Exercise 12-1. Create a class Invoice, which has a string member vendor and a double member amount, as well as a method to output the two properties of the invoice. Overload the addition operator so that if the vendor properties match, the amount properties of the two invoices are added together in a new invoice. If the vendor properties do not match, the new invoice is blank. Include some test code to test the addition operator.

Exercise 12-2. Modify the Invoice class so that two invoices are considered equal if the vendor and amount properties match. Test your methods.

Exercise 12-3. Modify the Invoice class once more so that you can determine whether one invoice is greater than or less than another. Test your methods.

Exercise 12-4. Create a class Foot and a class Meter. Each should have a single parameter that stores the length of the object, and a simple method to output that length. Create a casting operator for each class: one that converts a Foot object to a Meter object, and one that converts a Meter object to a Foot object. Test these operators to make sure they work.

Get Learning C# 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.