/*************************************************/
// R. A. Hillyard
// rational02.cpp
// November 2001
//
// class with constructors
/*************************************************/
#include <iostream>
using namespace std; //introduces namespace std
class Rational
{
public :
//constructors
Rational();
Rational(int top);
Rational(int top, int bottom);
void input();
void output();
int getTop();
int getBottom();
private:
int top;
int bottom;
};
/********************************************************/
Rational::Rational()
{
cout << "In default Constructor\n";
top = 0;
bottom =1;
}
/********************************************************/
Rational::Rational(int t)
{
cout << "In 1 arg Constructor\n";
top = t;
bottom = 1;
}
/********************************************************/
Rational::Rational(int t, int b)
{
cout << "In 2 arg Constructor\n";
top = t;
bottom = b;
}
/********************************************************/
void Rational::input()
{
cout << "enter top and bottom > ";
cin >> top >> bottom;
}
/********************************************************/
void Rational::output()
{ cout << top << "/" << bottom; }
/********************************************************/
int Rational::getTop()
{ return top; }
/********************************************************/
int Rational::getBottom()
{ return bottom; }
/********************************************************/
/********************************************************/
int main( void )
{
Rational f1(2,6);
Rational f2(1,3);
Rational f3;
Rational f4(7);
f3 = Rational(17,19);
cout << "Your Fractions are: ";
f1.output(); cout << " ";
f2.output(); cout << " ";
f3.output(); cout << " ";
f4.output(); cout << endl;
cout << "tops equal: " << f1.getTop() + f2.getTop() + f3.getTop() << endl;
cout << "bottoms equal: " << f1.getBottom() + f2.getBottom() + f3.getBottom() << endl;
return 0;
}
/********************Program Output*********************
In 2 arg Constructor
In 2 arg Constructor
In default Constructor
In 1 arg Constructor
In 2 arg Constructor
Your Fractions are: 2/6 1/3 17/19 7/1
tops equal: 20
bottoms equal: 28
*/