CS 391 Review 1.) Write the body of init, which initializes D's members to the parameter values. class B { // base class protected: int num1; int num2; }; class D : public B { // derived class int num3; public: void init(int, int, int); }; void D::init(int n1, int n2, int n3) { } 2.) Given the following slice of code: class stack { private: int count; int data[100]; public: void init(void); void push(const int numb); int pop(void); }; int stacks_are_equal(const stack &stack1, const stack &stack2) { if (stack1.count != stack2.count) return(0); for (int i = 0; i < stack1.count; ++i ) { if (stack1.data[i] != stack2.data[i]) return (0); } return (1); } Assume the functions init, push and pop are defined. a.) Explain the error(s), if any. b.) If there is an error(s), correct it (them). 3.) What is the output? class BC( ) public: BC ( ) { cout << "BC constructor" << endl; } ~BC ( ) { cout << "BC destructor" << endl; } }; class DC1( ) : public BC { public: DC1 ( ) : BC ( ) {cout << "DC1 constructor" << endl; } ~DC1 ( ) { cout << "DC1 destructor" << endl; } }; class DC2( ) : public DC1 { public: DC2 ( ) : DC1 ( ) {cout << "DC2 constructor" << endl; } ~DC2 ( ) { cout << "DC2 destructor" << endl; } }; int main ( ) { BC b; DC1 d1; DC2 d2; return 0; } Recall the rules for constructors: * If derived class DC has constructors but base class BC has no constructors, then the appropriate DC constructor executes automatically whenever a DC object is created. * If DC has no constructors and BC has constructors, then BC must have a default constructor so that BC's default constructor can execute automatically whenever a DC object is created. * If DC has constructors and BC has a default constructor, then BC's default constructor executes automatically whenever a DC object is created, unless the appropriate DC constructor explicitly invokes some other BC constructor. * If DC and BC both have constructors but BC has no default constructor, then each DC constructor must explicitly invoke a BC constructor, which then executes when a DC object is created. Obviously, if a derived class object is created some base class constructor should execute. After all, a derived class constructor may depend upon actions from a base class constructor. For example, the derived class may depend on the base class constructor to execute some data member initialization. Another point: a derived class object is a specialization of a base class object, recall base class vehicle and derived class car, which means that the body of the base class constructor should execute first when a derived object is created. 4.) Referring to the Complex Number class program, give code segments in which Complex::operator* (multiplication) is used with operator syntax and method syntax. Note: Recall the formula for multiplying two complex numbers (using FOIL): (a + ib)(c + id) = ac + iad + ibc - bd 5.) You are to overload an operator to convert temperature in degrees F to degrees C. Using one of the conversion formulas: C = (5/9)*(F - 32) and F = (9/5) * C + 32 and an operator, overload this operator to convert from degrees F to degrees C. 6.) Explain the error (if one exists): class A { protected: float f1, f2; public: int x; }; class B : public A { public: void pf1( ) {cout << f1 << endl;} }; void main( ) { B b1; b1.f1 = 3.14; b1.pf1( ); b1.x = ( int ) b1.f1; .... } 7.) In the code segment: class B { // base class public: int a; protected: float b; private: long int c; float d; }; class D : private B { // derived class public: int y; double x; }; Set up a Member Access Table showing the member, Access status in D and how the member was obtained. 8.) Using the Binary Search Algorithm, what is the minimum number of passes to search for a number in an array of 22 numbers? Explain your answer.