/*
CS371p: Quiz #19 (5 pts)
*/
/* -----------------------------------------------------------------------
1. What four methods constitute the orthodox canonical class form?
[Sec. 5.6.1, Pg. 115]
(2 pts)
default constructor
copy constructor
copy assignment operator
destructor
*/
/* -----------------------------------------------------------------------
2. Define class Complex such that the program below works.
The function conjugate() MUST call the method conjugate().
(2 pts)
*/
#include <cassert> // assert
template <typename T>
class Complex {
private:
T _r;
T _i;
public:
Complex (const T& r = T(), const T& i = T()) :
_r (r),
_i (i)
{}
Complex& conjugate () {
_i = -_i;
return *this;}
const T& real () const {
return _r;}
const T& imag () const {
return _i;}};
template <typename T>
Complex<T> conjugate (Complex<T> other) {
return other.conjugate();}
int main () {
Complex<int> x(2, 3);
assert(x.real() == 2);
assert(x.imag() == 3);
Complex<int>& y = x.conjugate();
assert(&y == &x);
assert(x.real() == 2);
assert(x.imag() == -3);
Complex<int> z = conjugate(x);
assert(x.real() == 2);
assert(x.imag() == -3);
assert(z.real() == 2);
assert(z.imag() == 3);
return 0;}
syntax highlighted by Code2HTML, v. 0.9.1