






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Read the following now.
(a) (10pt) Show the heap as a tree after inserting 99.
(b) (10pt) Starting from the original heap, show the heap as an array after removing the top element, 85.
class X { int a, b; X() {a=10; b=256;} virtual void f () = 0; void g () {a *= 2;} };
class Y : public X { Y() : X() {} // the a and b of Y are initialized // by the constructor of X f () {b /= 2;} };
class Z : public Y { Z() : Y() {} // the a and b of Z are initialized // by the constructor of Y, hence by // the constructor of X f () {b /= 4;} g () {a *= 4;} };
class W : public X { int c; W() : X() {c=27} // the a and b of W are initialized // by the constructor of X f () {b /= 8;} h () {c /= 3;} };
Y y; Z z; W w; X *x_ptr = &z;
(a) (5pt) Name the base class(es) in the above code.
(b) (5pt) Explain abstract class in two sentences or less.
(c) (2pt) Name the abstract class(es) in the above code.
(d) (6pt) Give the values of y.a and y.b after the execution of y.f() and y.g().
(e) (6pt) Give the values of z.a and z.b after the execution of z.f() and z.g().
(f) (6pt) Let us reinitialize z, that is, z.a=10 and z.b=256. Give the values of z.a and z.b after the execution of x ptr->f() and x ptr->g().
(g) (5pt) Circle the C++ statements below that are illegal.
struct StuRecord { string name; // including both first and last names float avg_gpa; }; struct ListNode { StuRecord stu_rec; ListNode* next; }; class Students{ public: add (string name, float avg_gpa); update_gpa (string name, float new_gpa); remove (string name); private: ListNode* head; // a pointer to the first ListNode };
Our goal is to develop an outstanding-students-iterator, called OSI, which enumerates all students with GPA above a given threshold. For example, the following loop will print the names of all GPA-3.75-and-above students in a Students object stu.
for (OSI p=stu.first_os(3.75); p!=stu.end_os(); ++p) cout << (*p).name << ‘‘\n’’;
(a) (8pt) Declare the data members of class OSI. Don’t worry about constructors, destructors, and any other method. class OSI { private: // declare your data members here
pulbic: // constructors/methods here; not required in your answer };
(b) (7pt) Implement the deference operator of class OSI.
StuRecord& OSI::operator* () {
(c) (10pt) Implement the pre-increment operator of class OSI.
OSI OSI::operator++ () {
23
10
6
3
4
4 5
2
8 8
18
24
10
20
(a) (5pt) Show the initial contents of the dist and pred arrays.
(b) (15pt) Show the contents of the dist and pred arrays after the completion of 4 iterations.
23
10
6
3
4
5
2
8
18
24
10
20
(^7 )