First Class (Rational Numbers

I have only been programming for a about 5 months, 2 months in C++ and this is my first attempt at a class and I'm probably just running into some serious newbie errors but the web tutorials are very confusing.

Heres My header file

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
#ifndef RATIONAL_
#define RATIONAL_

using namespace std;

#include <iostream>

class Rational
{
  public:

    // Construct rational from numerator and denominator
    //
    Rational( int = 0, int = 1 );

    // Construct rational by copying existing rational
    //
    Rational( const Rational& );

    // Assign into rational by copying existing rational
    //
    Rational& operator=( const Rational& );

    //Find Greatest Common Divisor
    int GCD();

    //Simplify the rational number to its smallest form
    void Simplify();

    // Return true if rational is valid (non-zero denominator)
    //
    bool IsValid() const;

    // Return value of numerator
    //
    int Numerator() const;

    // Return value of denominator
    //
    int Denominator() const;

    // Input/Output operations
    //
    friend istream& operator>>( istream&, Rational& );
    friend ostream& operator<<( ostream&, const Rational& );

  private:

    // Insert your declarations here
    int Numerator_;
    int Denominator_;
};
// Comparative operations
//
bool operator==( const Rational&, const Rational& );
bool operator!=( const Rational&, const Rational& );
bool operator< ( const Rational&, const Rational& );
bool operator<=( const Rational&, const Rational& );
bool operator> ( const Rational&, const Rational& );
bool operator>=( const Rational&, const Rational& );

// Arithmetic operations
//
Rational operator+( const Rational&, const Rational& );
Rational operator-( const Rational&, const Rational& );
Rational operator*( const Rational&, const Rational& );
Rational operator/( const Rational&, const Rational& );

#endif 






I keep fixing the first error i recieve then another 10 pop up... My code will be on the next post, and if anyone has any ideas on overloading the operator>> that would be helpful im lost on that one...
Last edited on
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*-----------------------------------------------------------------------------
   Name:  constructor for class "Rational"

   Purpose:  Initialize the contents of a rational number
   Receive:  The numerator and denominator or rational number
-----------------------------------------------------------------------------*/

Rational::Rational( int Num, int Den )
{
   Numerator_ = Num;

  if (Den <= 0)
  {
    Denominator_ = 1;
  }
  else
  {
    Denominator_ = Den;
  }
}

/*-----------------------------------------------------------------------------
   Name:  copy constructor for class "Rational"

   Purpose:  Initialize the value and scale inside a Rational object
   Recieve:   The rational to be copied
-----------------------------------------------------------------------------*/

Rational::Rational( const Rational& C )
{
  Numerator_ = C.Numerator_;
  Denominator_ = C.Denominator_;
}

/*-----------------------------------------------------------------------------
   Name:  assignment operator for class "Rational"

   Purpose:  Copy the contents of one rational to another
        Recieve:         The rational which is to be copied
   Return:   The rational number
-----------------------------------------------------------------------------*/

Rational& Rational::operator=(const Rational& C )
{
        Numerator_ = C.Numerator_;
        Denominator_ = C.Denominator_;

        return *this;
}
 
/*-----------------------------------------------------------------------------
   Name: GCD 
 
   Purpose:  Find the greatest common divisor of a rational number
   Return:   GCD
-----------------------------------------------------------------------------*/
 
int Rational::GCD()
{
        int A = Numerator_;
        int B = Denominator_;

        int Temp;

        while(B)
        {
                Temp = B;
                B = A%B;
                A = Temp;
        }

        return A;
}

/*-----------------------------------------------------------------------------
   Name:        Simplify
 
   Purpose:  Simplify a rational number
-----------------------------------------------------------------------------*/
 
void Rational::Simplify()
{
        int GCDNumber = GCD();

        if(GCDNumber != 0)
        {
                Numerator_ = Numerator_/GCDNumber;
                Denominator_ = Denominator_/GCDNumber;
        }
}

/*-----------------------------------------------------------------------------
   Name:  IsValid
 
   Purpose:  Test whether or not the rational is valid
   Return:   The Boolean value for the result of the test
-----------------------------------------------------------------------------*/
 
bool Rational::IsValid() const
{
  return ( Denominator_ != 0 ); 
}

/*-----------------------------------------------------------------------------
   Name:  Numerator
 
   Purpose:  extract numerator from rational number
        Return:  numerator
-----------------------------------------------------------------------------*/

int Rational::Numerator() const
{
        return Numerator_;
}

/*-----------------------------------------------------------------------------
   Name:  Denominator
 
   Purpose:  extract denominator from rational number
        Return:          denominator
-----------------------------------------------------------------------------*/

int Rational::Denominator() const
{
        return Denominator_;
}

/*-----------------------------------------------------------------------------
   Name:  operator==
 
   Purpose:  compare 2 rationals for equality
        Recieve:         the 2 rationals
        Return:  boolean result of the comparison
-----------------------------------------------------------------------------*/

bool operator==( const Rational& A, const Rational& B ) 
{
        A.Simplify();
        B.Simplify();

        return(A.Numerator() == B.Numerator() and A.Denominator()== B.Denominator());
}
 

/*-----------------------------------------------------------------------------
   Name:  operator!=
 
   Purpose:  compare 2 rationals for inequality
        Recieve:         the 2 rationals
        Return:  boolean result of the comparison
-----------------------------------------------------------------------------*/

bool operator!=( const Rational& A, const Rational& B ) 
{
        A.Simplify();
        B.Simplify();

        return(A.Numerator() != B.Numerator() and A.Denominator()!= B.Denominator());
}

/*-----------------------------------------------------------------------------
   Name:  operator<
 
   Purpose:  tell whether A is less than B
        Recieve:         the 2 rationals A and B
        Return:  boolean result of the comparison
-----------------------------------------------------------------------------*/

bool operator<( const Rational& A, const Rational& B ) 
{
        A.Simplify();
        B.Simplify();

        return(A.Numerator()*B.Denominator())<(A.Denominator()*B.Numerator());
}

/*-----------------------------------------------------------------------------
   Name:  operator<=
 
   Purpose:  tell whether A is less than or equal to B
        Recieve:         the 2 rationals A and B
        Return:  boolean result of the comparison
-----------------------------------------------------------------------------*/

bool operator<=( const Rational& A, const Rational& B ) 
{
        A.Simplify();
        B.Simplify();

        return(A.Numerator()*B.Denominator())<=(A.Denominator()*B.Numerator());
}


/*-----------------------------------------------------------------------------
   Name:  operator>
 
   Purpose:  tell whether A is greater than B
        Recieve:         the 2 rationals A and B
        Return:  boolean result of the comparison
-----------------------------------------------------------------------------*/

bool operator>( const Rational& A, const Rational& B ) 
{
        A.Simplify();
        B.Simplify();

        return(A.Numerator()*B.Denominator())>(A.Denominator()*B.Numerator());
}

/*-----------------------------------------------------------------------------
   Name:  operator>=
 
   Purpose:  tell whether A is greater than or equal to B
        Recieve:         the 2 rationals A and B
        Return:  boolean result of the comparison
-----------------------------------------------------------------------------*/

bool operator>=( const Rational& A, const Rational& B ) 
{
        A.Simplify();
        B.Simplify();

        return(A.Numerator()*B.Denominator())>=(A.Denominator()*B.Numerator());
}

/*-----------------------------------------------------------------------------
   Name:  operator+
 
   Purpose:  Add two rational numbers
        Recieve:         The two rationals to be added
        Return:  The rational result of adding the two rationals
-----------------------------------------------------------------------------*/

Rational operator+( const Rational& A, const Rational B ) 
{
        int Num;
        int Den;

        Num = A.Numerator()*B.Denominator()+A.Denominator()*B.Numerator();
        Den = A.Denominator()*B.Denominator();

        Rational C(Num, Den);
        return C.Simplify();
}

/*-----------------------------------------------------------------------------
   Name:  operator*
 
   Purpose:  Multiply two rational numbers
        Recieve:         The two rationals to be multiplied
        Return:  The rational result of multiplying the two rationals
-----------------------------------------------------------------------------*/

Rational operator*( const Rational& A, const Rational B ) 
{
        int Num;
        int Den;

        Num = A.Numerator()*B.Numerator();
        Den = A.Denominator()*B.Denominator();

        Rational C(Num, Den);
        return C.Simplify();
}
What errors do you get?
proj06.driver.cpp: In function 'int main()':
proj06.driver.cpp:38: warning: the address of 'Rational G()', will always evaluate as 'true'
proj06.rational.cpp: In function 'bool operator==(const Rational&, const Rational&)':
proj06.rational.cpp:150: error: passing 'const Rational' as 'this' argument of 'void Rational::Simplify()' discards qualifiers
proj06.rational.cpp:151: error: passing 'const Rational' as 'this' argument of 'void Rational::Simplify()' discards qualifiers
proj06.rational.cpp: In function 'bool operator!=(const Rational&, const Rational&)':
proj06.rational.cpp:167: error: passing 'const Rational' as 'this' argument of 'void Rational::Simplify()' discards qualifiers
proj06.rational.cpp:168: error: passing 'const Rational' as 'this' argument of 'void Rational::Simplify()' discards qualifiers
proj06.rational.cpp: In function 'bool operator<(const Rational&, const Rational&)':
proj06.rational.cpp:183: error: passing 'const Rational' as 'this' argument of 'void Rational::Simplify()' discards qualifiers
proj06.rational.cpp:184: error: passing 'const Rational' as 'this' argument of 'void Rational::Simplify()' discards qualifiers
proj06.rational.cpp: In function 'bool operator<=(const Rational&, const Rational&)':
proj06.rational.cpp:199: error: passing 'const Rational' as 'this' argument of 'void Rational::Simplify()' discards qualifiers
proj06.rational.cpp:200: error: passing 'const Rational' as 'this' argument of 'void Rational::Simplify()' discards qualifiers
proj06.rational.cpp: In function 'bool operator>(const Rational&, const Rational&)':
proj06.rational.cpp:216: error: passing 'const Rational' as 'this' argument of 'void Rational::Simplify()' discards qualifiers
proj06.rational.cpp:217: error: passing 'const Rational' as 'this' argument of 'void Rational::Simplify()' discards qualifiers
proj06.rational.cpp: In function 'bool operator>=(const Rational&, const Rational&)':
proj06.rational.cpp:232: error: passing 'const Rational' as 'this' argument of 'void Rational::Simplify()' discards qualifiers
proj06.rational.cpp:233: error: passing 'const Rational' as 'this' argument of 'void Rational::Simplify()' discards qualifiers
proj06.rational.cpp: In function 'Rational operator+(const Rational&, Rational)':
proj06.rational.cpp:255: error: conversion from 'void' to non-scalar type 'Rational' requested
proj06.rational.cpp: In function 'Rational operator-(const Rational&, Rational)':
proj06.rational.cpp:276: error: conversion from 'void' to non-scalar type 'Rational' requested
proj06.rational.cpp: In function 'Rational operator*(const Rational&, Rational)':
proj06.rational.cpp:296: error: conversion from 'void' to non-scalar type 'Rational' requested
proj06.rational.cpp: In function 'Rational operator/(const Rational&, Rational)':
proj06.rational.cpp:316: error: conversion from 'void' to non-scalar type 'Rational' requested
Topic archived. No new replies allowed.