Fall 2003 Exam #3, open books, open notes. Name _________________________ 1. Circle T for true or F for false (2 pts each). A derived class may access inherited private data members. F A derived class constructor should initialize inherited data members. F You cannot override a function unless it is virtual. F The default behavior of the assignment operator in a derived class is to call the assignment operator of the base class. T A base class with an empty virtual destructor should define an empty virtual copy constructor and empty virtual assignment operator. F When overriding a member function, it should take the same parameters. T A pointer to base class may point to a derived object. T Derived constructors override base constructors. F Making a function virtual increases the size of every instance. T When you assign a derived object to a base object, its type changes to that of the derived object. F A class with pure virtual functions is abstract. T If B is an abstract base class, you may have objects of type B. F If B is abstract, then a function may have parameters of type B& T If B is abstract, then you may have a pointer of type B* T If B is abstract, then you may have a vector F Given classes Square and Polygon, the base class should be Square. F Given classes Square and Triangle, neither should be a base class. T A pointer to base class may point to a derived object. T If class A is declared as a member of class B, then B is a base class. F If class A is declared as a member of class B, then A may access the protected members of class B. F 2. Write class B as an ABSTRACT base class of D (30 pts). class D: public B { public: void print() const {cout << "hello\n";} ~D() {cout "bye\n";} }; // ANSWER class B { public: virtual void print() const = 0; virtual ~B() {} // you can add a protected constructor if you like }; 3. Derive class Employee from Person such that you can execute the test code shown (30 pts). class Person { private: string* name_ptr; public: Person(const string& name): name_ptr(new string(name)) {} virtual ~Person() {delete name_ptr;} virtual void print() const {cout << *name_ptr;} }; // test code Employee e1("Joe", 25000); // name and salary e1.print(); // prints name and salary, e.g. Joe 25000 // ANSWER class Employee: public Person { private: int salary; // or double salary public: Employee(const string& name, int s): Person(name), salary(s) {} void print() const { Person::print(); // not cout << *name_ptr, it's private cout << " " << salary; } }; // No destructor. Do not delete salary, it isn't a pointer