Ask Question
5 October, 04:57

Consider the following code:

public class CustomerList{

private List customers;

public delegate void ChangeHandler (CustomerList customers);

public event ChangeHandler ChangedList;

public CustomerList () {customers = new List ();

}

public void Add (Customer c) {customers. Add (c);

}

public static CustomerList operator + (CustomerList customers, Customer c) {

customers. Add (c); return customers;

}

}

As you can see, the CustomerList class overloads the binary + operator to make it easier for you to add Customer objects to a CustomerList object.

Now, write code that uses the + = operator to add a Customer object named newCustomer to a CustomerList object named customers.

+3
Answers (1)
  1. 5 October, 05:27
    0
    customers + = newCustomer;

    Explanation:

    The operator + = expands into + and assignment. The assignment is not overloaded so the required code is

    customers + = newCustomer;

    This expands into

    customers = customers + newCustomer;

    The overloaded + operator is called for the right expression. This returns a 'CustomerList', which is then assigned through the = operator to 'customers'.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Consider the following code: public class CustomerList{ private List customers; public delegate void ChangeHandler (CustomerList ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers