Thread: Overloading operators

  1. #1
    Registered User Shockey62's Avatar
    Join Date
    Apr 2012
    Location
    New Jersey
    Posts
    3

    Overloading operators

    Hello all,

    I'm new here and I've been looking for a forum board to help with some questions. I'm fairly new to C++ programming as I am a Freshman in College who has never programmed before College so it was a big jump. Anyways, I have this homework program assignment that I am a bit confused and could use a little guidance. If any information is missing to help let me know. Thank you in advance!


    This is from my textbook:
    This program involves making a class for rational numbers. I have to represent rational numbers as two values of type int, one for the numerator and one for the denominator.

    Include a constructor with two arguments that can be used to set the member variables of an object to any legitimate values. Also include a constructor that has only a single parameter of type int; call this single parameter whole_number and define the constructor so that the object will be initialized to the rational number whole_number/1. also include a default constructor that initializes and object to 0. (that is, to 0/1)

    Overload the input and output operators >> and <<. Numbers are to input and output in the forum 1/2, 25/32, 300/401 and so forth. Note that the numerator, denominator or both can have a minus sign. Overload all of the following operators so that they correctly apply to the type Rational: ==. <, <=, >, >=, +, -, * and /. Also write a test program to test the class.
    below is my code so far:
    Code:
    #include
    <iostream>
    using
    namespace std;
    class
     Rational {
    public:
    Rational();           //Default constructor
    Rational(int);            //Second constructor
    Rational(int, int);        //Third constructor
    void setData(int, int);
    int getNumer();            // accessor function
    int getDenom();            // accessor function
    Rational operator>>(Rational &);
    private: 
    int numerator;
    int denominator;
    };
    Rational::Rational()
    {
        numerator = 0; denominator = 1;
    }
    Rational::Rational(int whole_number)
    {
        whole_number/1;
    }
    Rational::Rational(int num, int den)
    {
        numerator = num; denominator = den;
    }
    void Rational::setData(int num, int den)
    {
        numerator = num; denominator = den;
    }
    istream& operator>>(istream &in, Rational &rationalObj)
    {
       int n, d;
        cout << "Enter numerator and denominator: ";
         in >> n >> d;
        rationalObj.setData(n, d);
        
        return in;
    }
    // external non-friend version; need two accessor functions
    ostream& operator<<(ostream &out, Rational &rationalObj)
    {
        out << rationalObj.getNumer() << "/" 
            << rationalObj.getDenom();
        
    return out;
    }
    intRational::getNumer()
    {
      return numerator;
    }
    intRational::getDenom()
    {
        return denominator;
    }
    int main()
    {
        Rational r1, r2, r3, r4;
        
     return 0;
    } 
    

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    [13] Operator overloading ..Updated!.., C++ FAQ
    I'm not sure what you want, since you've made no attempt at anything in bold.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User Shockey62's Avatar
    Join Date
    Apr 2012
    Location
    New Jersey
    Posts
    3
    Quote Originally Posted by Salem View Post
    [13] Operator overloading ..Updated!.., C++ FAQ
    I'm not sure what you want, since you've made no attempt at anything in bold.
    Sorry about that. I just wanted to know if I had everything set up correctly so far. Here is my updated code with some of the overloaded operators. I don't really understand have to do the negatives. Am I doing this correctly?

    Thanks!

    Code:
    #include <iostream>
    using namespace std;
    
    class Rational {
    public:
     Rational();    //Default constructor
     Rational(int);   //Second constructor
     Rational(int, int);  //Third constructor
     void setData(int, int);
     int getNumer();   // accessor function
     int getDenom();   // accessor function
     Rational operator+(Rational &);
     Rational operator-(Rational &);
     Rational operator*(Rational &);
     Rational operator/(Rational &);
     void operator-();
    private:
     int numerator;
     int denominator;
    };
    Rational::Rational()
    {
     numerator = 0; denominator = 1;
    }
    Rational::Rational(int whole_number)
    {
     whole_number/1;
    }
    Rational::Rational(int num, int den)
    {
     numerator = num; denominator = den;
    }
    void Rational::setData(int num, int den)
    {
     numerator = num; denominator = den;
    }
    int Rational::getNumer()
    {
     return numerator;
    }
    int Rational::getDenom()
    {
     return denominator;
    }
    istream& operator>>(istream &in, Rational &rationalObj)
    {
     int n, d;
     cout << "Enter numerator and denominator: ";
     in >> n >> d;
    
     rationalObj.setData(n, d);
    
     return in;
    }
    // external non-friend version; need two accessor functions
    ostream& operator<<(ostream &out, Rational &rationalObj)
    {
     out << rationalObj.getNumer() << "/" << rationalObj.getDenom();
     return out;
    }
    Rational Rational::operator+(Rational &obj2)
    {
     Rational sum;
     sum.numerator = numerator + obj2.numerator;
     sum.denominator = denominator + obj2.denominator;
     return sum; 
    }
    Rational Rational::operator-(Rational &obj2)
    {
     Rational diff;
     diff.numerator = numerator - obj2.numerator;
     diff.denominator = denominator - obj2.denominator;
     return diff; 
    }
    Rational Rational::operator*(Rational &obj2)
    {
     Rational product;
     product.numerator = numerator * obj2.numerator;
     product.denominator = denominator * obj2.denominator;
     return product; 
    }
    Rational Rational::operator/(Rational &obj2)
    {
     Rational quotient;
     quotient.numerator = numerator * obj2.numerator;
     quotient.denominator = denominator * obj2.denominator;
     return quotient; 
    }
    void Rational::operator-()
    {
     numerator = -numerator;
     denominator = -denominator;
    }
    
    int main()
    {
     Rational r1(1,2), r2, r3, r4, r5, r6, r7(-5, 10), r8;
    
     r2.setData(10, 50);
     r3 = r1 + r2;
     cout << r1 << " + " << r2 << " = " << r3 << endl;
    
     cin >> r4;
     cout << r4;
     cout << endl;
    
     r5 = r1 * r4;
     cout << r1 << " * " << r4 << " = " << r5 << endl;
    
     r6 = r2 / r3;
     cout << r2 << " / " << r3 << " = " << r6 << endl;
    
     -r8 = -r7 + r2;
     cout << "r7 = " << r8 << endl;
    
     return 0;
    }
    Last edited by Shockey62; 04-19-2012 at 01:09 PM.

  4. #4
    Registered User Shockey62's Avatar
    Join Date
    Apr 2012
    Location
    New Jersey
    Posts
    3
    Sorry for the double post.. Here is my updated code with all of the operators that the questions asks for. I've ran the program a lot and it seems to work good. The only thing left I have to do is the Unary Operator and I don't really understand it. So if someone could point me in the right direction that would be awesome and if you see anything wrong with my code, feel free to share. I'm open to anything

    Thanks.

    Code:
    #include <iostream>
    using namespace std;
    
    class Rational {
    public:
     Rational();    //Default constructor
     Rational(int);   //Second constructor
     Rational(int, int);  //Third constructor
     void setData(int, int);
     int getNumer();   // accessor function
     int getDenom();   // accessor function
     Rational operator+(Rational &);
     Rational operator-(Rational &);
     Rational operator*(Rational &);
     Rational operator/(Rational &);
     bool operator==(Rational &);
     bool operator<(Rational &);
     bool operator>(Rational &);
     bool operator<=(Rational &);
     bool operator>=(Rational &);
     void operator-();
    private:
     int numerator;
     int denominator;
    };
    Rational::Rational()
    {
     numerator = 0; denominator = 1;
    }
    Rational::Rational(int whole_number)
    {
     whole_number/1;
    }
    Rational::Rational(int num, int den)
    {
     numerator = num; denominator = den;
    }
    void Rational::setData(int num, int den)
    {
     numerator = num; denominator = den;
    }
    int Rational::getNumer()
    {
     return numerator;
    }
    int Rational::getDenom()
    {
     return denominator;
    }
    istream& operator>>(istream &in, Rational &rationalObj)
    {
     int n, d;
     cout << "Enter numerator and denominator: ";
     in >> n >> d;
     rationalObj.setData(n, d);
     return in;
    }
    // external non-friend version; need two accessor functions
    ostream& operator<<(ostream &out, Rational &rationalObj)
    {
     out << rationalObj.getNumer() << "/" << rationalObj.getDenom();
     return out;
    }
    Rational Rational::operator+(Rational &obj2)
    {
     Rational sum;
     sum.numerator = numerator + obj2.numerator;
     sum.denominator = denominator + obj2.denominator;
     return sum; 
    }
    Rational Rational::operator-(Rational &obj2)
    {
     Rational diff;
     diff.numerator = numerator - obj2.numerator;
     diff.denominator = denominator - obj2.denominator;
     return diff; 
    }
    Rational Rational::operator*(Rational &obj2)
    {
     Rational product;
     product.numerator = numerator * obj2.numerator;
     product.denominator = denominator * obj2.denominator;
     return product; 
    }
    Rational Rational::operator/(Rational &obj2)
    {
     Rational quotient;
     quotient.numerator = numerator * obj2.numerator;
     quotient.denominator = denominator * obj2.denominator;
     return quotient; 
    }
    bool Rational::operator==(Rational &obj2)
    {
     return (numerator == obj2.numerator);
     return (denominator == obj2.denominator);
    }
    bool Rational::operator<(Rational &obj2)
    {
     return (numerator < obj2.numerator);
     return (denominator < obj2.denominator);
    }
    bool Rational::operator>(Rational &obj2)
    {
     return (numerator > obj2.numerator);
     return (denominator > obj2.denominator);
    }
    bool Rational::operator<=(Rational &obj2)
    {
     return (numerator <= obj2.numerator);
     return (denominator <= obj2.denominator);
    }
    bool Rational::operator>=(Rational &obj2)
    {
     return (numerator >= obj2.numerator);
     return (denominator >= obj2.denominator);
    }
    void Rational::operator-()
    {
     numerator = -numerator;
     denominator = -denominator;
    }
    
    int main()
    {
     Rational r1(1,2), r2, r3, r4, r5, r6, r7(-5, 10), r8;
    
     r2.setData(10, 50);
     r3 = r1 + r2;
     cout << r1 << " + " << r2 << " = " << r3 << endl << endl;
    
     cin >> r4;
     cout << r4;
     cout << endl << endl;
    
     r5 = r1 * r4;
     cout << r1 << " * " << r4 << " = " << r5 << endl << endl;
    
     r6 = r2 / r3;
     cout << r2 << " / " << r3 << " = " << r6 << endl << endl;
    
     if (r1 == r2)
      cout << r1 << " = " << r2 << endl;
     else
      cout << r1 << " does not equal " << r2 << endl;
     cout << endl;
     if (r2 < r4)
      cout << r2 << " < " << r4 << endl;
     else
      cout << r4 << " is not greater than " << r2 << endl;
     cout << endl;
     if(r1 > r6)
      cout << r1 << " > " << r6 << endl;
     else
      cout << r1 << " is not less than " << r6 << endl;
     cout << endl;
    
     if(r2 <= r2)
      cout << r2 << " <= " << r2 << endl;
     else
      cout << r2 << " is not greater than or equal to " << r2 << endl;
     cout << endl;
     if(r2 >= r4)
      cout << r2 << " >= " << r4 << endl;
     else
      cout << r4 << " is not less or equal to " << r2 << endl;
     cout << endl;
    
     -r8 = -r7 + r2;
     cout << "r7 = " << r8 << endl;
    
     cout << endl;
     return 0;
    }

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Hmm, there are quite a few mistakes.
    This bit of code for example, would make 1/3 + 2/3 equal 3/6, when clearly it should be 3/3, or 1/1 assuming you simplify fractions (hint: look up the GCD algorithm!), which you are probably meant to:
    Code:
     sum.numerator = numerator + obj2.numerator;
     sum.denominator = denominator + obj2.denominator;
    Your comparison operators aren't right either. You can't have two return statements one after the other as it leaves the function at the first one and can never reach the second one. Turn up your compiler warnings and pay attention to them because they will point out that kind of obvious mistake.

    Your unary minus function should not have a void return type. Instead of modifying itself, it should return a new fraction that is the negative of itself.

    Read up and learn about "const correctness".
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    Rational::Rational(int whole_number)
    {
     whole_number/1;
    }
    ????





    Tip: Since you've got the constructor for it, code like this:
    Code:
    Rational Rational::operator+(Rational &obj2)
    {
     Rational sum;
     sum.numerator = numerator + obj2.numerator;
     sum.denominator = denominator + obj2.denominator;
     return sum;
    }
    ...can be shortened to this:
    Code:
    Rational Rational::operator+(Rational &obj2)
    {
     return Rational(numerator + obj2.numerator,denominator + obj2.denominator);
    }




    I've been guilty of it too on occasion but I still have a problem with overloaded stream extraction operators (operator>>) that perform output (cout << for example). Don't know about other people's thoughts on the matter though.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading operators
    By Niels_M in forum C++ Programming
    Replies: 40
    Last Post: 09-13-2010, 01:23 PM
  2. Overloading operators
    By ugmusicbiz in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2009, 01:41 PM
  3. Overloading Operators
    By Zoalord in forum C++ Programming
    Replies: 6
    Last Post: 07-16-2003, 09:08 AM
  4. Overloading the << and >> operators
    By WebmasterMattD in forum C++ Programming
    Replies: 9
    Last Post: 10-15-2002, 05:22 PM
  5. Overloading Operators
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 08-19-2002, 02:23 PM