Chapter 12: Operator Overloading

Quiz

Solution to Question 12–1.

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 12–2.

Static methods.

Solution to Question 12–3.

Call to:

public static Fraction operator+(f2, f1)
Solution to Question 12–4.

Overload the Equals( ) method.

Solution to Question 12–5.

Use implicit conversion when you know the conversion will succeed without the risk of losing information. Use explicit conversion if information might be lost.

Exercises

Solution to Exercise 12-1.

Create a class Invoice, that has a string property vendor 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 ${1}.", 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 ${1}.", 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(  );
    }
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset