CSE2050 Exam #3, Open book, open notes. Name __________________________ 1. For each set of classes below, which would be the base class? Which classes are derived? (some may be neither). (5 pts each) ostream, ofstream Base ostream Derived ofstream House, Room, Building Base Building Derived House Integer, RealNumber Base RealNumber Derived Integer Student, Teacher, Person, Grade Base Person Derived Student, Teacher Look for the IS A relationship. A house is a building, but a room is not a house or a building and a house or building is not a room. All integers are real numbers, but not all real numbers are integers. 2. Write a constructor that converts a map to a MapString (10 pts). template class MapString: public map { public: // Answer - initialize the base class through its copy constructor MapString(const map& m): map(m) {} }; 3. Show what each statement prints, or if it is an error (5 pts each). class Bird { private: string color; public: Bird(string s = "blue"): color(s) {} virtual void sing() const {cout << "chirp";} virtual ~Bird() {cout << "bye ";} // Answer to question 5 private: Bird(const Bird& b); // Code is optional, since it can never be called. // This also disallows copying Ducks because the default copy // constructor for Duck is // // Duck(const Duck& d): Bird(d) {} // Error, is private // // If you leave the copy constructor public but omit the code as // above, then this is still correct because any attempt to call // it will result in a linker error (can't find // Bird::Bird(const Bird&); ), although the error message is harder // to understand. }; class Duck: public Bird { public: Duck(); // Defined in question 4 void sing() const {cout << "quack";} ~Duck() {cout << "good ";} }; main() { Duck d; d.sing(); // 3a quack (d is a Duck) Bird b = d; b.sing(); // 3b chirp (b is a Bird) Bird& rb = d; rb.sing(); // 3c quack (rb refers to a Duck) Duck& rd = b; rd.sing(); // 3d ERROR, can't convert to derived Bird* p = new Duck(); p->sing(); // 3e quack (p points to a Duck) delete p; // 3f good bye (both destructors) } 3g. What would 3f print if ~Bird() was not virtual? bye 3h. What would 3f print if Bird had no destructor? (nothing) 3i. Is class Bird abstract? No, you can create b 3j. Is color accessible to the methods of Duck? No, it is private 4. Write the code for Duck::Duck() that sets color to "brown" (10 pts). Duck::Duck(): Bird("brown") {} // Not color("brown") {} (Not Duck::Duck(const string& s = "brown"): Bird(s) {} either, because the constructor above takes no arguments). 5. Add a method to Bird in the space provided above to disallow copying of both Birds and Ducks. (10 pts).