2036 ECE2036_Lab5.htm


2036                                                                                                                   Spring 2015

5: Complex, Matrix, RealMatrix, and ComplexMatrix classes

March 14, 2015                                                      Section
B: Due March 30, 2015

Section A: Due
March 31, 2015

In this lab, you will use C++ to perform
calculations using complex numbers and complex matrices.  You must write classes containing overloaded
operators to achieve the following:

·       a
which stores the real and imaginary parts of a complex number as
private data members, whose overloaded operators can

input and output complex numbers

add a real (double)
number to a complex number, and vice versa

add two complex numbers

multiply a real (double)
number by a complex number, and vice versa

multiply two complex numbers

·       a
RealMatrix class which stores the
real (
matrix elements of a real matrix and a
ComplexMatrix class
which stores the
Complex matrix elements of a
complex matrix, whose overloaded operators and functions can

input and output real and complex

multiply a real matrix by a complex
matrix, and vice versa

multiply two complex matrices

We could store a real matrix as a complex matrix
with zero imaginary parts, but that would waste memory.  It is more efficient to store only the real
matrix elements of real matrices. 
However, the
RealMatrix and
ComplexMatrix objects
are both matrices, and the two classes have some identical data members and
functions.  Therefore, you will be coding
four classes with overloaded operators: a
class to store and manipulate a complex number, an abstract
class, a
RealMatrix class which
inherits from
Matrix and stores purely real matrices, and a ComplexMatrix
class which inherits from
Matrix and stores complex
matrices. In addition to inheritance, you must also use composition and
polymorphism; i.e. the
class must include a private data member which is a 2d array or vector of
objects, and a
Matrix reference or pointer
must be compatible with both
RealMatrix and
ComplexMatrix objects
  You must also write the standalone function inputMatrix, which
reads and interprets the user inputted matrix as described in more detail

The code you write should be contained in at least
seven files, named as follows: Complex.h, RealMatrix.h, and ComplexMatrix.h,
which will contain the class definitions; and Complex.cpp, Matrix.cpp,
RealMatrix.cpp, and ComplexMatrix.cpp, which will contain the function
implementations.  You may choose to place
inputMatrix in its own
separate file.  I have provided a Matrix.h file containing a
class definition, and a main.cpp file which is designed to test the
functionality of your classes.  You can
download these files here:



You can modify Matrix.h as
desired, it is only a ‘suggestion’.  However, for purposes of the check-off you
must use an unmodified copy of main.cpp. 
Feel free to modify main.cpp as needed while coding and testing your
classes.  When possible, you should write
and test a single function at a time, so I recommend commenting out most of
main.cpp and uncommenting only the parts that utilize the class components that
you have already written.  Alternatively,
you could write your own main.cpp to test your classes.  During check-off, the TA will compile the
combined program (my main.cpp and plus the eight class files) and test that it
provides the correct output in response to values they input.  Sample output is provided below; obviously
the TA or grader will use different inputs when testing your code.

and output

For full credit, you must write your input functions
(the overloaded >> operator in the case of
class, the
function in the case of the matrix classes) to be able to accept input similar
to Matlab.  A
complex number should be entered by the user in the form x + i y or x – i y, insensitive to extra
spaces.  A complex or real matrix should be
entered by the user inside square brackets, with rows separated by semicolons,
in the form [ x1 ±  i y1    x2  ±  i y2    x3  ±  i y3 ;  x4  ±  i y4     x5  ±  i y5     x6  ±  i y6   ] ,
which would create a 2 x 3 matrix of complex numbers.  This should also be insensitive to extra
spaces, and if the user wants to enter a purely real number they should be able
to entirely omit the imaginary part.  If
the user enters a matrix whose elements are all entirely real, your code must
automatically store this as a
RealMatrix object;
otherwise matrices should be stored as
ComplexMatrix objects.  However, you do NOT need to perform input checking to ensure the user has entered
proper data; we will make the (foolish) assumption that the user knows what
they’re doing.  The only input checking
that must be done is ensuring that the number of columns of the first matrix
entered equals the number of rows of the second matrix entered so that they can
be multiplied, but this has already been written for you in main.cpp.  

The complex and complex matrix outputs must be neat
and readable.


Please demonstrate your code for the TA or grader by
the end of office hours on the due date. 
Bring your checkoff sheet with your name
already written at the top.  If there are
students waiting to checkoff in Klaus 1446 at the end
of office hours on the due date, the grader or TA will write down the names of
the waiting students on a list.  Those
students will then upload their completed code to t-square,
and they will have three days afterwards to visit office hours, download their
(unmodified) code, and demonstrate it for the grader or TA.


output from completed code (user input indicated in red)

Initial variable values:

c1 = 1 + i5, c2 = 0 + i0


Please enter complex number c3, in the format X + iY, or X – iY: 4.2 + i 0.25

You entered c3 = 4.2 + i0.25

Please enter a double precision number d1: 7.1

d1 + c3 = 11.3 + i0.25

c3 + d1 = 11.3 + i0.25


d1 * c3 = 29.82 + i1.775

c3 * d1 = 29.82 + i1.775


Please enter another complex number c4, in the format X + iY, or X – iY: -3.7 – i 22.1

c3 + c4 = 0.5 – i21.85

c3 * c4 = -10.015 – i93.745


Please enter the first matrix to be multiplied:

-2 ; 2 + i
8  -1.1 + i 2;
56.1 – i 13.3 9 ]

The matrix you entered was:

      0.55 + i0        -2 + i0

         2 + i8      -1.1 + i2

      56.1 – i13.3      9 + i0

Please enter the second matrix to be multiplied:

+ i6 
3.5 90 –i 10; 32 33 – i0.01 10 ]

The matrix you entered was:

      77.1 + i6       3.5 + i0        90 – i10

        32 + i0        33 – i0.01     10 + i0

The product of the two matrices is:

   -21.595 + i3.3      -64.075 + i0.02    29.5 – i5.5

        71 + i692.8    -29.28 + i94.011    249 + i720

   4693.11 – i688.83   493.35 – i46.64     5006 – i1758

The memory allocated for the matrix has been deleted.

The memory allocated for the matrix has been deleted.

The memory allocated for the matrix has been deleted.