Ask Question

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.

+4
Answers (1)
  1. 29 June, 00:36
    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 & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers