/*************************************************/
// R. A. Hillyard
// rational01.cpp
// November 2001
//
// class to represent rational numbers i.e. fractions
/*************************************************/
#include <iostream>
using namespace std; //introduces namespace std
class Rational
{
public :
void input(); //get input values from keyboard for a rational number
void output(); //output the value of a rational number
void set(int t, int b); //set the value for a rational
int getTop(); //returns the numerator (top)
int getBottom(); //returns the denominator (bottom)
private:
int top; //holds numerator
int bottom; //holds denominator
};
/********************************************************/
void Rational::input()
{
cout << "enter top and bottom > ";
cin >> top >> bottom;
}
/********************************************************/
void Rational::output()
{
cout << top << "/" << bottom;
}
/********************************************************/
void Rational::set(int t, int b)
{
top = t;
bottom = b;
}
/********************************************************/
int Rational::getTop()
{ return top; }
/********************************************************/
int Rational::getBottom()
{ return bottom; }
/********************************************************/
/********************************************************/
int main( void )
{
Rational f1, f2, f3; //declare three rational numbers
f1.set(1,2); //call the set member function
f2.set(33,52);
f3.input(); //get values from keyboard
cout << "Your Fractions are: ";
f1.output(); cout << " ";
f2.output(); cout << " ";
f3.output(); cout << "\n";
cout << "tops equal: " << f1.getTop() + f2.getTop() + f3.getTop() << endl;
cout << "bottoms equal: " << f1.getBottom() + f2.getBottom() + f3.getBottom() << endl;
return 0;
}
/********************Program Output*********************
enter top and bottom > 17 19
Your Fractions are: 1/2 33/52 17/19
tops equal: 51
bottoms equal: 73
*/