Class help

frac.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//--------------- FRAC.CPP ---------------
// The class definition for fractions.
//

#include <iostream>
#include "frac.h"

using namespace 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//--------------- 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>
using namespace 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!
Last edited on
Topic archived. No new replies allowed.