The superscript "(d)" stands for "difficult". Exercises similar to those marked with "(d)" might appear in candidacy exams, but not in the standard exams of CSE 428.
class C { protected: int x; public: void f(){...}; }; class C1: public C { protected: int x1; public: void h(C *obj){...}; }; class C2: public C1 { public: int x2; }; class C3: public C { public: f(){...}; };
C1 obj1;For each of the following expressions, say whether it is allowed in the code of main or not (they can be forbidden either because they violate the class definition or the protection mechanism)
obj1.x , obj1.f() , obj1.x1 , obj1.x2
C2 *obj2; C3 *obj3;For each of the following expressions, say whether it is allowed in the body of C1::h or not
obj->x , obj2->x , obj3->x
obj->f();Assume that we call C1::h with a parameter (pointing to an object) of class C3. What is the method f executed, C::f() or C3::f()? And what would be the method executed if f were declared virtual in C?
C / \ / \ C1 C3 | | C2
Below is the result of compilation and execution using g++
// FILE Exercise_1.C #include < iostream.h > class C { protected: int x; public: void f(){ cout << "executes C::f()" << '\n'; } virtual void g(){ cout << "executes C::g()" << '\n'; } }; class C1: public C { protected: int x1; public: void h(C *); }; class C2: public C1 { public: int x2; }; class C3: public C { protected: int x3; public: void f(){ cout << "executes C3::f()" << '\n'; } void g(){ cout << "executes C3::g()" << '\n'; } }; void C1::h(C *obj){ C2 *obj2 = new C2; C3 *obj3 = new C3; //obj->x = 0; obj2->x = 2; //obj3->x = 3; cout << "method obj->f() from C1::h: "; obj->f(); cout << "method obj->g() from C1::h: "; obj->g(); cout << "method obj3->f() from C1::h: "; obj3->f(); } void main(){ C1 obj1; //obj1.x = 0; cout << "method obj1.f() from main: "; obj1.f(); //obj1.x1 = 1; //obj1.x2 = 2; C3 o3; obj1.h(&o3); } /* Errors (before commenting the lines producing errors) % g++ Exercise_1.C Exercise_1.C: In method `void C1::h(class C *)': Exercise_1.C:32: member `x' is a protected member of class `C' Exercise_1.C:34: member `x' is protected Exercise_1.C: In function `int main(...)': Exercise_1.C:41: member `x' is protected Exercise_1.C:43: member `x1' is a protected member of class `C1' Exercise_1.C:44: `class C1' has no member named `x2' */ /* Result of execution % a.out method obj1.f() from main: executes C::f() method obj->f() from C1::h: executes C::f() method obj->g() from C1::h: executes C3::g() method obj3->f() from C1::h: executes C3::f() */
template < class T > class cell { protected: T info; public: void set(T x){ info = x; } T get() { return info; } };Define the subclass colored_cell by extending the class cell with:
template < class T > class colored_cell: public cell< T >{ protected: char color; public: void set_color(char c){ color = c; } char get_color() { return color; } T get() { if (color != 'w') return info; else return 0; } };
~Item(){ if (source!=NULL) delete source; }
main(){ List *l = new List(); l->push(1); l->push(2); delete l; }This program leaves memory leaks. Why? Change the definition of ~List so that, when it is invoked, the destructor deallocates the whole structure.
~List(){ while (!empty()) pop(); delete rear; }
main(){ List l; l.push(1); l.push(2); }
main(){ List *l = new List(); l->push(1); l->push(2); List *k = l; delete k; }
main(){ printall(listall(100)); }where printall and listall are defined as follow:
List *listall(int n){ List *l = new List(); while (n>0) l->push(n--); return l; } void printall(List *l){ while (!(l->empty())) cout << l->pop(); }