CS 1704 Fall 2002 Homework 1 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 8 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 3 Straight from the course notes. 5 1 Straight from the reading in Deitel. 6 1 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. 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. 5 shouldn't pass the parameter by reference. 7 2 The correct syntax for calling a member function is: ObjectName.FunctionName(parameters) 8 1 This is the purpose of observers. 2 doesn't allow the client to inspect the value; and it does allow the client to modify the data member. 3 is simply bad design in almost all cases. 4 is irrelevant to the problem. 5 exhibits a perverse attitude. 9 2,3 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. However, the Standard only guarantees the existence of an implicit default constructor if no other constructors are declared; thus it is entirely possible the declaration of Foo is not legal. 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 2 Examining the constructor and the implementation of Evaluate(), it is clear that F represents the polynomial x^2 + 2x + 3. So, the call Evaluate(2) will return 11. 18 2 Given the default values for the constructor parameters, G will represent the polynomial x^2 + 0x + 0, or x^2. So, the call Evaluate(2) will return 4. 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.