Highschool C++ Homework Assignment

Rational fractions are of the form a / b, in which a and b are integers and b ≠ 0. In this exercise, by ‘‘fractions’’ we mean rational fractions. Suppose a / b and c / d are fractions. Arithmetic operations on fractions are defined by the following rules:

a/b + c/d = (ad + bc)/bd
a/b - c/d = (ad - bc)/bd
a/b * c/d = ac/bd
(a/b) / (c/d) = ad/bc, in which c/d ≠ 0
Fractions are compared as follows: a / b op c / d if ad op bc, in which op is any of the relational operations. For example, a / b < c / d if ad < bc.

Design a class fractionType that performs the arithmetic and relational operations on fractions. Overload the arithmetic and relational operators so that the appropriate symbols can be used to perform the operation. Also, overload the stream insertion and stream extraction operators for easy input and output.

Write the class fractionType, that performs operations on fractions. Among other things, test the following: Suppose x, y, and z are objects of type fractionType. If the input is 2/3, the statement:

cin >> x;
should store 2/3 in x. The statement:

cout << x + y << endl;
should output the value of x + y in fraction form. The statement:

z = x + y;
should store the sum of x and y in z in fraction form. Your answer need not be in the lowest terms.
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
//In my fractionType.cpp file
#include "Fraction.h"


Fraction::Fraction(int n, int d)
{
Num = n;
Denom = d;
if(d == 0)
cout << "Enter Denominator that is greater than 0";
}

int Fraction::getNum() const
{
return Num;
}

int Fraction::getDenom() const
{
return Denom;
}

Fraction Fraction::operator +(const Fraction &f2) const
{
Fraction total;

total.Num = Num * f2.Denom + f2.Num * Denom;
total.Denom = Denom * f2.Denom;

return total;
}

Fraction Fraction::operator-(const Fraction& f2) const
{
Fraction totalDiff;

totalDiff.Num = Num * f2.Denom + f2.Num * Denom;
totalDiff.Denom = Denom * f2.Denom;

return totalDiff;
}

Fraction Fraction::operator*(const Fraction& f2) const
{
Fraction totalMult;

totalMult.Num = Num * f2.Num;
totalMult.Denom = Denom * f2.Denom;

return totalMult;
}

Fraction Fraction::operator/(const Fraction& f2) const
{
Fraction totalDiv;

totalDiv.Num = Num * f2.Denom;
totalDiv.Denom = Denom * f2.Num;

return totalDiv;
}

//In my fractionType.h file
#ifndef FRACTION_H 
#define FRACTION_H
#include <iostream>
using namespace std;

class Fraction
{
private:
int Num,Denom;
public:
Fraction(int n = 0, int d = 1);
int getNum() const;
int getDenom() const;
Fraction operator+(const Fraction& f2) const; 
Fraction operator-(const Fraction& f2) const; // binary subtraction
Fraction operator*(const Fraction& f2) const; 
Fraction operator/(const Fraction& f2) const; 
Fraction operator-() const; // unary minus

};

#endif

//In my Main.cpp file
#include <iostream>
#include <iomanip> 
#include "fractionType.h"

using namespace std;

int main()
{
    fractionType num1(5, 6);
    fractionType num2;
    fractionType num3;

    cout << fixed;
    cout << showpoint;
    cout << setprecision(2);

    cout << "Num1 = " << num1 << endl;
    cout << "Num2 = " << num2 << endl;

    cout << "Enter the fraction in the form a / b:   ";
    cin >> num2;
    cout << endl;

    cout << "New value of num2 = " << num2 << endl;

    num3 = num1 + num2;

    cout << "Num3 = " << num3 << endl;
    cout << num1 << " + " << num2 << " = " << num1 + num2 << endl;
    cout << num1 << " * " << num2 << " = " << num1 * num2 << endl;

    num3 = num1 - num2;

    cout << "Num3 = " << num3 << endl;
    cout << num1 << " - " << num2 << " = " << num1 - num2 << endl;
    cout << "(" << num1 << ") / (" << num2 << ") = " << num1 / num2 << endl;

    return 0;
}
Do you have any questions?
Your implementation of the operator overloading is wrong. The two-operand operators need two arguments, and they needn't been implemented by the class. Look at this example:
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
#include <iostream>
using namespace std;

class Fraction
{
private:
    int Num,Denom;
public:
    Fraction(int n = 0, int d = 1) : Num(n), Denom(d) {};
    int getNum() const { return Num; }
    int getDenom() const { return Denom; }
};


Fraction operator+( const Fraction & a, const Fraction & b)
{
    return Fraction( a.getNum()*b.getDenom() + b.getNum()*a.getDenom()
                   , a.getDenom() * b.getDenom()
           );
}


ostream& operator<<( ostream& os, const Fraction & f)
{
    os << f.getNum() << '/' << f.getDenom();
    return os;
}

int main()
{
    Fraction a( 1, 2);
    Fraction b( 2, 3);
    Fraction c( 3, 4);

    cout << a << " + " << b << " + "<< c << " = " << a+b+c;
}
Your implementation of the operator overloading is wrong.

No, the OP's implementation is perfectly fine.
1
2
Fraction a, b, c;
c = a+b;   // calls a.operator+(b) 

In fraction.h, it's a really bad idea to have a using construct in a header file because it makes all those symbols visible to any other header files that you might include. Always put using in the cpp files and always after any #include s

Lines 96-98 of main.cpp: You declare variables of type fractionType but you have declared no such type. Do you mean Fraction?

You need to define the << and >> operators for Fraction. nuderobmonkey has shown how to define <<. Use that to help define >>

After the code is working and tested, save what you have and then consider this:

You could probably get some extra points by defining a normalize() method. Consider these problems with the class as written:

Fraction a(1,-2) create "1/-2" instead of "-1/2"
1/4 + 1/4 results in 2/4 instead of 1/2
1/2 / -1/2 will probably result in 2/-2 instead of -1/1
1/2 == 2/4 will probably return false

You can fix all of these problems by creating a function that casts a fraction into a standard form. It "normalizes" the fraction:
1
2
3
4
// Normalize a fraction
// Ensure that the denominator is positive.
// Reduce to lowest form by dividing numerator and denominator by their greatest common factor.
void Fraction::normalize();

At the end of each constructor and computation method, just call normalize() on the result.
This will force fractions to always be in normalized form. A side effect is that the comparison operations will work properly.

You will need this:
1
2
// Return the greatest common factor of two positive integers
int gcf(int a, int b);
@dhayden @nuderobmonkey

Thank you both for your responses and help. I would just like to clarify that I do NOT expect either of you to write this entire program for me, however I do ask that you guide me on the right path to completing the above instructions. Thank you both very much.
Owmn, thanks and understood. Have we given you enough to go on? If not, then let's pick a specific method. Try to write the code. When you get stuck for 30 minutes, post what you have here and ask as specific a question as possible.
Okay, so far this is what I've come up with:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 //In my fractionType.h file
#ifndef FRACTION_H 
#define FRACTION_H
#include <iostream>
using namespace std;

class Fraction
{
private:
int Num,Denom;
public:
Fraction(int n = 0, int d = 1);
int getNum() const;
int getDenom() const;
Fraction operator+(const Fraction& f2) const; 
Fraction operator-(const Fraction& f2) const; // binary subtraction
Fraction operator*(const Fraction& f2) const; 
Fraction operator/(const Fraction& f2) const; 
Fraction operator-() const; // unary minus

};

#endif 


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
//In my fractionType.cpp file
#include "fractionType.h"


Fraction::Fraction(int n, int d)
{
Num = n;
Denom = d;
if(d == 0)
cout << "Enter Denominator that is greater than 0";
}

int Fraction::getNum() const
{
return Num;
}

int Fraction::getDenom() const
{
return Denom;
}

Fraction Fraction::operator +(const Fraction &f2) const
{
Fraction total;

total.Num = Num * f2.Denom + f2.Num * Denom;
total.Denom = Denom * f2.Denom;

return total;
}

Fraction Fraction::operator-(const Fraction& f2) const
{
Fraction totalDiff;

totalDiff.Num = Num * f2.Denom + f2.Num * Denom;
totalDiff.Denom = Denom * f2.Denom;

return totalDiff;
}

Fraction Fraction::operator*(const Fraction& f2) const
{
Fraction totalMult;

totalMult.Num = Num * f2.Num;
totalMult.Denom = Denom * f2.Denom;

return totalMult;
}

Fraction Fraction::operator/(const Fraction& f2) const
{
Fraction totalDiv;

totalDiv.Num = Num * f2.Denom;
totalDiv.Denom = Denom * f2.Num;

return totalDiv;
}


And finally, this is what I am trying to accomplish:

Fraction operator-() const; // unary minus
bool operator==(const Fraction& f2) const;
bool operator<(const Fraction& f2) const;
bool operator<=(const Fraction& f2) const;
bool operator>(const Fraction& f2) const;
bool operator>=(const Fraction& f2) const;
void reduce();
void standardize();
* Friend functions:
ostream& operator<<(ostream& out, const Fraction& f);
* Regular functions:
bool operator!=(const Fraction& f1, const Fraction& f1);
The reduce() method is somewhat tricky, you need to find the greatest common divisor. Therefore you need to test for all prime numbers if they are divisors of both Num and Denom, whereby a prime could be for several times the divisor. But because all numbers are compounded by prime numbers, you cannot have a compounded number where two divisors are greater than the square root of a number. This behavior you can exploit by searching only prime numbers which are <= sqrt.
But it's somewhat hard finding prime numbers, so I would suggest to check instead merely for 2 and all odd numbers which are <= sqrt.

And for your standardize function you could check if Denom is a negative number. Then you could flip the signs of Denom and Num.
Last edited on
You don't need to find prime numbers to find GCD. Just use Euclid's method. Google "C++ Euclid" for examples.

When doing reduce(), be sure to handle the case of negative numbers too.

For the comparison operators, the easy, though somewhat inefficient, way is to define == and <. Then:
1
2
3
4
bool operator<=(const Fraction &f2) const { return ! (f2 < *this); }
bool operator>(const Fraction &f2) const { return f2 < *this; }
bool operator >=(const Fraction &f2) const {return ! (*this < f2); }
bool operator !=(const Fraction &f1, const Fraction &f2) { return !(f1 == f2); }


Okay, so this is what I have so far:

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
//In my fractionType.cpp file
#include <iostream>

#include "fractionType.h"



using namespace std;



fractionType::fractionType(const int& nu, const int& de)

{

  a = nu;

   

  if (de == 0)

  {

    cout << "ntInvalid denominator. "

    << "Default value considered for denominator. ";

    b = 1;

  }

  else

    b = de;

}



fractionType::fractionType()

{

  a = 0;

  b = 1;

}



fractionType::fractionType(const fractionType& rightFraction)

{

  a = rightFraction.a;

  b = rightFraction.b;

}



fractionType::~fractionType()

{

   

}



bool fractionType::operator==(const fractionType& rightFraction) const

{

  return ((a == rightFraction.a) && (b == rightFraction.b));

}



bool fractionType::operator != (const fractionType& rightFraction) const

{

  return ((a != rightFraction.a) || (b != rightFraction.b));

}



bool fractionType::operator<(const fractionType& rightFraction) const

{

  return (a * rightFraction.b < b * rightFraction.a);

}



bool fractionType::operator<=(const fractionType& rightFraction) const

{

  return (a * rightFraction.b <= b * rightFraction.a);

}



bool fractionType::operator>(const fractionType& rightFraction) const

{

  return (a * rightFraction.b > b * rightFraction.a);

}



bool fractionType::operator>=(const fractionType& rightFraction) const

{

  return (a * rightFraction.b >= b * rightFraction.a);

}



fractionType::operator + fractionType& rightFraction const

{

  fractionType temp;

  temp.a = (a * rightFraction.b) + (b * rightFraction.a);

  temp.b = b * rightFraction.b;

  return temp;

}



fractionType::operator - const fractionType& rightFraction

{

  fractionType temp;

  temp.a = (a * rightFraction.b) - (b * rightFraction.a);

  temp.b = b * rightFraction.b;

  return temp;

}



fractionType fractionType::operator*(const fractionType& rightFraction) const

{

  fractionType temp;

  temp.a = a * rightFraction.a;

  temp.b = b * rightFraction.b;

  return temp;

}



fractionType fractionType::operator/(const fractionType& rightFraction) const

{

  fractionType temp;

  if ((rightFraction.a == 0) || (rightFraction.b == 0))

  {

    temp.a = 0

    temp.b = 1;

    cout << "ntInvalid to perform division. ";

  }

  else

  {

    temp.a = a * rightFraction.b;

    temp.b = b * rightFraction.b;

  }

  return temp;

}



ostream& operator<<(ostream& osObject, const fractionType& myFraction)

{

  osObject << myFraction.a << "/" << myFraction.b;

  return osObject;

}



istream& operator>>(istream isObject, fractionType& myFraction)

{

  char ch;

  isObject >> myFraction.a >> ch >> myFraction.b;

  return isObject;

}


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
//In my fractionType.h
//#ifdef fractionType.h
//#define fractionType_H

#include <iostream>



using namespace std;



class fractionType

{

  friend ostream& operator << (ostream&, const fractionType&);

  friend istream& operator >> (istream&, fractionType&);

   

public:

  const fractionType& operator=(const fractionType&);

   

  fractionType();

  fractionType(const fractionType&);

  fractionType(const int&, const int&);

   

  ~fractionType();

   

  bool operator==(const fractionType&) const;

  bool operator!=(const fractionType&) const;

  bool operator<=(const fractionType&) const;

  bool operator< (const fractionType&) const;

  bool operator>=(const fractionType&) const;

  bool operator> (const fractionType&) const;

   

  fractionType operator + (const fractionType&);

  fractionType operator - (const fractionType&);

  fractionType operator * (const fractionType&);

  fractionType operator / (const fractionType&);

   


  int a;

  int b;

};


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
//In my main.cpp file
#include <iostream>

#include "fractionType.h"



using namespace std;



int main ()

{

  cout << "nntA program to check the functionality"

  << "ntof the class fractionType. n";

   

  fractionType myFraction1;

  fractionType myFraction2;

   

  cout << "nntEnter a fraction in the form a/b: ";

  cin >> myFraction1;

   

  cout << "nntEnter another fraction in the form a/b: ";

  cin >> myFraction2;

   

  cout << "nnt" << myFraction1 << " + " << myFraction2 <<" = " << myFraction1 + myFraction2;

  cout << "nnt" << myFraction1 << " - " << myFraction2 <<" = " << myFraction1 - myFraction2;

  cout << "nnt" << myFraction1 << " * " << myFraction2 <<" = " << myFraction1 * myFraction2;

  cout << "nnt" << myFraction1 << " / " << myFraction2 <<" = " << myFraction1 / myFraction2;

   

  cout << "nt" << myFraction1 << " == " << myFraction2;

  if (myFraction1 == myFraction2)

  {

    cout << " is true.";

  }

  else

    cout << " is false.";

   

  cout << "nt" << myFraction1 << " != " << myFraction2;

  if (myFraction1 != myFraction2)

  {

    cout << " is true. ";

  }

  else

  {

    cout << " is false. ";

  }

   

  cout << "nt" << myFraction1 << " < " << myFraction2;

  if (myFraction1 < myFraction2)

  {

    cout << " is true. ";

  }

  else

  {

    cout << " is false. ";

  }

   

  cout << "nt" << myFraction1 << " <= " << myFraction2;

  if (myFraction1 <= myFraction2)

  {

    cout << " is true. ";

  }

  else

  {

    cout << " is false. ";

  }

   

  cout << "nt" << myFraction1 << " > " << myFraction2;

  if (myFraction1 > myFraction2)

  {

    cout << " is true. ";

  }

  else

  {

    cout << " is false. ";

  }

   

  cout << "nt" << myFraction1 << " >= " << myFraction2;

  if (myFraction1 != myFraction2)

  {

    cout << " is true. ";

  }

  else

  {

    cout << " is false. ";

  }

   

  cout << "nnt";

   

  return 0;

   

}


However, these are the errors that show up:
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
fractionType.cpp:133:24: error: ‘operator+’ in ‘class fractionType’ does not name a type
 fractionType::operator + fractionType& rightFraction const
                        ^
fractionType.cpp:149:24: error: ‘operator-’ in ‘class fractionType’ does not name a type
 fractionType::operator - const fractionType& rightFraction
                        ^
fractionType.cpp:165:14: error: prototype for ‘fractionType fractionType::operator*(const fractionType&) const’ does not match any in class ‘fractionType’
 fractionType fractionType::operator*(const fractionType& rightFraction) const
              ^~~~~~~~~~~~
In file included from fractionType.cpp:3:0:
fractionType.h:58:16: error: candidate is: fractionType fractionType::operator*(const fractionType&)
   fractionType operator * (const fractionType&);
                ^~~~~~~~
fractionType.cpp:181:14: error: prototype for ‘fractionType fractionType::operator/(const fractionType&) const’ does not match any in class ‘fractionType’
 fractionType fractionType::operator/(const fractionType& rightFraction) const
              ^~~~~~~~~~~~
In file included from fractionType.cpp:3:0:
fractionType.h:60:16: error: candidate is: fractionType fractionType::operator/(const fractionType&)
   fractionType operator / (const fractionType&);
                ^~~~~~~~
fractionType.cpp: In function ‘std::istream& operator>>(std::istream, fractionType&)’:
fractionType.cpp:227:29: warning: reference to local variable ‘isObject’ returned [-Wreturn-local-addr]
 ream& operator>>(istream isObject, fractionType& myFraction)
                          ^~~~~~~~
bash: line 2: ./a.out: No such file or directory
fractionType.cpp:
lines 134 & 150: you're missing the parentheses.

fractionType.h line 55 to 61: these should be const.

this is what I have so far:
Almost everything about this code is different from your previous attempt: the name of the class, the names of the data members, the coding style, the names of local variables, everything. This makes me suspect that one or the other (or both) isn't your work.

Please understand that coding is like football. The only way to learn how to do is to practice. If you're grabbing someone else's work you're just delaying an inevitable crash and burn.
I thank you for your help, however I am still receiving many errors. And this is my first semester taking a c++ coding class, and I am having trouble understanding the language, because of how it was presented to me. I apologize if this inconveniences you; I would just rather receive outside help, pass, and then learn in a different way after the semester.
I understand if you are no longer willing to help me, but if you are the errors I am receiving are:
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
fractionType.cpp:133:57: error: ISO C++ forbids declaration of ‘parameter’ with no type [-fpermissive]
 (fractionType& rightFraction, const)
                               ^~~~~
fractionType.cpp:133:62: error: ISO C++ forbids declaration of ‘operator+’ with no type [-fpermissive]
 (fractionType& rightFraction, const)
                                    ^
fractionType.cpp:133:62: error: ‘intfractionType::operator+(fractionType&, int)’ must take either zero or oneargument
fractionType.cpp:149:60: error: ISO C++ forbids declaration of ‘operator-’ with no type [-fpermissive]
  (const fractionType& rightFraction)
                                    ^
fractionType.cpp:149:1: error: prototype forint fractionType::operator-(const fractionType&)’ does not matchany in class ‘fractionType’
 fractionType::operator - (const fractionType& rightFraction)
 ^~~~~~~~~~~~
In file included from fractionType.cpp:3:0:
fractionType.h:56:21: error: candidate is: const fractionType fractionType::operator-(const fractionType&)
  const fractionType operator - (const fractionType&);
                     ^~~~~~~~
fractionType.cpp:165:14: error: prototype for ‘fractionType fractionType::operator*(const fractionType&) const’ does not match any in class ‘fractionType’
 fractionType fractionType::operator*(const fractionType& rightFraction) const
              ^~~~~~~~~~~~
In file included from fractionType.cpp:3:0:
fractionType.h:58:21: error: candidate is: const fractionType fractionType::operator*(const fractionType&)
  const fractionType operator * (const fractionType&);
                     ^~~~~~~~
fractionType.cpp:181:14: error: prototype for ‘fractionType fractionType::operator/(const fractionType&) const’ does not match any in class ‘fractionType’
 fractionType fractionType::operator/(const fractionType& rightFraction) const
              ^~~~~~~~~~~~
In file included from fractionType.cpp:3:0:
fractionType.h:60:21: error: candidate is: const fractionType fractionType::operator/(const fractionType&)
  const fractionType operator / (const fractionType&);
                     ^~~~~~~~
fractionType.cpp: In function ‘std::istream& operator>>(std::istream, fractionType&)’:
fractionType.cpp:227:29: warning: reference to local variable ‘isObject’ returned [-Wreturn-local-addr]
 ream& operator>>(istream isObject, fractionType& myFraction)
                          ^~~~~~~~
Who cares if you pass? High school is a joke and a waste of time and effort. School places far too much pressure on attaining good grades when the whole point of school is supposed to be learning.

You'll do better in the short and long run if you focus on learning rather than performance.

If you don't like the class then don't go. It's not as if anyone is truly forcing you to go, even if they say it's "required." Depending on your age you may not even have to go to school at all.

I would stay in it, though. Programming practices valuable problem solving and critical thinking skills, even if you never write another line of code in your life. If you are smart you can learn even from a poorly structured class.

Do you understand function syntax? Pretty much all of those errors are related to incorrect function syntax. Your * operator seems fine. Use this as a template for the other ones that aren't working.

fractionType fractionType::operator*(const fractionType& rightFraction) const

Some of the other errors are because your prototypes in the header don't match the implementations in the .cpp file. All of the types have to match, including the const qualifiers.

Your prototype should look like this to match your implementation, notice I just added "const" at the end. This says that the object won't be modified by the function call.

fractionType operator * (const fractionType&) const;
Last edited on
I am having trouble understanding the language, because of how it was presented to me. I apologize if this inconveniences you; I would just rather receive outside help, pass, and then learn in a different way after the semester.
No need to apologize. I hope that you're able to learn the language. This site is a great place to get help.

Okay, getting back to your code.
In fractionType.cpp:
Line 134: The definition needs to match the declaration given in fractionType.h. so as a first pass, this should be
fractionType fractionType::operator + (const fractionType& rightFraction)

But really, operator+ should be const, meaning that you want to tell the compiler that it won't modify the object it's called on. While we're at it, let's make the other operators const also, so in fractionType.h, lines 55-61 should be :
1
2
3
4
  fractionType operator + (const fractionType&) const;
  fractionType operator - (const fractionType&) const;
  fractionType operator * (const fractionType&) const;
  fractionType operator / (const fractionType&) const;


and back in fractionType.cpp, Line 134 should actually be:
fractionType fractionType::operator + (const fractionType & rightFraction) const

Continuing in fractionType.cpp:
Line 150: Same comment as line 134 above. Make this line exactly like 134 as I've changed it, except change + to -

Line 192: add ; to the end of the line.
LIne 228: istream must be passed by reference:
istream& operator>>(istream &isObject, fractionType& myFraction)


This should make the code compile.
Thank you all very much for your help! My program is now working as it should, however there is some sort of technical difficulty preventing me from submitting it, but I will speak with my teacher about this. Thank you!
Topic archived. No new replies allowed.