CMSC 202 Spring 2003
Lab Assignment 5
Name:_____________________________________ Checked:__________________________________
SSN:______________________________________ Date:_____________________________________
Purpose
- Introduction to operator overloading.
The Assignment
- 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.
- 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.
- The header file declares the prototypes of the overloaded operators
+ and -. These operators are implemented in the file
Complex.C
- You have to perform the following tasks:
- Write the function to overload the binary * operator
to allow multiplication of two complex numbers.
- Write the function to overload the unary - operator.
- 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.
- 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.
- Compile the test program by typing
g++ -ansi -Wall -o ComplexTest ComplexTest.C Complex.C
- Execute the ComplexTest program.
- Have the TA verify your work to get credit for this lab.
Note
- The product of two complex numbers is defined as:
(a1 + ib1)(a2 +
ib2) = (a1a2 -
b1b2) + i(a1b2 +
a2b1)
- The unary - operator allows us to do something like
Complex C1(5, 3), C2;
C2 = -C1;
- 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
- Please tell us how helpful this lab was to you on a scale of 1 (waste of
time) to 10 (very helpful):______
- If you have any comments you can write them on this page, or email
them to aniket1@cs.umbc.edu