=========================preview======================
(COMP151)exam-soln.pdf
Back to COMP151 Login to download
======================================================
1.
(b), (c), (f)
2.
A: 180 60
B: 90 30
C: 120 30
3.
(a)
f(f(&a));
(b)
int& g(int& x)
{
x++; return x; }
(c)
g(g(a));
4.
The statement "cout << max(1, 2.2) << endl;" causes ambiguity in
determining which overloaded version of max() to use because both
versions require similar type conversions but give different answers.
5.
x, y
6.
A
B
A B D ~B E ~E ~D ~A
file:///F|/doc/paper/COMP/COMP151/exam-soln.txt 15 [2009/5/16 0:47:24]
~C ~B ~A
7. Calls for rg functions: A::rg A::rg B::rg B::rg Calls for vf functions: B::vf B::vf B::vf B::vf Last two function calls: C::vbf B::rbf Now delete c: ~C ~B ~A Now delete ab: ~B ~A Now delete ac: ~C ~B ~A Now delete bc: ~C ~B ~A
8.
(1) The member initialization list in the constructor of Secret is illegal because static data members cannot be initialized via a constructor.
(2) The static data members Secret::x and Secret::y have not been defined outside the class definition of Secret.
9.
(a)
file:///F|/doc/paper/COMP/COMP151/exam-soln.txt 25 [2009/5/16 0:47:24]
int X::j = 10; X::incr_j(); X::incr_j();
(b)
i = 2
j = 3
i = 1
j = 2
(c)
The three missing parts should be changed to:
int A::X::j = 10;
A::X::incr_j();
A::X::incr_j();
In addition, the following two changes should be made:
A::X a(1);
A::X b;
Alternative solution by adding the following statement: using A::X;
10.
template<typename T>
class Matrix
{
public:
Matrix(int height, int width, T value);
private: int _height; int _width; T** _values;
};
template<typename T> Matrix<T>::Matrix(int height, int width, T value) : _height(height), _width(width)
{ _values = new T*[height]; for (int i = 0; i < height; i++) {
_values[i] = new T[width];
for (int j = 0; j < width; j++)
file:///F|/doc/paper/COMP/COMP151/exam-soln.txt 35 [2009/5/16 0:47:24]
_values[i][j] = value; } }
11.
class Complex
{
public:
Complex(double re = 0.0, double im = 0.0) : real(re), imag(im) { }
double get_real() const { return real; }
double get_imag() const { return imag; }
Complex operator~() const { return Complex(real, -imag); }
Complex operator+(const Complex& c) const;
Complex operator-(const Complex& c) const;
Complex operator*(const Complex& c) const;
friend ostream& operator<<(ostream& os, const Complex& c);
private: double real; double imag;
};
Complex Complex::operator+(const Complex& c) const { return Complex(real + c.real, imag + c.imag); }
Complex Complex::operator-(const Complex& c) const { return Complex(real - c.real, imag - c.imag); }
Complex Complex::operator*(const Complex& c) const { return Complex(real * c.real - imag * c.imag, real * c.imag + imag * c.real); }
Complex operator*(const Complex& c, double s) { return Complex(s * c.get_real(), s * c.get_imag()); }
Complex operator*(double s, const Complex& c)
file:///F|/doc/paper/COMP/COMP151/exam-soln.txt 45 [2009