CSE2050 Exam #3. Open book, open notes. Name _________________________ 1. Write the destructor and the two other methods required by the "rule of 3" for class D. Assume that B is abstract with no constructor or data members. (40 pts). template class D: public B { T* p; public: D(const T& t): p(new T(t)) {} void print() const {cout << *p;} // Answer ~D() {delete p;} D(const D& d): p(new T(*d.p)) {} D& operator=(const D& d) { *p = *d.p; return *this; } /* or D& operator=(const D& d) { if (&d != this) { delete p; p = new T(*d.p); } return *this; } */ }; 2. Write the abstract base class B that specifies the interface for both D and E. (15 pts). class E: public B { public: void print() const {} }; // Answer class B { public: virtual void print() const = 0; virtual ~B() {} // -5 if you forgot this }; 3. Given the classes above, are each of these statements legal? If no, explain why not. (3 pts each). // -1 if you got the right answer but the wrong reason B b; // no, B is abstract const D d(5); // yes E e; // yes B* p; // yes p = new B; // no, B is abstract p = new D; // no, D requires a template and constructor argument p = new E; // yes p.print(); // no, p is a pointer p->print(); // yes E::print(); // no, print() is not static B &r = *p; // yes e = r; // no, conversion from base to derived e = *p; // no, conversion from base to derived e = new E; // no, e is not a pointer d = 0; // no, d is const