Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

# Rational Number w/Operator Overload

Posted on 2005-05-01
Medium Priority
1,194 Views
>>This code does not compile, please advise.  Del

*************************code**************************

#ifndef RAT_H
#define RAT_H

class Rational {

public:

Rational(int t = 0, int n = 1);

friend Rational operator + (const Rational& lVal, const Rational& rVal);
friend Rational operator - (const Rational& lVal, const Rational& rVal);
friend Rational operator * (const Rational& lVal, const Rational& rVal);
friend Rational operator / (const Rational& lVal, const Rational& rVal);

Rational& operator += (const Rational& rVal);
Rational& operator -= (const Rational& rVal);
Rational& operator *= (const Rational& rVal);
Rational& operator /= (const Rational& rVal);

friend int operator <  (const Rational& lVal, const Rational& rVal);
friend int operator <= (const Rational& lVal, const Rational& rVal);
friend int operator == (const Rational& lVal, const Rational& rVal);
friend int operator != (const Rational& lVal, const Rational& rVal);
friend int operator >= (const Rational& lVal, const Rational& rVal);
friend int operator >  (const Rational& lVal, const Rational& rVal);

friend ostream& operator << (ostream& os, const Rational& rat);
friend istream& operator >> (istream& is, Rational& rat);

private:

int teller, number;
void reduce();
int  number(int, int);

};

Rational::Rational(int t, int n): teller(t), number(n) {

reduce();
}

Rational Rational::operator -() const {

return Rational(-teller, number);
}

Rational operator + (const Rational& lVal, const Rational& rVal) {

Rational result;
result.teller = lVal.teller * rVal.number + lVal.number * rVal.teller;
result.number = lVal.number * rVal.number;
result.reduce();
return result;

}

Rational operator - (const Rational& lVal, const Rational& rVal) {

Rational result;
result = lVal +- rVal;
result.reduce();
return result;

}

Rational operator * (const Rational& lVal, const Rational& rVal) {

Rational result;
result.teller = lVal.teller * rVal.teller;
result.number = lVal.number * rVal.number;
result.reduce();
return result;

}

Rational operator / (const Rational& lVal, const Rational& rVal) {

Rational result;
result.teller = lVal.teller * rVal.number;
result.number = lVal.number * rVal.teller;
result.reduce();
return result;

}

Rational& operator += (const Rational& rVal) {

teller = teller * rVal.number + number * rVal.teller;
number = number * rVal.number;
reduce();
return *this;

}

Rational& operator -= (const Rational& rVal) {

this->operator += (-rVal);
reduce();
return *this;

}

Rational& operator *= (const Rational& rVal) {

teller = teller * rVal.teller;
number = number * rVal.number;
reduce();
return *this;

}

Rational& operator /= (const Rational& rVal) {

teller = teller * rVal.number;
number = number * rVal.teller;
reduce();
return *this;

}

int operator <  (const Rational& lVal, const Rational& rVal) {

return(lVal.toDouble() < rVal.toDouble());

}

int operator <= (const Rational& lVal, const Rational& rVal) {

return(lVal < rVal || lVal == rVal);

}

int operator == (const Rational& lVal, const Rational& rVal) {

return lVal.teller * rVal.number == lVal.number * rVal.teller;

}

int operator != (const Rational& lVal, const Rational& rVal) {

return !(lVal == rVal);

}

int operator >= (const Rational& lVal, const Rational& rVal) {

return(lVal > rVal || lVal == rVal);

}

int operator > (const Rational& lVal, const Rational& rVal) {

return(lVal.toDouble() > rVal.toDouble());

}

istream& operator >> (istream& is, Rational& rat) {

int t, n;
char deel;
cin >> is >> t >> deel >> n;
rat = Rational(t,n);
return is;

}

#endif

********************

// main.cpp

#include "rat.h"

#include <iostream>
#include <cstring>
#include <cstdlib>

using namespace std;

int main() {

Rational r;
r.clrscr();
r.Rational b1;
r.Rational b2;

cout << "Input the number in the form (a/b): ";
cin >> b1 >> b2;

// Test for +, -, *, /
cout << b1 << " + " << b2 << " = " << b1 + b2 << endl;
cout << b1 << " - " << b2 << " = " << b1 - b2 << endl;
cout << b1 << " * " << b2 << " = " << b1 * b2 << endl;
cout << b1 << " / " << b2 << " = " << b1 / b2 << endl;

// test program for overloaded operators
cout << b1 << " <  " << b2;
(b1 <  b2) ? cout << " [X]" << endl : cout << " [ ]" << endl;

cout << b1 << " <= " << b2;
(b1 <= b2) ? cout << " [X]" << endl << endl : cout << " [ ]" << endl << endl;

cout << b1 << " == " << b2;
(b1 == b2) ? cout << " [X]" << endl : cout << " [ ]" << endl;

cout << b1 << " != " << b2;
(b1 != b2) ? cout << " [X]" << endl << endl : cout << " [ ]" << endl << endl;

cout << b1 << " >= " << b2;
(b1 >= b2) ? cout << " [X]" << endl : cout << " [ ]" << endl;

cout << b1 << " >  " << b2;
(b1 >  b2) ? cout << " [X]" << endl : cout << " [ ]" << endl << endl;

getch();
return 0;

}

*************

output = multiple errors.
0
Question by:edelossantos

LVL 55

Assisted Solution

Jaime Olivares earned 1000 total points
ID: 13908557
Ok Del, here we go:
1) You have declared a data member 'number' and a function member 'number', change one
2) At Rational operator - (const Rational& lVal, const Rational& rVal)  implementation, this line is incorrect: result = lVal +- rVal; (I think must be result = lVal - rVal)
3) You are implementing a function you have not declared, unary minus operator: Rational Rational::operator -() const, declare it as:  Rational operator -() const;
4) Many operators as Rational& operator += (const Rational& rVal), must be declared as class function members, like:
5) At operator >>,  remove 'cin>>', it is not necessary
0

LVL 39

Accepted Solution

itsmeandnobodyelse earned 1000 total points
ID: 13908563
You may wonder why you don't get answers. I think most experts will share my opinion that your part of work wasn't done yet. When I compiled the source I got 61 errors. By adding

#include <iostream>
using namespace std;

to rat.h it was 38 errors. Actually, I think, you might have found that error yourself.

>>>> int number(int int);

That is the next compiler error, that simply says that you couldn't name a data member same as a function. But cause you don't need the function, you could remove that line ==> 37 errors.

>>>> Rational Rational::operator -() const {

Here, my compiler says that operator- isn't a member of class Rational and it is right. You have to add the prototype of that member function to the class definition ==> 33 errors

>>>>     Rational& operator += (const Rational& rVal) {
>>>>     Rational& operator -= (const Rational& rVal)  {
>>>>     Rational& operator *= (const Rational& rVal) {
>>>>     Rational& operator /= (const Rational& rVal)  {

These are member functions but you didn't define the class scope Rational:: prior to the function name, e. g.

Rational& Rational::operator += (const Rational& rVal) {

==> 13 errors

>>>> return(lVal.toDouble() < rVal.toDouble());

member toDouble wasn't defined yet. Add

double toDouble() const { return ((double)teller) / number; }

to class definition ==> 9 errors

>>>>   cin >> is >> t >> deel >> n;

remove cin >>  (?????) and it compiles... ==> 8 errors

>>>>   Rational r;
>>>>  r.clrscr();
>>>>  r.Rational b1;
>>>>  r.Rational b2;

change it to

Rational r;
// r.clrscr();     // clrscr *never* should be a member of class Rational
Rational b1;
Rational b2;

==> 1 error

>>>>   getch();

You would need old <conio.h> for getch() but you also could do that:

char dummy;
cin >> dummy;

VoilĂ : 0 compiler errors, but two linker errors cause of missing operator<< und reduce function.

Regards, Alex

0

Author Comment

ID: 13911251
Alex and Jaime,

Thanks to the both of you.

>>You may wonder why you don't get answers. I think most experts will share my opinion that your part of work wasn't done yet. -> I will definately work harder on this.  Del.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.htâ€¦
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilationâ€¦
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
###### Suggested Courses
Course of the Month11 days, 22 hours left to enroll

#### 564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.