class Fraction
{
public:
Fraction(); // Set numerator = 0, denominator = 1.
Fraction(int n, int d=1); // constructor with parameters
// standard input/output routines
void Input(); // input a fraction from keyboard.
void Show(); // Display a fraction on screen
// accessors
int GetNumerator();
int GetDenominator();
// mutator
void SetValue(int n, int d); // set the fraction's value through parameters
double Evaluate(); // Return the decimal value of a fraction
double Evaluate2();
private:
int numerator; // no restrictions
int denominator; // Invariant: denominator != 0
};
//--------------- FRAC.CPP ---------------
// The class definition for fractions.
//
#include <iostream>
#include "frac.h"
usingnamespace std;
Fraction::Fraction()
// Default constructor. Initializes fraction to 0/1
{
numerator = 0;
denominator = 1;
}
Fraction::Fraction(int n, int d)
// initializes fraction to n/d
// what kind of error checking should be added?
{
numerator = n;
denominator = d;
}
void Fraction::Input()
// Get a fraction from standard input, in the form "numerator/denominator."
// what kind of error checking should be added?
{
char divSign; // used to consume the '/' character during input
cin >> numerator >> divSign >> denominator;
}
void Fraction::Show()
// Display a fraction, in the form "numerator/denominator."
{
cout << numerator << '/' << denominator;
}
int Fraction::GetNumerator()
{
return numerator;
}
int Fraction::GetDenominator()
{
return denominator;
}
void Fraction::SetValue(int n, int d)
// what sort of error checking should be added?
{
numerator = n;
denominator = d;
}
double Fraction::Evaluate()
// Calculates and returns the decimal value of a fraction
{
double n = numerator; // convert numerator to double
double d = denominator; // convert denominator to double
return (n / d); // compute and return float representation
}
double Fraction::Evaluate2()
{
char final;
}
//--------------- MAIN.CPP ---------------
// Driver routine to test SOME of the functions of the Fraction class
#include <iostream> // for cout
#include "frac.h" // for Fraction declarations
#include <cmath>
usingnamespace std;
int main()
{
// Try all three possible fraction constructors
// and the input/output routines.
// These declarations use the default constructor
Fraction f1, f2;
// These declarations use the constructor with parameters
Fraction f3(3,4), f4(6);
// Use the objects
cout << "\n The fraction f1 is ";
f1.Show();
cout << "\n The fraction f2 is ";
f2.Show();
cout << "\n The fraction f3 is ";
f3.Show();
cout << "\n The fraction f4 is ";
f4.Show();
cout << "\n Now enter first fraction: ";
f1.Input();
cout << "\nYou entered ";
f1.Show();
cout << "\n Now enter second fraction: ";
f2.Input();
cout << "\nYou entered ";
f2.Show();
// Finally, find the floating-point value of f1 and f2
cout << "\n The value of fraction 1 is " << f1.Evaluate() << '\n';
cout << "\n The value of fraction 2 is " << f2.Evaluate() << '\n';
cout << "Goodbye!\n";
}
I'm trying to figure out using the class function double evaluate2(). How to add the final doubles when the user inputs them from the results of f1.Evaluate() and f2.Evaluate Thanks!