• (2 Pages)
• 1
• 2

## 17 Replies - 748 Views - Last Post: 29 April 2012 - 10:38 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=277330&amp;s=e580f6aaf5055a9c2cae8dac36d82daa&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 teamsilvamma

Reputation: -2
• Posts: 140
• Joined: 17-January 12

Posted 29 April 2012 - 07:28 AM

Hey guys, my program is working for the most part except the logical an relational overloading operator functions. This is due today in a couple of hours. If you can help me and guide me in the right direction, I'll appreciate it.

I don't want you to write my program, but please I have no time for riddles. Genuinely help me like "you need to use a switch statement instead of an if-else statement", or "put the if else in the class and call it in the main file" etc...

thanks in advance..heres my code so far (with instruction on the comment section on top of the class file

class
```/********************************
* rational_number.cpp
* Luis Silva
*
*********************INSTRUCTIONS******************************
* Create a constructor that:
* prevents zero as a denominator
* reduces a fraction
* avoids neg denominator
operators
* Overload the relational and equality operators

**************************OUTPUT*****************************
Fraction One

Enter numerator: 3
Enter denominator: 2

Error: "invalid input try again".

Enter numerator: 3
Enter denominator: 0

Error: "invalid input try again".

Enter numerator: 3
Enter denominator: -9

Error: "invalid input try again".

Enter numerator: 2
Enter denominator: 4

Fraction Two

Enter numerator: 3
Enter denominator: 5

The result from their sum is: 11 / 10
The result from their substration is: -1 / 10
The result from their multiplication is: 3 / 10
The result from their division is: 5 / 6

*************************************************************/

#include "rational_number.h"
#include <iomanip>
#include <cstdlib>

using namespace std;

//initializing constructor
Rational::Rational()
{
init();
}
//copy constructor.
Rational::Rational(const Rational& r)
{
copy(r);
}
//equal operator check for self assignment and makes a copy or moves on
Rational& Rational::operator=(const Rational& rhs)
{
if(this != &rhs)		// Check for self-assignment
copy(rhs);

return *this;
}
//input overloaded function creates input and checks for conditions
istream& operator>>(istream& in, Rational& r)
{
while(in)
{
cout << "Enter numerator: ";
in >> r.num;

cout << "Enter denominator: ";
in >> r.denom;

if(r.denom > 0 && r.denom > r.num)
{
r.reduce();
break;
}
else if (r.denom == 1)
break;

cerr << "\nError: \"invalid input try again\".\n\n";
}
cout << endl;
return in;
}
//output overloaded function is assigned to c
ostream& operator<<(ostream& out, const Rational& r)
{
return out << r.num << " / " << r.denom;
}
Rational& Rational::operator+=(const Rational& rhs)
{
num = (num * rhs.denom) + (rhs.num * denom);
denom *= rhs.denom;
return *this;
}

Rational operator+(const Rational& lhs, const Rational& rhs)
{
Rational sum(lhs);
sum += rhs;
return sum;
}

Rational& Rational::operator-=(const Rational& rhs)
{
num = (num * rhs.denom) - (rhs.num * denom);
denom *= rhs.denom;
return *this;
}

Rational operator-(const Rational& lhs, const Rational& rhs)
{
Rational diff(lhs);
diff -= rhs;
return diff;
}

Rational& Rational::operator*=(const Rational& rhs)
{
num *= rhs.num;
denom *= rhs.denom;
return *this;
}

Rational operator*(const Rational& lhs, const Rational& rhs)
{
Rational prod(lhs);
prod *= rhs;
return prod;
}

Rational& Rational::operator/=(const Rational& rhs)
{
num *= rhs.denom;
denom *= rhs.num;
return *this;
}

Rational operator/(const Rational& lhs, const Rational& rhs)
{
Rational quot(lhs);
quot /= rhs;
return quot;
}

Rational& Rational::operator==(const Rational& rhs)
{
num == rhs.num;
denom == rhs.denom;
return *this;
}

Rational operator==(const Rational& lhs, const Rational& rhs)
{
Rational same(lhs);
same == rhs;
return same;
}
//**********HELPER FUNCTIONS************
//function find gcd for proper reduction
int Rational::gcd(int x, int y)
{
for(int i = min(x, y); i > 1; --i)
{
if((x % i == 0) && (y % i == 0))
return i;
}
return 0;
}
//funtion that simplifies result
void Rational::reduce()
{
int factor = gcd(num, denom);

if(factor != 0)
{
num /= factor;
denom /= factor;
}
}
//Initiliazing function
void Rational::init()
{
num = denom = 0;
}
// Copy a Rational number into this Rational object.
void Rational::copy(const Rational& r)
{
num	 = r.num;
denom = r.denom;
}

```

main
```/********************************
* main.cpp
* Luis Silva
*
* main file to run class and header
*************************************************************/
#include "rational_number.h"

using namespace std;

int main()
{
//creating objects for each fraction
Rational a, b, c;
//allowing input
cout << "\tFraction One\n\n";
cin >> a;

cout << "\tFraction Two\n\n";
cin >> b;

//computation of fractions
c = a + b;
cout << "The result from their sum is: " << c << endl;
c = a - b;
cout << "The result from their substration is: " << c << endl;
c = a * b;
cout << "The result from their multiplication is: " << c << endl;
c = a / b;
cout << "The result from their division is: " << c << endl;
/*
if (a = B)/>
cout << "Both fractions are the same " << endl;
else
cout << "Both fractions are not the same " << endl;
*/

}

```

```/***************************
* rational_number.h
* Luis Silva
*
****************************/

#ifndef RATIONAL_NUMBER_H
#define RATIONAL_NUMBER_H

#include <iostream>

struct Rational
{
public:
//declaration of construtors
Rational();  //empty constructor
Rational(const Rational&); //copy constructor

// Rational input and output functions using overloaded operators:
friend std::istream& operator>>(std::istream& in, Rational& r);
friend std::ostream& operator<<(std::ostream& out, const Rational& r);

Rational& operator+=(const Rational& rhs);
friend Rational operator+(const Rational& lhs, const Rational& rhs);

Rational& operator-=(const Rational& rhs);
friend Rational operator-(const Rational& lhs, const Rational& rhs);

Rational& operator*=(const Rational& rhs);
friend Rational operator*(const Rational& lhs, const Rational& rhs);

Rational& operator/=(const Rational& rhs);
friend Rational operator/(const Rational& lhs, const Rational& rhs);

Rational& operator==(const Rational& rhs);
friend Rational operator==(const Rational& lhs, const Rational& rhs);

/*
//Relational operators ==, !=, >, <, >=, <=

Rational& operator!=(const Rational& rhs);
friend Rational operator/(const Rational& lhs, const Rational& rhs);

Rational& operator>=(const Rational& rhs);
friend Rational operator/(const Rational& lhs, const Rational& rhs);

Rational& operator<=(const Rational& rhs);
friend Rational operator/(const Rational& lhs, const Rational& rhs);

Rational& operator>=(const Rational& rhs);
friend Rational operator/(const Rational& lhs, const Rational& rhs);

Rational& operator<=(const Rational& rhs);
friend Rational operator/(const Rational& lhs, const Rational& rhs);

//logical operators !,&&,||

*/

private:
int num, denom;
const char* errMsg;

// Helper functions:
void init();
int  gcd(int, int);
void reduce();
void copy(const Rational&);
};

#endif // RATIONAL_NUMBER_H

```

Is This A Good Question/Topic? 0

### #2 r.stiltskin

• D.I.C Lover

Reputation: 1831
• Posts: 4,927
• Joined: 27-December 05

Posted 29 April 2012 - 07:36 AM

The only relational operator I see there is ==. You have 2 versions, both wrong.

The return type should be bool. It should return true if the "this" object is equal to the argument, and false otherwise. Similarly for the > and < operators, except that for those of course the relationship is greater than and less than.

### #3 teamsilvamma

Reputation: -2
• Posts: 140
• Joined: 17-January 12

Posted 29 April 2012 - 07:45 AM

r.stiltskin, on 29 April 2012 - 09:36 AM, said:

The only relational operator I see there is ==. You have 2 versions, both wrong.

The return type should be bool. It should return true if the "this" object is equal to the argument, and false otherwise. Similarly for the > and < operators, except that for those of course the relationship is greater than and less than.

Ok, good, I did some research online and that's what I got too. But we didn't cover bool in class for overloading operator functions. Reason why I was stuck. I don't know the syntax.
This produces syntax error

```bool Rational& Rational::operator==(const Rational& rhs)
{
num == rhs.num;
denom == rhs.denom;
return true;
}

```

### #4 r.stiltskin

• D.I.C Lover

Reputation: 1831
• Posts: 4,927
• Joined: 27-December 05

Posted 29 April 2012 - 07:53 AM

Error is because now you have 2 return types. It shouldn't be bool Rational& Rational:: ....

Just bool Rational:: ...

Also, on second thought, it's better to implement this as a friend, so it would have 2 const Rational& arguments, and compare them.

And also, you might have to do some more work in the body of the function. Unless your Rational numbers are always kept in reduced form, you have to reduce both sides before you compare. Otherwise, for example, 2/4 will not be equal to 1/2.

### #5 teamsilvamma

Reputation: -2
• Posts: 140
• Joined: 17-January 12

Posted 29 April 2012 - 08:10 AM

r.stiltskin, on 29 April 2012 - 09:53 AM, said:

Error is because now you have 2 return types. It shouldn't be bool Rational& Rational:: ....

Just bool Rational:: ...

Also, on second thought, it's better to implement this as a friend, so it would have 2 const Rational& arguments, and compare them.

And also, you might have to do some more work in the body of the function. Unless your Rational numbers are always kept in reduced form, you have to reduce both sides before you compare. Otherwise, for example, 2/4 will not be equal to 1/2.

alright so I changed the function. I gave it a bool, both sides in the parameters, I called the reduce() function, and modified the header and it's not taking it.

```bool Rational::operator==(const Rational& lhs, const Rational& rhs)
{
reduce();
num == rhs.num;
denom == rhs.denom;
return true;
}
```

```    friend bool Rational& operator==(const Rational& rhs);

```

Error code says

39|error: 'Rational' is neither function nor member function; cannot be declared friend|
39|error: expected ';' before '&' token|
157|error: 'bool Rational::operator==(const Rational&, const Rational&)' must take exactly one argument|
||=== Build finished: 3 errors, 0 warnings ===|

### #6 r.stiltskin

• D.I.C Lover

Reputation: 1831
• Posts: 4,927
• Joined: 27-December 05

Posted 29 April 2012 - 08:17 AM

You're still making the same mistake. You have 2 return types in this declaration:

Quote

```friend bool Rational& operator==(const Rational& rhs);
```

Just friend bool operator== ...

And in this

Quote

```bool Rational::operator==(const Rational& lhs, const Rational& rhs)
{
reduce();
num == rhs.num;
denom == rhs.denom;
return true;
}
```

(1) whose "reduce" are you calling? You have to call the reduce() methods of the two arguments.
(2) a friend is not a member function, so it shouldn't be scoped with Rational::. The only thing that goes inside the class is the friend declaration.

This post has been edited by r.stiltskin: 29 April 2012 - 08:19 AM

### #7 teamsilvamma

Reputation: -2
• Posts: 140
• Joined: 17-January 12

Posted 29 April 2012 - 08:37 AM

r.stiltskin, on 29 April 2012 - 10:17 AM, said:

You're still making the same mistake. You have 2 return types in this declaration:

Quote

```friend bool Rational& operator==(const Rational& rhs);
```

Just friend bool operator== ...

And in this

Quote

```bool Rational::operator==(const Rational& lhs, const Rational& rhs)
{
reduce();
num == rhs.num;
denom == rhs.denom;
return true;
}
```

(1) whose "reduce" are you calling? You have to call the reduce() methods of the two arguments.
(2) a friend is not a member function, so it shouldn't be scoped with Rational::. The only thing that goes inside the class is the friend declaration.

Cool! The friend line is fixed.

The class function however is still generating errors. The reduce(); is declared in my class file to reduce the fractions when they're entered. It works for the other operators I overloaded.

I'm getting this error. I have no idea what this means
157|error: no 'bool Rational::operator==(const Rational&)' member function declared in class 'Rational'|
||=== Build finished: 1 errors, 0 warnings ===|

### #8 r.stiltskin

• D.I.C Lover

Reputation: 1831
• Posts: 4,927
• Joined: 27-December 05

Posted 29 April 2012 - 08:48 AM

As I said in point 2 in my previous post:
a friend function is not a member function. It's a "top-level" function, not a member of the class. The friend declaration goes inside the class definition, but the function itself is just like any ordinary global function. Not bool Rational::operator==. Just bool operator==.

The compiler decides which "operator==" it is based on the types of the arguments you supply.

### #9 teamsilvamma

Reputation: -2
• Posts: 140
• Joined: 17-January 12

Posted 29 April 2012 - 09:17 AM

r.stiltskin, on 29 April 2012 - 10:48 AM, said:

As I said in point 2 in my previous post:
a friend function is not a member function. It's a "top-level" function, not a member of the class. The friend declaration goes inside the class definition, but the function itself is just like any ordinary global function. Not bool Rational::operator==. Just bool operator==.

The compiler decides which "operator==" it is based on the types of the arguments you supply.

Ok so I removed the Rational word and this is what I have. the errors shifted to the next line.

```bool operator==(const Rational& lhs, const Rational& rhs)
{
num == rhs.num;
denom == rhs.denom;
return true;
}
```

159|error: 'num' was not declared in this scope|
160|error: 'denom' was not declared in this scope|
||=== Build finished: 2 errors, 0 warnings ===|

### #10 r.stiltskin

• D.I.C Lover

Reputation: 1831
• Posts: 4,927
• Joined: 27-December 05

Posted 29 April 2012 - 09:35 AM

Think about it: this == is NOT a member function. It doesn't have "its own" num and denom members.

You're comparing the data members of the lhs object to those of the rhs object.

### #11 teamsilvamma

Reputation: -2
• Posts: 140
• Joined: 17-January 12

Posted 29 April 2012 - 09:50 AM

r.stiltskin, on 29 April 2012 - 11:35 AM, said:

Think about it: this == is NOT a member function. It doesn't have "its own" num and denom members.

You're comparing the data members of the lhs object to those of the rhs object.

ok so I did this. It compiles but returns nothing. How do I check this in my main file. I imagine it's suposed to tell me if fraction 1 is the same as fraction 2?

but lhs and rhs is not defined in the main file. Only a and b. So when I tried if a = b then cout <<something it doesn't recognize it. However for the rest of the operators it works

### #12 r.stiltskin

• D.I.C Lover

Reputation: 1831
• Posts: 4,927
• Joined: 27-December 05

Posted 29 April 2012 - 09:55 AM

I don't know what you mean by "it doesn't recognize it".

However, it's
```if( a == b )
{
// do something

```

a == b NOT a = b

If that doesn't solve your problem, explain more clearly, along with the actual code that's giving the problem.

### #13 teamsilvamma

Reputation: -2
• Posts: 140
• Joined: 17-January 12

Posted 29 April 2012 - 10:07 AM

r.stiltskin, on 29 April 2012 - 11:55 AM, said:

I don't know what you mean by "it doesn't recognize it".

However, it's
```if( a == b )
{
// do something

```

a == b NOT a = b

If that doesn't solve your problem, explain more clearly, along with the actual code that's giving the problem.

I put this in
```
if (a == b)
cout << "Both fractions are the same " << endl;
else
cout << "Both fractions are not the same " << endl;

```

and when I entered 2/4 and 3/5 for the fractions it says they are the same! I changed it to a!=b to check for a different output and it didnt compile. I also did if !(a == b ) and also didnt compile. Then I did this if (!( a == b ) ) and it did tell me that they weren't the same but it also told me they werent the same when I entered 2/4 and 2/4 lol

I can post the whole code if you want.

This post has been edited by teamsilvamma: 29 April 2012 - 10:10 AM
Reason for edit:: Fixed code tags.

### #14 r.stiltskin

• D.I.C Lover

Reputation: 1831
• Posts: 4,927
• Joined: 27-December 05

Posted 29 April 2012 - 10:15 AM

Post the complete code.

### #15 teamsilvamma

Reputation: -2
• Posts: 140
• Joined: 17-January 12

Posted 29 April 2012 - 10:17 AM

```/***************************
* rational_number.h
* Luis Silva
*
****************************/

#ifndef RATIONAL_NUMBER_H
#define RATIONAL_NUMBER_H

#include <iostream>

struct Rational
{
public:
//declaration of construtors
Rational();  //empty constructor
Rational(const Rational&); //copy constructor

// Rational input and output functions using overloaded operators:
friend std::istream& operator>>(std::istream& in, Rational& r);
friend std::ostream& operator<<(std::ostream& out, const Rational& r);

Rational& operator+=(const Rational& rhs);
friend Rational operator+(const Rational& lhs, const Rational& rhs);

Rational& operator-=(const Rational& rhs);
friend Rational operator-(const Rational& lhs, const Rational& rhs);

Rational& operator*=(const Rational& rhs);
friend Rational operator*(const Rational& lhs, const Rational& rhs);

Rational& operator/=(const Rational& rhs);
friend Rational operator/(const Rational& lhs, const Rational& rhs);

//Rational& operator==(const Rational& rhs);
friend bool operator==(const Rational& lhs, const Rational& rhs);

/*
//Relational operators ==, !=, >, <, >=, <=

Rational& operator!=(const Rational& rhs);
friend Rational operator/(const Rational& lhs, const Rational& rhs);

Rational& operator>=(const Rational& rhs);
friend Rational operator/(const Rational& lhs, const Rational& rhs);

Rational& operator<=(const Rational& rhs);
friend Rational operator/(const Rational& lhs, const Rational& rhs);

Rational& operator>=(const Rational& rhs);
friend Rational operator/(const Rational& lhs, const Rational& rhs);

Rational& operator<=(const Rational& rhs);
friend Rational operator/(const Rational& lhs, const Rational& rhs);

//logical operators !,&&,||

*/

private:
int num, denom;
const char* errMsg;

// Helper functions:
void init();
int  gcd(int, int);
void reduce();
void copy(const Rational&);
};

#endif // RATIONAL_NUMBER_H

```

```/********************************
* rational_number.cpp
* Luis Silva
*
*********************INSTRUCTIONS******************************
* Create a constructor that:
* prevents zero as a denominator
* reduces a fraction
* avoids neg denominator
operators
* Overload the relational and equality operators

**************************OUTPUT*****************************
Fraction One

Enter numerator: 3
Enter denominator: 2

Error: "invalid input try again".

Enter numerator: 3
Enter denominator: 0

Error: "invalid input try again".

Enter numerator: 3
Enter denominator: -9

Error: "invalid input try again".

Enter numerator: 2
Enter denominator: 4

Fraction Two

Enter numerator: 3
Enter denominator: 5

The result from their sum is: 11 / 10
The result from their substration is: -1 / 10
The result from their multiplication is: 3 / 10
The result from their division is: 5 / 6

*************************************************************/

#include "rational_number.h"
#include <iomanip>
#include <cstdlib>

using namespace std;

//initializing constructor
Rational::Rational()
{
init();
}
//copy constructor.
Rational::Rational(const Rational& r)
{
copy(r);
}
//equal operator check for self assignment and makes a copy or moves on
Rational& Rational::operator=(const Rational& rhs)
{
if(this != &rhs)		// Check for self-assignment
copy(rhs);

return *this;
}
//input overloaded function creates input and checks for conditions
istream& operator>>(istream& in, Rational& r)
{
while(in)
{
cout << "Enter numerator: ";
in >> r.num;

cout << "Enter denominator: ";
in >> r.denom;

if(r.denom > 0 && r.denom > r.num)
{
r.reduce();
break;
}
else if (r.denom == 1)
break;

cerr << "\nError: \"invalid input try again\".\n\n";
}
cout << endl;
return in;
}
//output overloaded function is assigned to c
ostream& operator<<(ostream& out, const Rational& r)
{
return out << r.num << " / " << r.denom;
}
Rational& Rational::operator+=(const Rational& rhs)
{
num = (num * rhs.denom) + (rhs.num * denom);
denom *= rhs.denom;
return *this;
}

Rational operator+(const Rational& lhs, const Rational& rhs)
{
Rational sum(lhs);
sum += rhs;
return sum;
}

Rational& Rational::operator-=(const Rational& rhs)
{
num = (num * rhs.denom) - (rhs.num * denom);
denom *= rhs.denom;
return *this;
}

Rational operator-(const Rational& lhs, const Rational& rhs)
{
Rational diff(lhs);
diff -= rhs;
return diff;
}

Rational& Rational::operator*=(const Rational& rhs)
{
num *= rhs.num;
denom *= rhs.denom;
return *this;
}

Rational operator*(const Rational& lhs, const Rational& rhs)
{
Rational prod(lhs);
prod *= rhs;
return prod;
}

Rational& Rational::operator/=(const Rational& rhs)
{
num *= rhs.denom;
denom *= rhs.num;
return *this;
}

Rational operator/(const Rational& lhs, const Rational& rhs)
{
Rational quot(lhs);
quot /= rhs;
return quot;
}

bool operator==(const Rational& lhs, const Rational& rhs)
{
lhs.num == rhs.num;
lhs.denom == rhs.denom;
return true;
}

/*Rational operator==(const Rational& lhs, const Rational& rhs)
{
Rational same(lhs);
same == rhs;
return same;
}
*/
//**********HELPER FUNCTIONS************
//function find gcd for proper reduction
int Rational::gcd(int x, int y)
{
for(int i = min(x, y); i > 1; --i)
{
if((x % i == 0) && (y % i == 0))
return i;
}
return 0;
}
//funtion that simplifies result
void Rational::reduce()
{
int factor = gcd(num, denom);

if(factor != 0)
{
num /= factor;
denom /= factor;
}
}
//Initiliazing function
void Rational::init()
{
num = denom = 0;
}
// Copy a Rational number into this Rational object.
void Rational::copy(const Rational& r)
{
num	 = r.num;
denom = r.denom;
}

```

```/********************************
* main.cpp
* Luis Silva
*
* main file to run class and header
*************************************************************/
#include "rational_number.h"

using namespace std;

int main()
{
//creating objects for each fraction
Rational a, b, c;
//allowing input
cout << "\tFraction One\n\n";
cin >> a;

cout << "\tFraction Two\n\n";
cin >> b;

//computation of fractions
c = a + b;
cout << "The result from their sum is: " << c << endl;
c = a - b;
cout << "The result from their substration is: " << c << endl;
c = a * b;
cout << "The result from their multiplication is: " << c << endl;
c = a / b;
cout << "The result from their division is: " << c << endl;

if (!(a == B)/>)
cout << "Both fractions are the same " << endl;
else
cout << "Both fractions are not the same " << endl;

}

```