10 Points Front and Back

Total Page:16

File Type:pdf, Size:1020Kb

10 Points Front and Back

Sample Quiz 2 CS / SE 2630 10 points front and back

// This is file: q2.h #include using namespace std; class TA { public: int a1; TA() { a1 = 3; a2 = 5; a3 = 7; } virtual void DoIt1() {cout << "TA's DoIt1" << endl;} void DoIt2() {cout << "TA's DoIt2" << endl;} protected: int a2; private: int a3; }; class TB : public TA { public: void print() { cout << "a1 is " << a1 << endl; // # 1 cout << "a3 is " << a3 << endl; // # 2 cout << "a2 is " << a2 << endl; // # 3

} virtual void DoIt1() {cout << "TB's DoIt1" << endl;} virtual void DoIt2() {cout << "TB's DoIt2" << endl;}

TB():TA() { a1 = 6; // # 4 a2 = 8; // # 5 a3 = 10; // # 6 } }; (1) 1. In the above file: q2.h, give the number of 2 of the 6 numbered statements that will not compile:

______

(3) 2. Give the output of the following main program that uses the above h file, assuming that the 2 error statements are commented out. In addition, indicate whether statement 7 or 8 is invalid – assume the invalid one (statement 7 or 8) is commented out as well. #include "q2.h" int main() { TA a, *ap; TB b, *bp = &b; a = b; // # 8 b = a; // # 7 ap = new TB(); ap->DoIt1(); ap->DoIt2(); bp->print(); return 0; } (5) 3. Write the pop and push routines to complete the following template for a stack.

#ifndef __TSTACK_H #define __TSTACK_H template class TStack { private: TStackInfo elements[MAX]; int top; public: TStack() : top(0) {} TStackInfo Pop(); void Push ( const TStackInfo & element ); bool IsEmpty() const { return top == 0; } bool IsFull() const { return top == MAX; } };

// write Pop method here

// write Push method here

#endif

(1) 4. Declare an integer stack of 50 elements called intStack and a float stack of 100 elements called floatStack.

Recommended publications