/*The Fraction + and Fraction / are not being allowed to access private members declared in class Fraction. Any suggestions? */

class Fraction
{
	public:
		Fraction();
		Fraction(int, int);
		void setNum(int);
		void setDen(int);
		int getNum(){return num;}
		int getDen(){return den;}
		void reduce();
		void print(){cout << num <<"/"<< den;}
		friend ostream& operator << (ostream& outputstream, Fraction& fraction);	
		friend istream& operator << (istream& inputstream, Fraction& fraction);
		friend Fraction& operator +(Fraction& c);
		friend Fraction& operator -(Fraction& c);
		friend Fraction& operator *(Fraction& a, Fraction& b);
	    int Fraction_Do_Op (int operation, Fraction& a, Fraction& b);
		
	private:
		int num, den;
		int gcd(int, int);

};
#endif 

Fraction operator +(Fraction d)
{
	int num, den, num2, den2;
	Fraction retfrac;
	retfrac.num = (num * d.den) + (d.num * d.den);
	retfrac.den = d.den * den;
	return(retfrac);
}

Fraction operator /(Fraction n, Fraction d)
{
	int num, den;
	Fraction retfrac;
	retfrac.num = num * d.den;
	retfrac.den = den * d.num;
	if(retfrac.den < 0) //just in case the inversion caused the denominator to be negative
	{
		retfrac.num *= -1;
		retfrac.den *= -1;
	}
	return(retfrac);
}

Recommended Answers

All 12 Replies

Handle them like the others that do?

The compiler will not allow access. I know I am overlooking something. How else would I handle them?

Ain't it supposed to be something like this?

class Fraction
{
public:
   Fraction(int n, int d) : num(n), den(d) {}
   friend Fraction operator+ (const Fraction& a, const Fraction& b);
   friend Fraction operator/ (const Fraction& a, const Fraction& b);

private:
   int num, den;
};

Fraction operator+ (const Fraction& a, const Fraction& b)
{
   Fraction result(a.num * b.den + b.num * a.den, a.den * b.den);
   return result;
}

Fraction operator/ (const Fraction& a, const Fraction& b)
{
   Fraction result(a.num * b.den, a.den * b.den);
   if ( result.den < 0 ) //just in case the inversion caused the denominator to be negative
   {
      result.num *= -1;
      result.den *= -1;
   }
   return result;
}

>>
friend Fraction& operator +(Fraction& c);
friend Fraction& operator -(Fraction& c);
friend Fraction& operator *(Fraction& a, Fraction& b);

Why do these need to be friends anyway?

The compiler will not allow access. I know I am overlooking something.

You have to pay attention to the method signatures. The class declaration has

// returns a reference to a Fraction, and takes in a reference 
// to Fraction
friend Fraction & operator + (Fraction & c);

That is different from what you are trying to implement, which is;

// returns a Fraction, and takes in a Fraction
Fraction operator + (Fraction c);

I want them to have access to the private section of the class.

I want them to have access to the private section of the class.

The reason you might want friends is so you can do something like this:

Fraction frac(2,3);
Fraction f = 1/2.0 * frac;

But in your friend function is doing this :

Fraction f(3,4);
Fraction g(4,5);
Fraction r = f * g; //or g * f;

Since you only provide fraction as an argument to the friend function.
For your function a friend is not needed because implementing a simple Fraction operator*(double); will do the job.

Now if you prototype were this :

Fraction operator*(const double frac)const;

Then you should also provide a friend function with this prototype:

friend Fraction operator*(const double frac1, const Fraction& frac2);

That way you can do this :

Fraction f = 1/2 * aFractionObject; //uses the friend function
Fraction g = aFractionObject * 1/2; //uses aFractionObject.operator*(double);

But won't a friend give more versatility?

But won't a friend give more versatility?

Obviously you did not get my post. In your case friend does not
do anything different to offer than the same function without it being friend.
So don't use friends for that. There is no need. Just implement
a class member function of type :

Fraction operator*(const Fraction& rational){  
  /* logic goes here * /
}
commented: I don't look at these things closely enough sometimes. I'm glad other do. +13

Thanks for the help. Why should I make the Fraction constant?

Thanks for the help.

Thanks for the help. Why should I make the Fraction constant?

Because you are not changing the parameter. You are just
using its values for the computation.

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.