Ask Question
3 July, 10:03

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.

+2
Answers (1)
  1. 3 July, 13:53
    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?
Get an answer to your question ✅ “Consider the following code: public class CustomerList{ private List customers; public delegate void ChangeHandler (CustomerList ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers