NivaarExam PrepOfficial exam papers ↗

25-Comp-A4 Program Design and Data Structures · December 2019

Question 6 of 9: Object-Oriented Design — A C++ Complex Number Class

Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer)

Notes on this paper

Paper format. 17-Comp-A4 Program Design and Data Structures, December 2019 — 3 hours, closed book, no calculator permitted. Nine questions, each of equal weight (Questions 1, 2, 7 and 8 are split 10+10; Questions 3–6 and 9 are 20 marks each), so 180 marks are printed in total. The cover page directs candidates to answer any six of the nine, and only the first six as they appear in the answer book are marked — so a complete paper is $6\times 20 = 120$ marks, which is the "total mark is out of 120" the paper's Note 6 states. Pseudocode or any high-level language (e.g. C or C++) is accepted, and the examiner's note states explicitly that marking emphasises the operation of the program, not syntactic details. All nine questions are answered below, because the whole set is the more useful revision resource. Answers are given in C or C++ as the question dictates; each is compilable as written, but a clear, correctly reasoned pseudocode answer would earn the same marks.

Reference texts for this subject.

  • Cormen, Leiserson, Rivest & Stein, Introduction to Algorithms, 4th ed. — tree walks (ch. 12), sorting and its complexity (ch. 2, 7), asymptotic analysis (ch. 3).
  • Weiss, Data Structures and Algorithm Analysis in C, 2nd ed. — arrays (ch. 1), linked lists (ch. 3), binary trees (ch. 4), searching and hashing (ch. 5).
  • Deitel & Deitel, C++ How to Program, 10th ed. — class design and operator overloading (ch. 9–11), file streams (ch. 14).
  • Kernighan & Ritchie, The C Programming Language, 2nd ed. — character/file I/O idioms (ch. 1, 7), pointers and structures (ch. 5–6).
  • Stroustrup, The C++ Programming Language, 4th ed. — value semantics, const-correctness and operator overloading (ch. 3, 11).

The Computer Engineering citation list is built around architecture and networking texts (Patterson & Hennessy, Tanenbaum, Mano); this subject is programming and data structures, so the works above are cited instead.

Question 6: Object-Oriented Design — A C++ Complex Number Class (20 marks)

Question text not reproduced: the examination questions are © Engineers and Geoscientists BC. Open the official past paper (linked at the top of this page) to read the question, then follow the worked solution below.

Given. No numeric data; a design requirement for a class that can be declared both with and without initial real/imaginary values, gives read/write access to each part, and supports $+,-,\times$ plus printing.

Find. A Complex class, split into Complex.h (declaration) and Complex.cc (implementation), whose arithmetic operators implement the standard complex number formulas.

Approach. Store the real and imaginary parts as two double members; provide a single constructor whose two default arguments $(0,0)$ make it serve as both the no-argument declaration Complex c; and the initialised one Complex c(3,4); — which is exactly the "with and without initialization" requirement — then accessor/mutator pairs for each part, overload +, - and * as member functions returning a new Complex by value, and overload << as a free function so cout << c reads naturally.

  1. Write Complex.h.
    // Complex.h
    #ifndef COMPLEX_H
    #define COMPLEX_H
    #include <iostream>
    
    class Complex {
    public:
        Complex(double re = 0.0, double im = 0.0);
    
        double getReal() const;
        double getImag() const;
        void   setReal(double re);
        void   setImag(double im);
    
        Complex operator+(const Complex &other) const;
        Complex operator-(const Complex &other) const;
        Complex operator*(const Complex &other) const;
    
        friend std::ostream &operator<<(std::ostream &os, const Complex &c);
    
    private:
        double real, imag;
    };
    
    #endif
  2. Write Complex.cc, implementing the arithmetic formulas directly. $$ (a+bi)+(c+di) = (a{+}c) + (b{+}d)i, \qquad (a+bi)-(c+di) = (a{-}c) + (b{-}d)i $$ $$ (a+bi)\times(c+di) = (ac{-}bd) + (ad{+}bc)i $$
    // Complex.cc
    #include "Complex.h"
    
    Complex::Complex(double re, double im) : real(re), imag(im) {}
    
    double Complex::getReal() const { return real; }
    double Complex::getImag() const { return imag; }
    void   Complex::setReal(double re) { real = re; }
    void   Complex::setImag(double im) { imag = im; }
    
    Complex Complex::operator+(const Complex &o) const
    {
        return Complex(real + o.real, imag + o.imag);
    }
    
    Complex Complex::operator-(const Complex &o) const
    {
        return Complex(real - o.real, imag - o.imag);
    }
    
    Complex Complex::operator*(const Complex &o) const
    {
        return Complex(real * o.real - imag * o.imag,
                        real * o.imag + imag * o.real);
    }
    
    std::ostream &operator<<(std::ostream &os, const Complex &c)
    {
        os << c.real;
        if (c.imag >= 0) os << " + " << c.imag << "i";
        else             os << " - " << -c.imag << "i";
        return os;
    }
  3. Trace the arithmetic on $c_1=3+4i$, $c_2=1-2i$. $$c_1+c_2 = (3{+}1)+(4{+}(-2))i = 4+2i, \qquad c_1-c_2 = (3{-}1)+(4{-}(-2))i = 2+6i$$ $$c_1\times c_2 = (3\cdot1 - 4\cdot(-2)) + (3\cdot(-2) + 4\cdot1)i = 11-2i$$ $$\boxed{c_1+c_2=4+2i,\quad c_1-c_2=2+6i,\quad c_1\times c_2=11-2i}$$
Question 6 — results
Operation$c_1=3+4i,\ c_2=1-2i$Result
$c_1+c_2$$(3{+}1)+(4{-}2)i$$4+2i$
$c_1-c_2$$(3{-}1)+(4{+}2)i$$2+6i$
$c_1\times c_2$$(3\cdot1{-}4\cdot(-2))+(3\cdot(-2){+}4\cdot1)i$$11-2i$