I have to create a program that uses a class that adds, subtracts, multiplies, and divides fractions....and then reduces them....I did the program to make sure calculations worked and they did....I just began trying to reduce the fraction but I'm stomped. I have many errors concerning my get and set functions..could someone help please....

header file

#include <iostream>
using namespace std;

class FrAction
{
		private:
		int num, den, j, k, numer, denom, x, y;

      public:
		FrAction (int numerator, int denomenator)
		{
				num= numerator;
				den=denomenator;
		}

		FrAction operator+(FrAction f)
		{
				FrAction temp(num * f.den + f.num * den, den * f.den);

				return temp;
		}

		FrAction operator-(FrAction f)
		{
				FrAction temp(num * f.den - f.num * den, den * f.den);

				return temp;
		}

		FrAction operator*(FrAction f)
		{
				FrAction temp((num * f.num),(den * f.den));

				return temp;
		}

		FrAction operator/(FrAction f)
		{
				FrAction temp((num * f.den), (f.num * den));

				return temp;
		}
		
		FrAction reduce()
		{
			x = getNumerator();
			int a;
			x = a;
			y = getDenominator();
			int b;
			y = b;



			int i;
			while (i = (a % b))
			{
				a = b;
				b = i;
			}
			x /= b;
			y /= b;

			return(x,y)

		}

		void setNumerator(int num)
		{
			numer= num;
		}

		void setDenominator(int den)
		{
			denom=den;
		}

		getNumerator(int numer)
		{
			return numer;
		}

		getDenominator(int denom)
		{
			return denom;
		}

		void add_display()
		{			
			 if (den==1)
				 cout<<"These fractions added together equals "<<num<<endl;
				 
			 else
			 {
				 cout <<"These fractions added together equals "<<num<<'/'<<den<<endl;
				 setNumerator(num);
				 setDenominator (den);
				 cout<<"This fraction reduced is "<<reduce();
			 }
		}

		void sub_display()
		{
			
			 if (den==1)
				 cout<<"These fractions added together equals "<<num<<endl;
			 else
			    cout<<"These fractions subtracted together equals " <<num <<'/'<<den<<endl;
		}

		void mult_display()
		{
			if (den==1)
				 cout<<"These fractions added together equals "<<num<<endl;
			else
				 cout<<"These fractions multiplied together equals " <<num <<'/'<<den<<endl;
		}

		void div_display()
		{	
			if (den==1)
			    cout<<"These fractions added together equals "<<num<<endl;
			else
			    cout<<"These fractions divided together equals " <<num <<'/'<<den<<endl;
		}
};

Main file

#include <iostream>
#include "FrAction.h"

int main()
{
	int num1, dem1, num2, dem2;

	cout<<"Enter the numerator of fraction 1: ";
	cin>>num1;

	cout<<"Enter the denominator of fraction 1: ";
	cin>>dem1;

	FrAction fract(num1, dem1);

	cout<<"Enter the numerator of fraction 1: ";
	cin>>num2;

	cout<<"Enter the denominator of fraction 1: ";
	cin>>dem2;

	FrAction fract2(num2, dem2);

	FrAction fract3_add(fract + fract2);

	fract3_add.add_display();

	FrAction fract3_subtract(fract - fract2);

	fract3_subtract.sub_display();

	FrAction fract3_mult (fract * fract2);

	fract3_mult.mult_display();

	FrAction fract3_div(fract / fract2);

	fract3_div.div_display();

	return 0;
}

Recommended Answers

All 7 Replies

In reduce function, you are equating x and y to local uninitialized variables a and b which should give errors.

Set function looks ok, but in get you are returning parameter values instead of actual numerator and denominator values.

I have not checked the logic of your program.

In reduce function, you are equating x and y to local uninitialized variables a and b which should give errors.

Set function looks ok, but in get you are returning parameter values instead of actual numerator and denominator values.

I have not checked the logic of your program.

Ok how can i correct the get functions...always have trouble with these functions

Try it dude .. will give you a hint.
Why do you need to have parameters in get function? The variables with values already exists, just return them. If you still don't get it, feel free to ask.

Try it dude .. will give you a hint.
Why do you need to have parameters in get function? The variables with values already exists, just return them. If you still don't get it, feel free to ask.

Thanks about to try it!

Well I don't think this is going to be that welcome, so I sorry.

First off I am not 100% you understand why you want to encapsulate something in a class. In this case, you want a fraction type, that behaves as if it is a standard type, e.g. a double etc.

So first consider those variables that are common to all methods of the class and represent the state of the class object. In this case there are only really two, the numerator and denominator. Therefore these are the only two variables that should be in your class definition. e.g.

class Fraction
{
  private:
    int num;
    int den;
  public:
};

Now those variables are global to the class, and specific for each instance of the class you make. e.g. Fraction A(3,4), B(7,8); , if you call a method, e.g. B.add_display(), then in the method there are two variable num, den with values 7 and 8, but if you call A.add_display() the variables are 3 and 4. In particular you do not need to either explicitly redefine then or get them.

ALL other variables required for methods of the class, can be declared locally to the method. We have a code review saying at work, "constness, locality, type generality". Ignoring the last for this post, locality is where a variable is defined in the deepest scope possible to do its work, i.e. if you are swapping two variables, define the temp copy variable within the loop.

Now let us look at your getNumerator method. Here you are going to be hurt no thinking what you want to give the function and what you get back. If I ask you to count say the coins in your pocket, you don't ask me for a number. That is what you have done. You should write it like this:

int getnumberator() const { return num; }

Now note that you don't need to use the get/setters throughout the class, unless they get additional functionality, e.g. checking the input. You will also find a number of compile errors with your set functions, that you should be able to easily fix. But remember that if you define a local variable with a name that is ALREADY in use, the the variable to use is the local one. I suspect you are begin hurt by not
being very careful with you const again. e.g. the general form is normally

void setNumber(const int N) { number=N; }

Get this to compile [ and do it in pieces if need be ], then we will talk about your GCD algorithm errors. (you have the right algorithm but you have to fix your variable problems]

Best of luck with it.

Well I don't think this is going to be that welcome, so I sorry.

First off I am not 100% you understand why you want to encapsulate something in a class. In this case, you want a fraction type, that behaves as if it is a standard type, e.g. a double etc.

So first consider those variables that are common to all methods of the class and represent the state of the class object. In this case there are only really two, the numerator and denominator. Therefore these are the only two variables that should be in your class definition. e.g.

class Fraction
{
  private:
    int num;
    int den;
  public:
};

Now those variables are global to the class, and specific for each instance of the class you make. e.g. Fraction A(3,4), B(7,8); , if you call a method, e.g. B.add_display(), then in the method there are two variable num, den with values 7 and 8, but if you call A.add_display() the variables are 3 and 4. In particular you do not need to either explicitly redefine then or get them.

ALL other variables required for methods of the class, can be declared locally to the method. We have a code review saying at work, "constness, locality, type generality". Ignoring the last for this post, locality is where a variable is defined in the deepest scope possible to do its work, i.e. if you are swapping two variables, define the temp copy variable within the loop.

Now let us look at your getNumerator method. Here you are going to be hurt no thinking what you want to give the function and what you get back. If I ask you to count say the coins in your pocket, you don't ask me for a number. That is what you have done. You should write it like this:

int getnumberator() const { return num; }

Now note that you don't need to use the get/setters throughout the class, unless they get additional functionality, e.g. checking the input. You will also find a number of compile errors with your set functions, that you should be able to easily fix. But remember that if you define a local variable with a name that is ALREADY in use, the the variable to use is the local one. I suspect you are begin hurt by not
being very careful with you const again. e.g. the general form is normally

void setNumber(const int N) { number=N; }

Get this to compile [ and do it in pieces if need be ], then we will talk about your GCD algorithm errors. (you have the right algorithm but you have to fix your variable problems]

Best of luck with it.

I've redone the class and everything works but my reduce algorithm....

#include <iostream>
using namespace std;

class FrAction
{
		private:
		int num, den, j, k, numer, denom, x, y, a, b;
		

      public:
		FrAction (int numerator, int denomenator)
		{
				num= numerator;
				den=denomenator;
		}

		FrAction operator+(FrAction f)
		{
				FrAction temp(num + f.num, den);

				return temp;
		}

		FrAction operator-(FrAction f)
		{
				FrAction temp(num * f.den - f.num * den, den * f.den);

				return temp;
		}

		FrAction operator*(FrAction f)
		{
				FrAction temp((num * f.num),(den * f.den));

				return temp;
		}

		FrAction operator/(FrAction f)
		{
				FrAction temp((num * f.den), (f.num * den));

				return temp;
		}
		

		void reduce()
		{
			a = getNumerator();
			b = getDenominator();

			if(b>a)
			{
				j = b;
				k = a;
			}
			else
			{
				 j = a;
				 k = b;
			}

			for (int count = j; count > k; count--)
			{
				if((a%count==0) && (b%count==0))
				{
					a/=count;
					b/=count;
				}
			}

			cout<<"The reduced fraction is: "<<a<<'/'<<b<<endl;
		}

		void setNumerator(const int N) { numer=N; }
		void setDenominator(const int D) { denom=D; }
		int getNumerator() const { return numer; }
		int getDenominator() const { return denom; }

		void add_display()
		{			
			 if (den==1)
				 cout<<"These fractions added together equals "<<num<<endl;
				 
			 else
			 {
				 cout <<"These fractions added together equals "<<num<<'/'<<den<<endl;
				 setNumerator(num);
				 setDenominator (den);
				 reduce();
			 }
		}

		void sub_display()
		{
			
			 if (den==1)
				 cout<<"These fractions added together equals "<<num<<endl;
			 else
			    cout<<"These fractions subtracted together equals " <<num <<'/'<<den<<endl;
		}

		void mult_display()
		{
			if (den==1)
				 cout<<"These fractions added together equals "<<num<<endl;
			else
				 cout<<"These fractions multiplied together equals " <<num <<'/'<<den<<endl;
		}

		void div_display()
		{	
			if (den==1)
			    cout<<"These fractions added together equals "<<num<<endl;
			else
			    cout<<"These fractions divided together equals " <<num <<'/'<<den<<endl;
		}
};

The strangest thing that you have done here is recode your reduce function with a different algorithm. So let us consider the first algorithm: You implemented (with errors on the variables, the GCD algorithm using the Euclidian method. That can simple be put as this: The gcd(a,0) == a and gcd(a,b) == gcd(b, a % b)
So given two numbers a, b, you find the biggest and

FrAction reduce()
{
a=num;
b=det;
while (i = (a % b))
{

// Check on size needed here:
a = b;
b = i;

}
std::cout"Greatest common divisor is "<<b<<std::endl;

So you should be able to test this and finish the reduce method.

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.