Homework 2, A Rational Class
Short Description:
Implement and test a rational number class.
Goals
When you finish this homework, you should have:- Demonstrated your ability to implement a class with overloaded operators and functions.
- Demonstrated your ability to provide a required interface.
Formal Description
Implement a rational number class.You class must be implemented in RationalT.h and RationalT.cpp. The type should be called RationalT. This class should store integer based rational numbers.
Your class data should conform to the following standards:
- The denominator should never be 0. If a denominator ever becomes 0, the numerator should be set to 0 and the denominator should be set to 1.
- The denominator should never be negative. If the denominator is negative, the sign of the numerator should be changed.
- The rational number should always be reduced. If a rational number is declared or computed in a form that is not reduced, the result should be reduced.
- A no-parameter constructor which creates the value 1/1
- A single parameter constructor which takes a numerator n and creates the value n/1
- A two parameter constructor which takes a numerator n and a denominator d and creates the value n/d, but in reduced form, and corrected according to the standards above.
- Numerator(), a function that returns the numerator of the instance.
- Denominator(), a function that returns the denominator of the instance.
- an overloaded cast to double, see below.
- Overload the +, -, * and / operators. Each returns a RationalT.
- Overload the == operator to return a boolean indicating if two RationalT instances are equal.
- Prototype:
operator double() const;in the header file. - Prototype:
RationalT::operator double() const;in the implementation file. - Note: There is no return type, it is assumed to be double.
void Reduce()
- This MUST be private as it is not your responsibility to provide a reduce function as part of your RationalT class.
- This fixes all of the problems mentioned above after an operation has been performed.
- It should employ
int GCD(int a, int b)described below. - An instance can just call
Reduce()to reduce its value in a member function. - An instance
othercan callother.Reduce()to reduce other's value in a member function.
int GCD(int a, int b);
- Declare this as a function inside of RationalT.cpp.
- It does not need to be declared in RationalT.h, as you are not providing a generic GCD function.
- Implement Euclid's GCD Algorithm, which you will/have probably learn in discrete.
Finally, Implement an overloaded stream insertion operator.
- This is not a member of the class, but should have a function prototype in RationalT.h
- This should not be a friend function, no matter what the AI says.
- This function should:
- Print 0/1 as 0
- Print n/1 as n
- print n/d in reduced form as n/d
- This function should not print spaces, endl, or any other spurious characters.
- Your code must compile with rationalProgram.cpp.
- This program must not be changed.
- This program provides the standard for the interface, but does not test all aspects of the rational number class.
Notes
- You may not use any functions from the algorithm library or elements from the boost library.
- You may not use std::ratio.
- Your code must compile on prog1 with the following flags without warnings or errors.
-
-Wall -Wextra -Wpedantic -Wshadow -Wconversion -std=c++23 -g -O2
-
- Implementation of +,-,/ and * should be for the standard fraction operations:
- 1/4 + 1/8 = 3/8
- 1/4 - 1/8 = 1/8
- 1/4 * 1/8 = 1/32
- 1/4 / 1/8 = 2
- Your code should follow all local coding conventions.
- Your class should properly implement data hiding.
- Your code should be well written, extensible and reusable.
- Please note, the supplied code is to provide an interface test, it will NOT sufficiently test your rational class.
- You should provide a program that completely tests your code.
- All observer member functions should be marked
const. If we have not yet discussed this, please remind me. - If you work with others please include this information as a comment in your RationalT.h file.
- If you use AI in any way, please include how the AI was used and the percentage of the program written by AI in your RationalT.h file.
- If you wish, here is my Makefile.
Required Files
Submit a zip or tar file containing- A makefile that builds your program
- Your source code for your rational number class, including the header and the implementation file.
- A test driver that tests your rational number class.