Solved

class rational

Posted on 2004-08-07
4
562 Views
Last Modified: 2012-05-05
Can you comment on the effectiveness of this code?  I tried to create a class for rational numbers with overloaded operators which apply to my rational type class?  This is a homework assignment, but I wanted to make sure it looked ok.

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);  
};


//Constructors

Rational::Rational(int t, int n): teller(t), number(n) {
     reduce();
}

Rational Rational::operator -() const {
     return Rational(-teller, number);
}


// operator overloading

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& Rational::operator+= (const Rational& rVal) {
     teller = teller * rVal.number + number * rVal.teller;
     number = number * rVal.number;

     reduce();
     return *this;
}

Rational& Rational::operator-= (const Rational& rVal) {
     this->operator+=(-rVal);
     reduce();
     return *this;
}

Rational& Rational::operator*= (const Rational& rVal) {
     teller = teller * rVal.teller;
     number = number * rVal.number;

     reduce();
     return *this;
}


Rational& 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;
     is >> t >> deel >> n;
     rat = Rational(t,n);
     return is;
}


int main() {
     clrscr();
     Rational b1;
     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;
}
0
Comment
Question by:rumi54
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11743145
Everything seems to be ok, although I don't find Rational::reduce() implementation.
0
 

Author Comment

by:rumi54
ID: 11743155
The GCF function...
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 11743264
LOL. Yes I know what means but maybe you can optimize something there.
Your comparison (<, >, ==, etc.) operators could be "inline" to enhance performance.
Your division operators have not "divide by zero" exception management.
0
 

Author Comment

by:rumi54
ID: 11743283
I figured you know what it means.  :)  I thought I left it out completely.  Thanks.  
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

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

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

Join & Ask a Question