Homework 5, Class Production
Short Description:
Implement and test two classes.
This assignment is worth 100 points.
Goals
When you finish this homework, you should:
- Have implemented an ADT in a C++ class.
- Have designed a simple C++ class from an ADT.
Formal Description
For this program you are to implement a complex number class and a rational number class. In addition to these classes, you should build a driver program to test your classes.
If at any time you have problems with the mathematics in this exercise, ASK FOR HELP.
Complex Number Class
Implement the complex number class we have been discussing in lecture for the
past few weeks. You should employ a specification (.h) file as well as an implementation file.
Your complex number class should operate on doubles. It should contain a
realPart (R) and a imaginaryPart (I).
Your complex number class should support the following operations.
- Instantiation with 0 parameters
- Instantiation with 1 parameter (a)
- Instantiation with 2 parameters (a,b)
- SetReal(a), R=a.
- SetImag(a), C= a.
- Set(a,b), R= a, C= b
- RealPart(), returns a double containing R.
- ImagPart(), returns a double containing C.
- Add(a), R+= a.
- Add(a,b), R+=a, C+= B .
- Overload the following operators
- Mag(), returns a double containing sqrt(R*R+C*C)
- Power(int a), returns a complex, this number raised to the ath power. Assume a is positive.
- Overload the stream insertion operator.
- If R=0, and C=0, output 0
- If R=0, output Ci
- If C=0, output R
- Otherwise output R+Ci
The header file must be called complex.h, the object file must be called
complex.o. The class must be called complex. All functions above must be defined, and must be named as stated. As part of the grading process, I will link your class against my code. If this is unsuccessful due to a failure follow the specification, points will be deducted.
You should develop a driver for your class which tests all aspects of the class.
Rational Number Class
Design and implement a rational number class. A rational number is a fraction. Your class should store any fraction as a numerator and denominator. If not specified, the denominator is 1. If not specified the numerator is 0. You should always store the rational number in reduced form.
The GCD function from the test might be helpful for this task, it should be a private member function of your class. (GCD(a,b) = a if b = 0, GCD(b,a%b) otherwise). If this is causing you problems, ASK FOR HELP.
You should provide a set of constructors.
You should provide a set of observers, including the ability to check the numerator, denominator, and convert the real to a double.
You should never allow the denominator to become 0.
You should provide operators to perform +,-,*,/
You should provide a < operator.
You should provide a stream insertion operator.
You must provide a specification (.h) and implementation file. You should also provide a driver that exercises your class. Finally you should provide documentation on your interface.
Submission
This program is due November 24 at class time. Email a tar file containing all required files to dbennett@edinboro.edu by this time.