Comp 1672 Homework 5 Due Tuesday, Feb 11, 2003 1. a. False - this would destroy the usefulness of a class! b. False - classes may have utility functions that are for internal use c. True - it's recommended under most circumstances d. False - see page 565 e. False - a constructor may, but a destructor never may 2. a. The second constructor returns an int - constructors never return values b. It's better form to explicitly declare the data members as private The print function should be declared as void The semi-colon is missing at the end of the class declaration c. It says "public;" and it should say "public:" The second constructor returns a bool - constructors never return values Problem 3: #include #include using namespace std; class Two_by_two { public: Two_by_two(); Two_by_two(float, float, float, float); void set(); void print_out(); float det(); private: vector data; }; // default constructor - fill it with all 0s Two_by_two::Two_by_two() { for (int i=0; i < 4; i++) data.push_back(0); } // constructor - allows initialization of 4 values Two_by_two::Two_by_two(float a, float b, float c, float d) { data.push_back(a); data.push_back(b); data.push_back(c); data.push_back(d); } // input the 4 values from user void Two_by_two::set() { float a,b,c,d; data.clear(); // get rid of old values do { cout << "Please enter the 4 numbers: "; cin >> a >> b >> c >> d; } while (!cin); data.push_back(a); data.push_back(b); data.push_back(c); data.push_back(d); } // print out all four elements void Two_by_two::print_out() { vector ::iterator iter=data.begin(); // output in two rows of two cout << *iter++ << " " << *iter++ << endl; cout << *iter++ << " " << *iter++ << endl; } float Two_by_two::det() { return (data[0]*data[3] - data[1]*data[2]); } int main() { Two_by_two matrix1; Two_by_two matrix2(1, 5, 2, 4); Two_by_two matrix3; cout << "Matrix 1: " << endl; matrix1.print_out(); cout << "determinant: " << matrix1.det() << endl; cout << "Matrix 2: " << endl; matrix2.print_out(); cout << "determinant: " << matrix2.det() << endl; cout << "Setting values in Matrix 3: " << endl; matrix3.set(); matrix3.print_out(); cout << "determinant: " << matrix3.det() << endl; }