A.12. Chapter 12: Operator Overloading A.12.1. Quiz -
Solution to Question 121 . -
The process of writing methods for your class that allow clients of your class to interact with your class using standard operators ( + , == ). -
Solution to Question 122 . -
Static methods. -
Solution to Question 123 . -
Call to: public static Fraction operator+(f2, f1) -
Solution to Question 124 . -
Overload the Equals( ) method. -
Solution to Question 125 . -
Use implicit conversion when you know the conversion will succeed without the risk of losing information. Use explicit conversion if information might be lost. A.12.2. Exercises -
Solution to Exercise 12-1 . -
Create a class Invoice , that has a string property v endor and a double property amount . 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. using System; public class Invoice { private string vendor; private double amount; public string Vendor { get { return vendor; } set { vendor = value; } } public double Amount { get { return amount; } set { amount = value; } } // constructor public Invoice(string vendor, double amount) { this.vendor = vendor; this.amount = amount; } // Overloaded operator + takes two invoices. // If the vendors are the same, the two amounts are added. // If not, the operation fails, and a blank invoice is returned. public static Invoice operator +(Invoice lhs, Invoice rhs) { if (lhs.vendor == rhs.vendor) { return new Invoice(lhs.vendor, lhs.amount + rhs.amount); } else { Console.WriteLine("Vendors don't match; operation failed."); return new Invoice("", 0); } } public void PrintInvoice() { Console.WriteLine("Invoice from {0} for .", this.vendor, this.amount); } } public class Tester { public void Run( ) { Invoice firstInvoice = new Invoice("TinyCorp", 345); Invoice secondInvoice = new Invoice("SuperMegaCo", 56389.53); Invoice thirdInvoice = new Invoice("SuperMegaCo", 399.65); Console.WriteLine("Adding first and second invoices."); Invoice addedInvoice = firstInvoice + secondInvoice; addedInvoice.PrintInvoice( ); Console.WriteLine("Adding second and third invoices."); Invoice otherAddedInvoice = secondInvoice + thirdInvoice; otherAddedInvoice.PrintInvoice( ); } static void Main( ) { Tester t = new Tester( ); t.Run( ); } } -
Solution to Exercise 12-2 . -
Modify the Invoice class so that two invoices are considered equal if the vendor and amount properties match. Test your methods. using System; public class Invoice { private string vendor; private double amount; public string Vendor { get { return vendor; } set { vendor = value; } } public double Amount { get { return amount; } set { amount = value; } } // constructor public Invoice(string vendor, double amount) { this.vendor = vendor; this.amount = amount; } // Overloaded operator + takes two invoices. // If the vendors are the same, the two amounts are added. // If not, the operation fails, and a blank invoice is returned. public static Invoice operator+ (Invoice lhs, Invoice rhs) { if (lhs.vendor == rhs.vendor) { return new Invoice(lhs.vendor, lhs.amount + rhs.amount); } else { Console.WriteLine("Vendors don't match; operation failed."); return new Invoice("", 0); } } // overloaded equality operator public static bool operator== (Invoice lhs, Invoice rhs) { if ( lhs.vendor == rhs.vendor && lhs.amount == rhs.amount ) { return true; } return false; } // overloaded inequality operator, delegates to == public static bool operator !=(Invoice lhs, Invoice rhs) { return !(lhs == rhs); } // method for determining equality; tests for same type, // then delegates to == public override bool Equals(object o) { if (!(o is Invoice)) { return false; } return this == (Invoice)o; } public void PrintInvoice() { Console.WriteLine("Invoice from {0} for .", this.vendor, this.amount); } } public class Tester { public void Run( ) { Invoice firstInvoice = new Invoice("TinyCorp", 399.65); Invoice secondInvoice = new Invoice("SuperMegaCo", 56389.53); Invoice thirdInvoice = new Invoice("SuperMegaCo", 399.65); Invoice testInvoice = new Invoice("SuperMegaCo", 399.65); if (testInvoice == firstInvoice) { Console.WriteLine("First invoice matches."); } else if (testInvoice == secondInvoice) { Console.WriteLine("Second invoice matches."); } else if (testInvoice == thirdInvoice) { Console.WriteLine("Third invoice matches."); } else { Console.WriteLine("No matching invoices."); } } static void Main( ) { Tester t = new Tester( ); t.Run( ); } } |