Help with using overloading operators

This is a discussion on Help with using overloading operators within the C++ Programming forums, part of the General Programming Boards category; Hi, I am new to C++, verrry new. I have an assignment due for our class and I dont understand ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    1

    Help with using overloading operators

    Hi, I am new to C++, verrry new. I have an assignment due for our class and I dont understand what we are supposed to do with it. Maybe someone can help me understand it or possibly point me in the right direction. Noo I am not looking for someone to take me to the destination point, but possible draw me a map on how to get there??? Thanks, I would appreciate it very much.

    I have to modify the Fraction.h and Fraction.cpp files so that the program implements the overloaded operators *, /, -,>, and <.

    I have to use the following program to test the new fraction class and I cant modify this program, because this is what they will be grading me on:

    Code:
    #include <iostream>
    #include "Fraction.h"
    
    int main()
    {
      Fraction a, b , c, d, e, f;
      cout << "Enter 2 fractions\n";
      cin >> b >> c;
      a = b * c;
      d = b / c;
      e = b - c;
      cout << "After multiply " << a << endl;
      cout << "After divide " << d << endl;
      cout << "After subtract " << e << endl;
      if (b < c)
      {
        cout << b << " is less than " << c << "\n";
      }
      else
      {
        cout << b << " is not less than " << c << "\n";
      }
      if (b > c)
      {
        cout << b << " is greater than " << c << "\n";
      }
      else
      {
        cout << b << " is not greater than " << c << "\n";
      }
      return 0;
    }
    Fraction.h file

    Code:
    #include <iostream>
    using namespace std;
    
    class Fraction
    {
      private:
        int top;
        int bottom;
        int gcd();
      public:
        Fraction(int, int);
        Fraction();
        void input(istream&);
        void output(ostream&);
        void add(Fraction, Fraction);
    };
    Fraction.cpp file
    Code:
    #include "Fraction.h"
    
    Fraction::Fraction()
    {
      top = 0;
      bottom = 1;
    }
    
    Fraction::Fraction(int a, int b)
    {
       top = a;
       if (b != 0)
         bottom = b;
       else
         bottom = 1;
    }
    
    
    void Fraction::input(istream& in)
    {
      int a, b;
      char slash;
      in >> a >> slash >> b;
      if (slash != '/' || b == 0)
      {
        top = 1; bottom = 1;
      }
      else
      {
        top = a; bottom = b;
      }
    }
        
    void Fraction::output(ostream& out)
    {
      out << top << "/" << bottom;
    }
     
       
    void Fraction::add(Fraction a, Fraction b)
    {
       bottom = a.bottom * b.bottom;
       top = a.top * b.bottom + b.top * a.bottom;
       int x = gcd();
       bottom = bottom / x;
       top = top / x;
    }
    
    
    int Fraction::gcd()
    {
       int x = top;
       while ((top % x != 0) || (bottom % x != 0))
       {
          x--;
       }
       return x;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I have to modify the Fraction.h and Fraction.cpp files so that the program implements the overloaded operators *, /, -,>, and <
    The program won't work too well without operators >> and << as well.

    >Maybe someone can help me understand it or possibly point me in the right direction.
    If you understand the operations and can do them on paper, writing the code is simple. It might look like this:
    Code:
    Fraction operator* ( const Fraction& lhs, const Fraction& rhs )
    {
      // Perform the operation
    }
    
    Fraction operator/ ( const Fraction& lhs, const Fraction& rhs )
    {
      // Perform the operation
    }
    
    Fraction operator- ( const Fraction& lhs, const Fraction& rhs )
    {
      // Perform the operation
    }
    
    bool operator> ( const Fraction& lhs, const Fraction& rhs )
    {
      // Perform the operation
    }
    
    bool operator< ( const Fraction& lhs, const Fraction& rhs )
    {
      // Perform the operation
    }
    Note that these are all binary operators and should not be class members. If they need access to the private members of Fraction, you can declare them as friends inside of Fraction's class declaration.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about overloading operators
    By *DEAD* in forum C++ Programming
    Replies: 9
    Last Post: 05-08-2008, 10:27 AM
  2. Replies: 16
    Last Post: 10-27-2007, 12:42 PM
  3. Overloading fstream's << and >> operators
    By VirtualAce in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2007, 03:17 AM
  4. operators overloading
    By waqasriazpk in forum C++ Programming
    Replies: 1
    Last Post: 07-26-2002, 01:05 AM
  5. Overloading operators...
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2001, 07:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21