Here is my coding, I know its sad, but I am lost.

After reading my code below and the guidelines I just have a few questions:

1. Am I on the right track so far?
2. To get the fractions should I put a cin in the setter functions, if not how should I go about getting the fractions, in main?
3.How should I declare the operators, as I'm not 100% sure?
4.Where should I put the coding to prompt the user to choose multiplication or addition?
5. How would I do the last two steps, the comparing to see if the two fractions are equal?

#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();
 setDenominator();
 }
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;
}
using namespace std;
int main(int argc, char *argv[])
{
    system("PAUSE");
    return EXIT_SUCCESS;
}

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.

You may not use any global variables except symbolic constants.

Try to modularize your code through the use of functions. Your program must have a minimum of 3 functions (not including main) but may have more. This does not include the functions that are members of the Fraction class. One function must accept a pointer to a Fraction object and this function must use the pointer to update the object.

The application (client) code should prompt the user to enter the numerator and denominator for the first Fraction object. Prompt the user to enter the numerator and denominator for the second Fraction object. Prompt the user to enter + or * to indicate if they want the fractions added or multiplied. Perform the desired operation based on the user input and print the result. Print the result using the get functions to retrieve the class data. You do not need to reduce the fraction. That is a result of 3/6 doesn't have to be reduced to 1/2.

After completing the previous step, ask the user if he/she wants to continue. Repeat the previous step if the user enters Y or y. Stop the application if the user enters anything else.


At the end of the program (just before quitting), create another copy of the Fraction object using the copy constructor. Then call the overloaded == operator to determine if the two objects are equal. Print an appropriate message indicating whether the objects are equal or not. Print the original Fraction object and the new Fraction object using the operator double function. Print the double value to three decimal positions.

At the end of the program (just before quitting), operator= function to assign one of your existing Fraction objects to the new Fraction object created in the previous step. Call the overloaded == operator to determine if the two objects are equal. Print an appropriate message indicating whether the objects are equal or not. Then use a class set function to change one of the Fraction objects. Compare the objects again and print an appropriate message indicating whether the objects are equal or not.

I know this seems like a lot, but i'm not asking for you to code it for me, but any examples, templates, or optionally if your really nice some code would be deeply appreciated

And by the way for your help if you really want I am quite good at English and would be willing to proofread a paper or anything else english related or help you with any math homework up to Calculus 1.

well for starters your constructor on line 24 is not right. you are calling the set functions but not passing anything into them. you should just initialize the member variables right in the constructor instead of calling your set functions.

second you set numerator function will not allow 0 as a numerator. why is that? zero is a perfectly legal numerator to have.

third just as a matter of good coding practice you get functions should be const.

lastly for your operator == function you will either need to write a reduction routine in it or better yet write a reduce function that is a member of you class. after that you need to reduce both fractions to there simplest terms then compare the numerators and denominators to see if there equal.

Well it seems like you're begging.. was the code in the other post not enough to give you an idea ? "Some code would be deeply appreciated" ?You have to be kidding .. three of us shew you three different opinions. Show some wits and we'll usher you to the end of the project.

I a way your right I wont deny that I sound desperate, and in all honesty I sort of am. Between tomorrow and Thursday I have 5 finals and I am so overwhelmed. I took this class thinking it'd be a fun and easy elective, boy was I wrong. Between Statistics, Calc 1, Chem 2, C++, and English 2, I have a lot of things to do and this program is consuming so much more time than I can really put into it. It is worth quite a lot of points, and if I don't complete it I could fail the class thus possibly lose my scholarship. And no I'm not trying to make you feel pity for me, but just understand I'm not trying to be lazy I just dont have much time to do this in between studying and I'm freaking out about not getting this done.

So .. point your problems specifically .. enum them :) and i'll try to help with each one , that's if you don't want the whole program which i think it sounds as an alternative from what you said about your finals. Go ahead.. be specific , i'll be up for an hour or so.

First off I think I set up the class correctly.

Second my issue at the moment is declaring the operator* and printing it. If I could figure out how to declare it right and then print it I think I could figure out how to do the addition.

The declaration of the getters and setters seems right, but i'm not sure.

#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 numerator){
 if(numerator > 0){
 this -> numerator = numerator;
 }
}
int Fraction::getNumerator(){
return numerator;
}
void Fraction::setDenominator(int denominator){
 if(denominator > 0){
 this -> denominator = denominator;
 }
}
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[])
{
    cout << "Fraction: " << operator*() << endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
}

There are two ways:

float operator* (const fraction& f) { return (num/denum) * (f.num/f.denum); }

or

friend float operator* (const fraction one,const fraction two)
  {
    return (one.num/one.denum) * (two.num/two.denum);
  }

Note that you should use floats or doubles.

ok so the one.num and one.denum, do those need to be declared above?
Also the fraction one and two, where should I declare them and where should I accept them. Should I do a series of cin in main to get the numerator and denominator, then take the two fractions and multiplst totally off on this one?

#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 &);
             float operator* (const fraction one,const fraction two);
             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 numerator){
 if(numerator > 0){
 this -> numerator = numerator;
 }
}
int Fraction::getNumerator(){
return numerator;
}
void Fraction::setDenominator(int denominator){
 if(denominator > 0){
 this -> denominator = denominator;
 }
}
int Fraction::getDenominator(){
return denominator;
}
friend float operator* (const fraction one,const fraction two)
{
return (one.numerator/one.denominator) * (two.numerator/two.denominator);
}

This is me adding your suggestion, but its wrong

would

cout << "Fraction: " << operator* << endl;

in main print or is it far more complicated.

I meant to say I did it wrong not that you were wrong

No you shoud do something like:

float num;
float denum;
cin >> num >> denum;
fraction a(num,denum);
fraction b(num,denum);
cout << "Fraction " << a << " * " << "Fraction " << b << " equals " << a*b;

it's pretty simple

1. Am I on the right track so far?

It almost seems schizophrenic. You have many good features and then you don't bother to read the instructions where it clearly says the math operators return a Fraction, not int.

2. To get the fractions should I put a cin in the setter functions, if not how should I go about getting the fractions, in main?

I would get the information in free standing int variables in main() and then use them as parameter in the set() functions or use them as parameters in the nondefault constructor.

3.How should I declare the operators, as I'm not 100% sure?

See your previous post. The options are reviewed there.

4.Where should I put the coding to prompt the user to choose multiplication or addition?

In main() is fine, within a loop, as described in the instructions.

5. How would I do the last two steps, the comparing to see if the two fractions are equal?

The process of deciding equality is explained in the instructions. As per the instructions two fractions are equal in this program if both numerator and denominator are equal.

Fraction B;
B = A; 
if(A == B) //check if A equals B and display appropriate message.
  cout << "A equals B";
else
  cout << "A is not equal B";

A.setDenominator(999999); //change A by giving it a new denominator
//now repeat the protocol as above to test the equality operator for inequality as opposed to equality

Edit: Chiming in a bit late I see. Oh well. Questions posed answered in any event.

Alright Im lost, tired and confused. I'm just gonna call it quits for now I have other classes to study for. I appreciate all the help and hopefully I'll finish it in time. At this point I'm so tired of this class I dont care anymore. I guess if worst comes to worst I'll just take summer classes to boost my gpa. Thank you guys and have a good night

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.