Assignment 6


This assignment introduces the concept of operator overloading, the use of the friend keyword, and the concept of "const correctness".

A number of the form a + ib, where i2 = -1 and a and b are real numbers, is called a complex number. We call a the real part and b the imaginary part of a + ib. Complex numbers can also be represented as ordered pairs (a, b). The addition and multiplication of complex numbers is defined by the following rules:

(a + ib) + (c + id) = (a + c) + i(b + d)

(a + ib) * (c + id) = (ac - bd) + i(ad + bc)

Using the ordered pair notation, these rules are written as:

(a, b) + (c, d) = ((a + c), (b + d))

(a, b) * (c, d) = ((ac - bd), (ad + bc))

C++ has a standard library class that represents complex numbers defined in the header file <complex>. However, in this assignment we will create our own new data type, complex, to process complex numbers. We will overload the stream insertion and stream extraction operators for easy input and output. We will also overload the operators + and * to perform addition and multiplication of complex numbers. If x and y are complex numbers, we can evaluate expressions such as x + y and x * y.

More information on complex numbers can be found here, but the description given above should be sufficient to complete this assignment.

1. Initial Setup


  1. Log in to Unix.

  2. Run the setup script for Assignment 6 by typing:

        setup 6
    

2. Files You Must Write


You will need to write one class for this assignment. A main program to test your class will be provided.

The complex class

The complex class represents a complex number as an ordered pair. Like the other classes we've written this semester, this class should be implemented as two separate files.

The class definition should be placed in a header file called complex.h. Include header guards to prevent it from accidentally being #included more than once in the same source code file.

The complex class should contain the following private data members:

In addition to the data members described above, the class definition should also contain prototypes for the member functions described below.

The implementations of the class member functions should be placed in a separate source code file called complex.cpp. Make sure to #include "complex.h" at the top of this file.

The complex class should have the following member functions (most of which are quite small):

In addition to the member functions described above, you will need to write two standalone functions. These functions are not (and can not be) member functions. You should

  1. Include a friend declaration for each of these functions in the complex class definition.
  2. Put the definitions for these functions in complex.cpp.

3. Files We Give You


The setup script will create the directory Assign6 under your csci241 directory. It will copy a makefile named makefile to the assignment directory.

You will also receive a driver program named main.cpp that contains a main() function that will test your complex class.

4. Output


The correct output for this assignment is shown below (text in red represents input entered by the user):

Testing constructor... done
Testing default constructor... done

Testing stream insertion operator and constructors...
c1 = (23, 34)
c2 = (0, 0)

Testing get methods...
Real part of c4 = 3
Imaginary part of c4 = 4
Real part of c4 = 3
Imaginary part of c4 = 4

Testing set methods...
New value of c2 = (3.7, 2.5)
New value of c2 = (-1.4, 2.5)
New value of c2 = (-1.4, 83)

Testing stream extraction operator...
Enter a complex number in the form (a, b) (2, 7)
New value of c2 = (2, 7)

Testing addition operator...
c3 = (26, 38)
(3, 4) + (23, 34) = (26, 38)
(3, 4) + (3, 4) = (6, 8)

Testing multiplication operator...
c3 = (-67, 194)
(3, 4) * (23, 34) = (-67, 194)
(3, 4) * (3, 4) = (-7, 24)

Testing equality operator...
(23, 34) and (-67, 194) are not equal
(3, 4) and (3, 4) are equal

If you would like a copy of this output to compare against your own program's output using the diff command, it is available on Unix at the pathname /home/turing/t90kjm1/CS241/Output/Spring2021/Assign6/output6.txt.

5. Hints


The driver program is designed to make this assignment easy to develop incrementally. Simply comment out all of the lines of the driver program that call functions that you haven't written yet. You should be able to write, test, and debug function at a time. I would suggest writing the functions in the following order:

  1. constructor
  2. operator<<()
  3. get_real(), get_imaginary(), get_complex()
  4. set_real(), set_imaginary(), set_complex()
  5. operator>>()
  6. operator+()
  7. operator*()
  8. operator==()

Remember that member functions that do not change any data members of the object that called them should be made const so that they may be called by const objects. If not, you will get the syntax error message "error: passing 'const complex' as 'this' argument discards qualifiers" when the member function is called by a const object.