I have had a lot of problems with C++ and I am quite confused. Here is the code so far;

#include <iostream.h>
#include <stdlib.h>
#include <cstring>
#include <cctype>

class Fraction
{
      private:
              Fraction ();
              Fraction (double);
              Fraction (Fraction &);
      public:
             double operMultiplication ();
             double operSum ();
             double operDouble ();
             double operEqual ();
             double operSet ();
             double getNumerator ();
             void setNumerator (double numerator);
             double getDenominator ();
             void setDenominator (double denominator);
};



using namespace std;

int main(int argc, char *argv[])
{
    system("PAUSE");
    return EXIT_SUCCESS;
}

Here is what I need to do, from what code I have done does it look like im on the right track and if not what should I change?;

Name the class "Fraction". Create three constructors for the class. The first constructor (the default constructor) will assign 1 for the numerator and 1 for the denominator. Create an overloaded constructor that takes two integer arguments. This argument will allow the class user to specify the numerator and the denominator. Finally, you must create a copy constructor.

The class must have a private data members for the numerator and the denominator. You may add as many other private variables as necessary to implement the remainder of the assignment. You should not have any public variables unless they are constants. Do not allow the numerator to be less than one. Do not allow the denominator to be less than one.

The class must also have the following functions. These functions should all be public.

operator* - Return a new Fraction object that is the product of two existing Fraction objects.

operator+ - Return a new Fraction object that is the sum of two existing Fraction objects.

operator double - Return the value of the Fraction object as a double. Divide the numerator by the denominator to get this result.

operator= - Assign the value of one Fraction object to another Fraction object.

operator== - Compare the values of each data member in one object to the respective value of the data members in a second object. The objects are equal if the respective data elements are equal. Return true if the objects are equal. Return false if they are not equal.

getNumerator - Return the class numerator.

getDenominator - Return the class denominator.

setNumerator - Receive one integer argument and update the class numerator.

setDenominator - Receive one integer argument and update the class denominator if the argument isn't zero. Default to one if the argument is zero.

You do not need a destructor for this class.

Do not use any inline functions.

Hi,

There are some problems with your code:
1.

Create an overloaded constructor that takes two integer arguments. This argument will allow the class user to specify the numerator and the denominator

You should use Fraction (int numerator, int denominator);

2.

Fraction ();
Fraction (double);
Fraction (Fraction &);

These should all be "public" member functions. Otherwise you won't be able to create a Fraction object!!

3. You need to add private variables:

private:
     int numerator;
     int denominator;

The rest is a bit more complicated, but I need to go now.

Hope this helps.

Edited 6 Years Ago by CppFTW: n/a

#include <iostream>
using namespace std;

template <typename C> class fraction
  {
    C numerator;
    C denominator;
    C res;

    public:
      fraction () {}
      fraction (const fraction& f)
        {
          numerator=f.numerator;
          denominator=f.denominator;
          res=f.res;
        }
      fraction (C n,C d) : numerator(n),denominator(d)
        {
          if ((n/d)>=1) res=numerator/denominator;          
        }
      friend ostream& operator<< (ostream& o,fraction f) {return o<<f;}
      friend bool operator== (fraction f,fraction g) {return f==g;}
      friend C operator* (fraction f,fraction g) {return f.res*g.res;}
      friend C operator+ (fraction f,fraction g) {return f.res+g.res;}

      C GetDenum () {return denominator;}
      C GetNum () {return numerator;}
      void SetNum (C c) {numerator=c;}
    
  };

int main ()
  {    
    fraction<float> x(21,3);
    fraction<float> y(6,2);
    cout << "A*B: " << x*y << endl;
    cout << "A+B: " << x+y << endl;               
  }

You'll get the idea ;)

I think they want him to use integers only.
Also, 'res' is kind of like cheating. You need to hold the numerator and denominator for real.

You have to store the result somewhere though. Programming means "cheating" most of the time if you ask me .You are cheating limitations.

Well you could just calculate it on the fly with float(numerator)/float(denominator), but maybe I am missing something.

Whatever, looks like the OP is not coming back (probably assignment was due today morning or something...)

I don't know why a teacher would ask for a class that constructs fractions but i suppose he wasn't very inspired.Anyway i would appreciate if you could share your idea about someting like this.I'm really interested if there is a way to this in a more elegant fashion.

Okay, here is my plan.

private:
    int numerator;
    int denominator;
public:
    float getResult()
    {
          return float(numerator)/float(denominator);
    }
    void multiplyIn(const Fraction &rhs)
    {
        this->numerator = this->numerator * rhs.numerator;
        this->denominator= this->denominator* rhs.denominator;
        Simplify();
    }

etc.
etc.

Edited 6 Years Ago by CppFTW: n/a

Yes but he said he needed overloaded operators and a constructor for initializing the operands.

I've gotten to here, but this may be wrong;

#include <iostream.h>
#include <stdlib.h>
#include <cstring>
#include <cctype>
class Fraction
{
      private:
              int numerator;
              int denominator;
      public:
             Fraction ();
             Fraction (int numerator, int denominator);
             Fraction (Fraction &);
             int operator* ();
             int operator+ ();
             operator double ();
             int operator= (Fraction &);
             int operator== (Fraction &);
             int getNumerator ();
             void setNumerator (int numerator);
             int getDenominator ();
             void setDenominator (int denominator);
};
Fraction::Fraction (int numerator, int denominator) {
 setNumerator(99999999);
 setDenominator(99999999);
 }
void Fraction::setNumerator(int numeratorArg){
 if(numeratorArg > 0){
 numerator = numeratorArg;
 }
}
int Fraction::getNumerator(){
return numerator;
}
void Fraction::setDenominator(int denominatorArg){
 if(denominatorArg > 0){
 denominator = denominatorArg;
 }
}
int Fraction::getDenominator(){
return denominator;
}
int Fraction::operator*(){
    if (numerator > 0 && denominator > 0)
        return numerator/denominator;
    else
        cout << "invalid input" << endl;

using namespace std;
int main(int argc, char *argv[])
{
    double Denominator1;
    double Numerator1;
    double Denominator2;
    double Numerator2;

    cout << "Enter an integer for the numerator: ";
    cin >> Numerator1;
    cout << "Enter an integer for the denominator: ";
    cin >> Denominator1;
    cout << "Enter an integer for the numerator: ";
    cin >> Numerator2;
    cout << "Enter an integer for the denominator: ";
    cin >> Denominator2;
    cout << Numerator1 << "/" << Denominator1 << endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

The declaration of the * operator is a bit off. * is presumably being overloaded as the multiplication operator which takes two parameters, one on the right hand side and one on the left hand side operator and frequently returns a result (else why bother to do the process in the first place).

The operator can be declared two ways. The first way is as a trure class method where the right hand operator is the object itself and the left hand operand is passed to the operator. It could look something like this:

Fraction Fraction::operator *(const Fraction lhs)
{
    Fraction result;
    result = num * lhs.num;
    result = dem * lhs.den;
    return result;
}

This version is similar to declaring a function with a name, like multiply(), instead of using an operator. The named function could look something like this:

Fraction multiply(const Fraction * lhs);

The definition of mulitply would be exactly the same as the definition for the single parameter overloaded * operator described above.

The other vesion is declared as a friend function and is passed both the right and left hand operands, as has been posted in one of the eralier post. It's declaration could look something like this:

friend Fraction operator *(const Fraction rhs, const Fraction lhs);

I'll let you work out the details of both versions to meet your needs, such as using private member variables with accessor member functions as opposed to using public member variables such as I've utilized, etc.

Edited 6 Years Ago by Lerner: n/a

This article has been dead for over six months. Start a new discussion instead.