c++ operator overloading errors

Hi this is my first time at this site and i am trying to create a program that uses operator overloading on rational numbers entered by the user.
here is my code.

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
  //rational header//
  #ifndef Rational_H//header file//
#define Rational_H
#include<iostream>
class Rational
{
friend ostream & operator<<(ostream &  , const Rational &);
friend istream & operator<<(istream & , const Rational &);
public:
//Constructor with default values numerator = 0 and denominator = 1. If denominator = 0 then set to 1.//
Rational(int numerator = 0, int denominator = 1);

Rational(const Rational &);//Copy Constructor //

~Rational();//deconstructur//

Rational & operator=(const Rational &);//Function assigns a  rational number// 

operator double();//Function converts  rational number to a double data type (floating point number)//

//RATIONAL OPEREATORS//
Rational operator+(const Rational &);
Rational operator-(const Rational &);
Rational operator/(const Rational &);
Rational operator*(const Rational &);
Rational operator*=(const int number);

bool operator==(const Rational &);
bool operator!=(const Rational &);
bool operator>(const Rational &);
bool operator<(const Rational &);
private:
int *numerator;//Holds the numerator of the rational number//
int *denominator;//Holds the denominator of the rational number //
void reduction();//A private Utility Function for reducing Rational Number.//
};
#endif
// here is the rational cpp this part of the code is where i am having the most trouble with errors for the numerator and denominator//
#include<iostream>
#include"Rational.h"
#include<iomanip>
using namespace std;
//Programmer: Justin Bennett
//Date 7/27/2014//
//Description: A program that does rational operations on fractions entered by a user//
//File: lAB6a.CPP//
Rational::Rational( int numerator , int denominator )
{
	*numerator = n ; // sets numerator
                 if(d == 0) d = 1; // If denominator is zero then set to one.
   *denominator = d; // sets denominator
   reduction(); // store the fraction in reduced form
} // end Rational constructor
void Rational::reduction()
{
   void largest; 
  void largest = numerator > denominator ? numerator : denominator;

   int gcd = 0; // greatest common divisor

   for ( int loop = 2; loop <= largest; loop++ )

       if ( numerator % loop == 0 && denominator % loop == 0 )
          gcd = loop;

   if (gcd != 0) 
   {
      numerator /= gcd;
      denominator /= gcd;
   } // end if 
} // end function reduction

Rational::~Rational()
{
	cout<<endl <<" The Destructor for ";
	printRational();
	cout<<" has been called"<<endl;
}
Rational Rational::operator+( const Rational &a )
{
   Rational t; // creates Rational object

   t.numerator = a.numerator * denominator;
   t.numerator += a.denominator * numerator; 
   t.denominator = a.denominator * denominator;
   t.reduction(); // store the fraction in reduced form
   return t;
} // end function addition

Rational Rational::operator-( const Rational &s )
{
   Rational t; // creates Rational object 

   t.numerator = s.denominator * numerator;
   t.numerator -= denominator * s.numerator;
   t.denominator = s.denominator * denominator;
   t.reduction(); // store the fraction in reduced form
   return t;
} // end function subtraction

Rational Rational::operator*( const Rational &m )
{
   Rational t; // creates Rational object 

   t.numerator = m.numerator * numerator;
   t.denominator = m.denominator * denominator;
   t.reduction(); // store the fraction in reduced form
   return t;
} // end function multiplication

Rational Rational::operator *=( const int number) // function multipliation
{
   Rational t; // creates Rational object 

   t.numerator = numerator * number;
   t.denominator = denominator;
   t.reduction(); // store the fraction in reduced form
   return t;
} // end function multiplication	

Rational Rational::operator/( const Rational &v )
{
   Rational t; // creates Rational object 

   t.numerator = v.denominator * numerator;  
   t.denominator = denominator * v.numerator;
   t.reduction(); // store the fraction in reduced form
   return t;
} // end function division
 Rational::operator double() 
{  
   return static_cast< double >( numerator ) / denominator; 
} // end function printRationalAsDouble

bool Rational::operator == (const Rational &rhs) // is equal?
{
	double d_lhs = numerator / static_cast<double>(denominator);
	double d_rhs = rhs.numerator / static_cast<double>(denominator);
	
	return (d_lhs == d_rhs);
}
bool Rational::operator !=(const Rational &rhs) // is not equal?
{
	double d_lhs = numerator / static_cast<double>(denominator);
	double d_rhs = rhs.numerator / static_cast<double>(denominator);
	
	return (d_lhs != d_rhs);
}
bool Rational:: operator>(const Rational &rhs) // is greater than?
{
	double d_lhs = numerator / static_cast<double>(denominator);
	double d_rhs = rhs.numerator / static_cast<double>(denominator);
	
	return (d_lhs > d_rhs);
}
bool Rational::operator<(const Rational &rhs) // is less than?
{
	double d_lhs = numerator / static_cast<double>(denominator);
	double d_rhs = rhs.numerator / static_cast<double>(denominator);
	
	return (d_lhs < d_rhs);
}
//and this is the output it is suppose to have //
 Sample Output 1 (user input is bold):

Enter Rational Number (i.e. 1/3): 2/6
Enter Rational Number (i.e. 1/3): 7/8
2/6 + 7/8 = 29/24

29/24 = 1.20833


2/6 - 7/8 = -13/24
-13/24 = -0.541667


2/6 x 7/8 = 7/24
7/24 = 0.291667


2/6 x 10 = 10/3
10/3 = 3.33333


10 + 2/6 = 31/3
31/3 = 10.3333


2/6 / 7/8 = 8/21
8/21 = 0.380952


2/6 == 7/8 : false

2/6 != 7/8 : true

2/6 > 7/8 : false

2/6 < 7/8 : true

Press any key 
Any problem mentioned?
yeah i have a problem with the class definitions i keep getting errors with
setting up the numerator and denominator.
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
the compiler keeps giving me errors with numerator, and denominator
mainly because the numerator and denominator are pointers in the header 
and i have to have them that way.
Rational::Rational( int numerator , int denominator )
{
	*numerator = n ; // sets numerator
                 if(d == 0) d = 1; // If denominator is zero then set to one.
   *denominator = d; // sets denominator
   reduction(); // store the fraction in reduced form
} // end Rational constructor
 also i have trouble with the operators being setup right
Rational Rational::operator/( const Rational &v )
{
   Rational t; // creates Rational object 

   t.numerator = v.denominator * numerator;  
   t.denominator = denominator * v.numerator;
   t.reduction(); // store the fraction in reduced form
   return t;
} // end function division
 Rational::operator double() 
{  
   return static_cast< double >( numerator ) / denominator; 
} // end function printRationalAsDouble

bool Rational::operator == (const Rational &rhs) // is equal?
{
	double d_lhs = numerator / static_cast<double>(denominator);
	double d_rhs = rhs.numerator / static_cast<double>(denominator);
	
	return (d_lhs == d_rhs);

There're many wrong spots I saw only scan for secs.
Just like, i.e.
1
2
3
4
5
6
7
8
int *numerator;//Holds the numerator of the rational number//
int *denominator;//Holds the denominator of the rational number //
Rational::Rational( int numerator , int denominator ){
    *numerator = n ; // sets numerator
    if(d == 0) d = 1; // If denominator is zero then set to one.
    *denominator = d; // sets denominator
    reduction(); // store the fraction in reduced form
}

Where do n and d come from?
And you get param as int numerator and you have int *numerator; defined and then use *numerator = n ; ?
Non-sense at all.
1
2
3
4
5
6
7
8
int numerator;//Holds the numerator of the rational number//
int denominator;//Holds the denominator of the rational number //
Rational::Rational( int n, int d){
    numerator = n ; // sets numerator
    if(d == 0) d = 1; // If denominator is zero then set to one.
    denominator = d; // sets denominator
    reduction(); // store the fraction in reduced form
}
makes more sense?

I think you have to fix all of these non-sense syntax first and come ask again.
Last edited on
Topic archived. No new replies allowed.