I remember doing a class to handle rational numbers in one of my classes way back when. I decided to do it again. What I'm having trouble understanding right now is why do I even create a class when my overloaded functions are all friend functions? As I understand it, in order to be able to do this fractionA + fractionB, you must use a non-member function. So that meant to me, a friend function. Am I wrong in thinking this? Non-static member functions take one argument and the left argument must be an object of the class that the function is a member of. With that said, am I able to use a non-static member function and still use something like this "fractionA + fractionB"? I'm not sure how I can. Here is what my class looks like. Looking at it, it just seems strange to me to define a class and have all those friend functions since they aren't member functions of the class.

#ifndef RATIONAL_H
#define RATIONAL_H

#include <iostream>
class Rational
{
public:
    Rational();
    Rational(int, int);
    ~Rational();

    // overloaded operators
    friend std::ostream &operator<<(std::ostream &, const Rational &);
    friend std::istream &operator>>(std::istream &, Rational &);

    //Rational operator+(Rational);
    friend Rational operator+(Rational, Rational);
    friend Rational operator-(Rational, Rational);
    friend Rational operator*(Rational, Rational);

private:
    int numerator;
    int denominator;

    // helper functions
    void simplify();
    friend int GCD(int, int);
    friend int LCM(int, int);
};
#endif

I apologize. I just realized I posted code for this 5 years ago and I somehow answered my own question ... 5 years ago. >.>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.