CS 1704 Fall 2003 Homework 2 Answers Q A Reason -------------------------------------------------------------------------------- 1 3 An observer allows the client to obtain the value of a data member. 2 4 A mutator allows the client to modify the value of a data member. 3 5 1 is fine. 2 is an illegal attempt to access a private member from client code. 3 is fine; there is a default assignment operator for every class. 4 uses the type name CreditCard as if it were the name of an object. 4 6 Straight from the course notes. 5 6 Assignment is provided automatically, but no other operators are. 6 5 The parameter should be passed by value, the return type should be bool, and the function should be const since it doesn't modify any data members. 1 shouldn't pass the parameter by reference, and it should be const 2 shouldn't restrict the return value to const. 3 shouldn't pass the parameter by reference. 4 combines the faults of 2 and 3. 7 2 The correct syntax for calling a member function is: ObjectName.FunctionName(parameters) 8 3 This would require both an observer function and a mutator for the relevant data member. 1 is necessary, but doesn't provide support for modifying the value 2 doesn't allow the client to inspect the value 3 does what's required 4 is just bad design 5 is irrelevant to the question 6 is simply perverse 9 2 The declaration doesn't provide any parameters, so Foo would be initialized by the default constructor for the class. But, there is no implementation for that constructor, so it is not possible to say what the value of the data member would be. 10 1 The automatic assignment operator for a class simply copies each data member from the RHS object to the corresponding (by name) data member of the LHS (using the assignment operator determined by the type of the data member). 11 4 The addition operator is implemented to just add the tops and the bottoms together; then the automatic assignment operator just copies those values into the right members of E. 12 1 The previous operation didn't change the members of A, and Display() just writes the value of A.Top, followed by a slash, followed by the value of A.Bottom, with no extra spaces. 13 3 The implementation of the subtraction operator just subtracts the corresponding data members; so this is just an extension of question 11. 14 4 There is no definition of an operator named * that would combine an int and a Farey. 15 2 The definition of == for Farey objects only returns true if the corresponding data members are equal, so X and Y are NOT equal. 16 1 The expression X + X creates a Farey object with data members 2 and 4; that WILL compare equal to Y. 17 3 Examining the constructor and the implementation of Evaluate(), it is clear that F represents the polynomial 1 + 2x + 3x^2. So, the call Evaluate(2) will return 1 + 2*2 + 3*2*2 == 17. 18 1 Given the default values for the constructor parameters, G will represent the polynomial 1. So, the call Evaluate(2) will return 1. 19 3 The addition operator should create and return an object of the class type. (After all, adding two quadratic polynomials yields another quadratic polynomial.) 20 1 We still haven't created a Quadratic object to return, so that must happen here. 21 2 Just count operations... the version given here takes one more multiply. (The version in the code is Horner's algorithm.) 22 2 Since the description says a Vendor doesn't make change. 1 would be OK if change were returned (or the customer ripped off). 3 is syntactically incorrect 23 5 It should be: Stock == 0 1 would accept money only if the machine was empty. 2 would accept money only if the machine wasn't full. 3 is syntactically incorrect 24 3 It's got to be of type Response... no other value makes sense. 25 5 26 6 There is no constructor that accepts 3 parameters. 27 7 The choice of type doesn't affect memory usage, since int and unsigned int both occupy 4 bytes. 2 is clearly a sound reason to use unsigned. 3 is less clear, but using unsigned lets us eliminate error-checking code to handle negative values. 28 1 This never checks to see if the list is full before accepting another entry; if the list IS full, putNext will have wrapped around and be the index of the first entry. 29 9 If the list is empty, the if simply returns false. The update of doNext may not be obvious, but it is correct. It cannot be const, since it does modify data members. 30 4 The loop starts at index zero, and processes nTasks elements. But the structure stores the elements in "circular" fashion, so there's no reason the first one will be at index zero, or that there will be an (real) element there at all; and, if you start at the wrong place, there's no reason to think it will process all of the entries.