Specifications: Create the class: rationals with the attributes:
and has for methods:
the default constructor that initializes bottom to 1
a get and a set for each attribute
and that overloads each of the following operators:
the binary arithmetic operations with the result in reduced form:
the relational operators
the I/O operators
Create a program that inputs two objects of rationals and then tests whether each of the methods of the class works properly.
Design: A UML chart for the class.
f Creating the UML chart for rationals is similar to that for other classes except with respect to overloading the insertion and the extraction operators require the "data types": istream& and ostream& for output and for first argument. (These are not really system data types and that is the way to achieve the outcome desired when using Visio.) These data types are not listed in Visio when selecting the arguments and the output for these functions. Therefore what must be done is to create these data types as you are creating the UML chart for the class. To do this, click Top Package in the Model Explorer.
Next click the right mouse button and select New and then Class. When the dialog appears, enter the name of the respective classes one at a time as in the following:
Next type in the class name e.g. istream& and in the following
Do the same for ostream&. What you should now see on the left should be the following:
Then as you create your class like the rationals in this example and they contain as a friend function the insertion operator, you would use the name: friend operator << for the name of the Operation and select the return type as: Top Package: ostream& as in the following graphic:
For the Parameters you can first select: stream as the Parameter name and the Type as Top Package:: ostream& and the Kind as inout as in the following graphic:
Next the Parameter: ab of Type: Top Package: rationals and for Kind the in may be selected as in the following:
After the insertion operator is added to the UML for the class rationals, the extraction operator may be added in a similar fashion. The only major difference is that the second Parameter is &ab and the Kind is inout.
When all of the methods and the two friend functions have been added to the UML it looks like the following with the insertion and the extraction operators:
Coding: .The following files meet the specifications: