Help with Rational Class Lab

I am having trouble with the main part but the class part is fine and was looked over by my teacher. The 'ToDo's are from my teacher so I need to follow those. But I am lost when it comes to the main part... Any help out there is greatly appreciated

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream> 
using namespace std; 

// Add appropriate headers

/*  KEEP THIS COMMENT
* class Rational
*    represents a Rational number. Remember rational means ratio-nal
*    which means there is a numerator and denominator having
*    integer values. Using good ADT techniques, we have made member
*    variable private (also known as instance variables) and made member
*    functions public.
*/

class rational
{
    public: 
	// ToDo: Constructor that takes int numerator and int denominator
        rational(int numerator, int denominator);
	// ToDo: Constructor that takes int numerator
        rational(int numerator);
	// ToDo: Default Constructor
        rational();
	// ToDo: Member function to read a rational in the form: n/d
        void read();
         
	// ToDo: Member function to write a rational as n/d 
        void write();
        
        
	// ToDo: declare an accessor function to get the numerator
        int get_numerator();
	// ToDo: declare an accessor function to get the denominator
        int get_denominator();
	// ToDo: delcare a function called Sum that takes two rational objects
	    void sum (rational rat1, rational rat2);
	    
	    
	// sets the current object to the sum of the given objects using the
	// formula: a/b + c/d = ( a*d + b*c)/(b*d)
        

};
//void rational::input()
// char c;
// cin >> numerator >> c >> denominator; 


//rational3.sum(rational1,rational2);

int main()
{
	// ToDo: declare three rational objects using the default constructor
    rational rat1,rat2,rat3;
	int numerator, denominator;
	char c;
	char answer;

	// Main loop to read in rationals and compute the sum
	do {
		cout << "\nEnter op1 (in the format of p/q): ";
		
		// ToDo: use your input member function to read the first rational
		 read (rat1);

		cout << "\nEnter op2 (in the format of p/q): ";
		// ToDo: use your input member function to read the second rational
		 read (rat2);

		// ToDo: use the third rational to call Sum with first and second as parameters
	     
         sum.rat3(rat1, rat2);

		cout << "\nThe sum of op1 and op2 is: ";
		// ToDo: ouptput the third rational
	      void sum rat1,rat3; 

		cout << "\nTry again (Y/N)?";
		cin >> answer;

	} while (answer == 'y' || answer == 'Y');

	// ToDo: test getters 
	cout << "\nC's numerator is: " ;
	cin >> numerator >> c >> denominator;

	cout << "\nC's denominator is: ";

	return 0;
}

// ToDO: Implement your class member functions below.
   
Liners 64, 68 and 72: The way you call a method is object.method(). For example, at line 64, you want to call the read() method on the rat1 object. The code for that is rat1.read(). Make that change and change lines 68 & 72 accordingly.

Line 76: To output the 3rd rational number (rat3), just call it's write() method. Use what I said above to figure out how to do this.

Line 85: You get the numerator by calling a rational's get_numerator() function. For example, to print the of rat3, you'd do cout << rat3.get_numerator();

The comments at the top say "we have made member variable private" but I don't see the private member variables. You should add those.

Right now all of those methods must be written just to get this to compile. To help with testing, here are stubs for most methods. These compile and print "blah blah is not implemented." By implementing these one at a time, you'll be able to get the program working a little at a time.

Add this to the program.

Implement the default constructor.

Get the code to compile and then replace the "not yet implemented" message in each method with an actual implementation.

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
rational::rational(int numerator)
{
    cout << "rational(num) not yet implemented\n";
}
rational::rational(int n, int d)
{
    cout << "rational(num, denom) not yet implemented\n";
}

void rational::read()
{
    cout << "read not yet implemented\n";
}

void rational::write()
{
    cout << "write not yet implemented\n";
}

void rational::sum(rational rat1, rational rat2)
{
    cout << "sum not yet implemented\n";
}

int rational::get_numerator()
{
    cout << "get_numerator not yet implemented\n";
    return 0;
}

int rational::get_denominator()
{
    cout << "get_denominator not yet implemented\n";
    return 1;
}


Topic archived. No new replies allowed.