just want to check my code: the debug messege board said: \'in fraction\' is pri
ID: 664233 • Letter: J
Question
just want to check my code:
the debug messege board said:
'in fraction' is private
#include
using namespace std;
class Fraction
{
private:
int numerator;
int denominator;
public:
Fraction()
{
numerator = 0;
denominator = 0;
}
void set(int n, int d)
{
n = numerator; d = denominator;
}
int getNumerator()
{
return numerator;
}
int getDenominator()
{
return denominator;
}
Fraction addTo(Fraction &frac2, Fraction& frac3);
void print()
{
cout << numerator << "/" << denominator;
}
};
Fraction Fraction::addTo(Fraction& frac2, Fraction& addedTo)
{
addedTo.numerator = (numerator * frac2.denominator) + (denominator * frac2.numerator);
addedTo.denominator = (denominator * frac2.denominator);
return addedTo;
}
int main()
{
Fraction frac1, frac2, frac3;
int n1, n2, d1, d2;
cout << "Enter First Numerator: ";
cin >> n1;
frac1.numerator = n1;
cout << "Enter First Denominator: ";
cin >> d1;
frac1.denominator = d1;
cout << "Enter Second Numerator: ";
cin >> n2;
frac2.numerator = n2;
cout << "Enter Second Denominator: ";
cin >> d2;
frac2.denominator = d2;
frac1.set(n1, d1);
frac2.set(n2, d2);
frac1.addTo(frac2, frac3);
cout<< "------------------------- ";
frac1.print();
std::cout << " + ";
frac2.print();
std::cout << " = ";
frac3.print();
return 0;
}
Explanation / Answer
In your given code consider the below method
Fraction Fraction::addTo(Fraction& frac2, Fraction& addedTo)
Here you are passing a fraction value, for that you no need to put the '&' .
And also in the below method Fraction addTo(Fraction &frac2, Fraction& frac3);
Here also we dont need the '&' symbol.
And another thing is if you want to use a fraction number or if you want to pass a variable value which contains the fractional datatype, then you can use the data type double for passing that value.
Once modify your code by using the double data type in the place of fraction and by removing the symbol '&'.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.