CMSC 202 Spring 2003
Lab Assignment 5

Name:_____________________________________ Checked:__________________________________
SSN:______________________________________ Date:_____________________________________

Purpose

  1. Introduction to operator overloading.

The Assignment

  1. Copy the file /afs/umbc.edu/users/a/n/aniket1/pub/202/labs/lab5/Complex.H and /afs/umbc.edu/users/a/n/aniket1/pub/202/labs/lab5/Complex.C to a convenient directory for this lab.
  2. These files contain the interface and implementation of the Complex class. The Complex class contains members to hold the real and imaginary parts of a complex number, a constructor, and accesors and mutators for the private data.
  3. The header file declares the prototypes of the overloaded operators + and -. These operators are implemented in the file Complex.C
  4. You have to perform the following tasks:
    1. Write the function to overload the binary * operator to allow multiplication of two complex numbers.
    2. Write the function to overload the unary - operator.
    3. Write the function to overload the == operator to test for equality of two complex numbers.
    All these functions prototypes have been declared in the header file.
  5. Copy the file /afs/umbc.edu/users/a/n/aniket1/pub/202/labs/lab5/ComplexTest.C to your directory. This file contains a program that tests the operators you overloaded.
  6. Compile the test program by typing
    g++ -ansi -Wall -o ComplexTest ComplexTest.C Complex.C
  7. Execute the ComplexTest program.
  8. Have the TA verify your work to get credit for this lab.

Note

  1. The product of two complex numbers is defined as:
    (a1 + ib1)(a2 + ib2) = (a1a2 - b1b2) + i(a1b2 + a2b1)
  2. The unary - operator allows us to do something like
        Complex C1(5, 3), C2;
        C2 = -C1;
        
  3. Two complex numbers are equal if both their real and imaginary parts are equal.

Sample Output

%] ./ComplexTest 
C1 = (8 - 2.5i)
C2 = (-4.5 + 0.5i)

C3 = C1 + C2
(3.5 - 2i) = (8 - 2.5i) + (-4.5 + 0.5i)

C4 = C2 * C3
(-14.75 + 10.75i) = (-4.5 + 0.5i) * (3.5 - 2i)

C5 = (8 - 2.5i)
C1 == C5: true
C2 == C5: false
C5 = (8 - 2.5i)
-C5 = (-8 + 2.5i)
After C2 = -C2... C2 = (4.5 - 0.5i)
%]

Feedback

  1. Please tell us how helpful this lab was to you on a scale of 1 (waste of time) to 10 (very helpful):______
  2. If you have any comments you can write them on this page, or email them to aniket1@cs.umbc.edu