Object Oriented Design Final Exam (From 3:30 pm to 4:45 pm) Name: Section 1 Multiple Choice Questions (40 pts total, 2 pts each): Q1: Employee is a base class and HourlyWorker is a derived class, with a redefined nonvirtual print function. Given the following statements, will the output of the two print function calls be identical? HourlyWorker h; Employee *eptr = &h; eptr->print(); eptr->employee::print(); a. No. b. It would depend on the implementation of the print function. c. Yes. d. Yes, if print is a static function. Q2: The main difference between a pure virtual function and a virtual function is: a. The return type. b. The member access specifier. c. That a pure virtual function cannot have an implementation. d. The location in the class. Q3: What mistake prevents the following class declaration from functioning properly as an abstract class? class Shape public: virtual double print() const; double area() const return base * height; private: double base; double height; ; a. There are no pure virtual functions. b. There is a non-virtual function. c. private variables are being accessed by a public function. d. Nothing, it functions fine as an abstract class. Q4: The relationship between function templates and function-template specializations is most similar to the relationship between: a. Classes and objects. b. Classes and functions. c. Functions and return types. 1
d. Headers and source files. Q5: Function-template specializations: a. Are identical to macros. b. Are generated at compile time. c. Have a maximum allowed number of type parameters. d. Are not more concise than the equivalent set of overloaded functions. Q6: A function template can be overloaded by: a. Using other function templates with the same function name and parameters. b. Using non-template functions with the same name and different parameters. c. Using non-template functions with a different name but the same parameters. d. Using other function templates with a different name but the same parameters. Q7: For a class template, the binary scope resolution operator (::) is needed: a. Only in the definitions of the member functions defined outside the class. b. Both in the prototype and definition of a member function. c. Only if multiple class-template specializations will be created from this class template. d. In neither the definition nor prototype of member functions. Q8: Function templates: a. Can include objects of template classes as parameters. b. Must have return type T. c. Do not need a separate template< typename type > statement if they take objects from a template class as a parameter. d. Do not need a separate template< typename type > statement. Q9: When an object of a derived class is instantiated, the constructor initializes the members. a. Derived class, base class. b. Base class, base class. c. Base class, derived class. d. Derived class, public. Q10: Which forms of inheritance are is-a relationships? a. Only public and private. b. Only public and protected. c. Only public. d. All forms of inheritance are is-a relationships. Q11: Problems using switch logic to deal with many objects of different types other than inheritance do not include: a. Forgetting to include an object in one of the cases. b. Not being able to implement separate functions on different objects. c. Having to update the switch statement whenever a new type of object is added. d. Having to track down every switch statement to do an update of object types. Q12: virtual functions must: a. Be declared virtual in the base class. b. Be overridden in every derived class. 2
c. Be declared virtual in every derived class. d. Have the same implementation in every derived class. Q13: protected base class members cannot be accessed by: a. Functions that are neither friends of the base class, derived-class member functions nor friends of a derived class. b. friends of the base class. c. Functions that are not derived-class member functions. d. friends of derived classes. Q14: Which of the following assignments would be a compilation error? a. Assigning the address of a base-class object to a base-class pointer. b. Assigning the address of a derived-class object to a base-class pointer. c. Assigning the address of a derived-class object to a derived-class pointer. d. Assigning the address of a base-class object to a derived-class pointer. Q15: From most restrictive to least restrictive, the access modifiers are: a. private, protected, public b. protected, private, public c. private, public, protected d. protected, public, private Q16: virtual destructors must be used when: a. The constructor in the base class is virtual. b. delete is used on a derived-class object. c. delete is used on a base-class pointer to a derived-class object. d. A constructor in either the base class or derived class is virtual. Q17: Suppose class B inherits from base class A. What is the order in which their constructors and destructors will be called when an object of class A is instantiated and then destroyed? a. B constructor, A constructor, A destructor, B destructor. b. B constructor, A constructor, B destructor, A destructor. c. A constructor, B constructor, A destructor, B destructor. d. A constructor, B constructor, B destructor, A destructor. Q18: If objects of all the classes derived from the same base class all need to draw themselves, the draw function would most likely be declared: a. private b. protected c. friend d. virtual Q19: The line: virtual double earnings() const = 0; appears in a class definition. You cannot deduce that: a. All classes that directly inherit from this class will override this method. b. This class is an abstract class. c. Any concrete class derived from this class will have an earnings function. d. This class will probably be used as a base class for other classes. 3
Q20: The line: virtual double earnings() const = 0; in a class definition indicates that the class is probably a: a. Base class. b. Derived class. c. Protected class. d. Library class. Section 2 True/False Questions (7 pts total, 1 pt each, please mark \/ or X in front of the questions): 1. Virtual function can use either static or dynamic binding, depending on the handles on which the functions are called. 2. A difference between function-template specializations and overloaded functions is that Function-template specializations cannot accept user-defined types. 3. Common attributes and behaviors should be factored out of closely related classes and placed into a base class from which the original classes can now inherit. 4. Some derived classes can have multiple base classes. 5. When deriving a class from a protected base class, the public members of the base class become protected and the protected members of the base class become protected. 6. Virtual functions must be overridden in every derived class. 7. Programmers should never intentionally instantiate any objects from abstract classes. Section 3 (23 pts) Question 1 (14 pts): (a) (7 pts) Draw an inheritance hierarchy for students at a university. Use Student as the base class of the hierarchy, then include classes UndergraduateStudent and GraduateStudent that derive from Student. Continue to extend the hierarchy to include Freshman, Sophomore, Junior and Senior, and DoctoralStudent and MastersStudent. 4
(b) (7 pts) After drawing the hierarchy, discuss the relationships that exist between the classes: what should be inherited, and which member functions should be virtual and pure virtual. Question 2 (9 pts): Correct the following code: The following code creates an object of type Y. Class Y inherits from class X. There are three errors in the code. Identify and correct them. #include <iostream> using namespace std; // class X definition class X public: // constructor X() cout << "X constructed!"; // end class X constructor ;//endclassx // class Y definition class Y public: // redefine inherited constructor X() cout << "Y created, not X!"; // end class Y constructor ;//endclassy int main() Y yobject(); //endmain 5
Section 4 (30 pts) Question 1. (10 pts) Convert the IntArray to a template version Array class so the class is not bounded to one type, but can be used for int, double, and char. #include <assert.h> // for assert() class IntArray private: int m_nlength; int *m_pndata; public: IntArray() m_nlength = 0; m_pndata = 0; IntArray(int nlength) m_pndata = new int[nlength]; m_nlength = nlength; ~IntArray() delete[] m_pndata; void Erase() delete[] m_pndata; m_pndata = 0; m_nlength = 0; int& operator[](int nindex) assert(nindex >= 0 && nindex < m_nlength); return m_pndata[nindex]; int GetLength(); ; int IntArray ::GetLength() return m_nlength; 6
Question 2. (20 pts) Write three classes: Animal, Dog, Cat, Mouse. The Animal class should be an abstract class. #include <iostream> #include "Animal.h" #include "Dog.h" #include "Cat.h" #include "Mouse.h" const int cnumanimals = 3; int main() Animal* panimal[cnumanimals] = new Dog, new Cat, new Mouse ; for (int i = 0 ; i < cnumanimals ; ++i) std::cout << panimal[i]->speak() << std::endl; delete panimal[i]; return 0; Output: Woof woof! meow meow! zzzzzzzzz! 7
8