Example - Rational Numbers

Specifications: Create the class: rationals with the attributes:

  • top

  • bottom

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:

    • addition

    • subtraction

    • multiplication

    • division

  • the relational operators

    • <

    • <=

    • >

    • >=

    • ==

  • the I/O operators

    • insertion: <<

    • extraction: >>

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.

image from book

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:

image from book

Next type in the class name e.g. istream& and in the following

image from book

Do the same for ostream&. What you should now see on the left should be the following:

image from book

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:

image from book

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:

image from book

Next the Parameter: ab of Type: Top Package: rationals and for Kind the in may be selected as in the following:

image from book

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.

image from book

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:

image from book

Coding: .The following files meet the specifications:

  • rationals.h

  • rationals.cpp

  • testrationals.cpp

Intermediate Business Programming with C++
Intermediate Business Programming with C++
ISBN: 738453099
Year: 2007
Pages: 142

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net