L
Luis
can you look at my code, and tell
my wy the section of the client program which says :
const Fraction f3(12, 8);
const Fraction f4(202, 303);
result = f3.MultipliedBy(f4);
cout << "The product of ";
f3.print();
cout << " and ";
f4.print();
cout << " is ";
result.print();
cout << endl;
does not work for me? I know that it is because they have const, so private
members cannot be changed, but some of my function require this. DO i have
to make new functions such as Fraction Fraction <function>()const and it's
counterpart without the const? or do i simply have to change all of my
functions to value returning?
below is the client program with the implmentation file below it, then the
header file.
#include <iostream>
#include "fraction.h"
using namespace std;
#include <iostream>
#include "fraction.h"
using namespace std;
int main()
{
Fraction f1(9,8);
Fraction f2(2,3);
Fraction result;
cout << "The result starts off at ";
result.print();
cout << endl;
cout << "The product of ";
f1.print();
cout << " and ";
f2.print();
cout << " is ";
result = f1.MultipliedBy(f2);
result.print();
cout << endl;
cout << "The quotient of ";
f1.print();
cout << " and ";
f2.print();
cout << " is ";
result = f1.DividedBy(f2);
result.print();
cout << endl;
cout << "The sum of ";
f1.print();
cout << " and ";
f2.print();
cout << " is ";
result = f1.AddedTo(f2);
result.print();
cout << endl;
cout << "The difference of ";
f1.print();
cout << " and ";
f2.print();
cout << " is ";
result = f1.Subtract(f2);
result.print();
cout << endl;
if (f1.isEqualTo(f2)){
cout << "The two fractions are equal." << endl;
} else {
cout << "The two fractions are not equal." << endl;
}
const Fraction f3(12, 8);
const Fraction f4(202, 303);
result = f3.MultipliedBy(f4);
cout << "The product of ";
f3.print();
cout << " and ";
f4.print();
cout << " is ";
result.print();
cout << endl;
}
Fraction :: Fraction()
{
numerator = 0;
denominator = 1;
}
Fraction :: Fraction (int inNumerator, int inDenominator)
{
numerator = inNumerator;
denominator = inDenominator;
}
void Fraction :: print()
{
cout << numerator << "/" << denominator <<endl;
}
Fraction Fraction::MultipliedBy (Fraction otherFraction)
{
Fraction reducedFraction;
Fraction product(numerator * otherFraction.numerator,
denominator * otherFraction.denominator);
Reduce(product.numerator, product.denominator, reducedFraction);
return reducedFraction;
}
Fraction Fraction :: DividedBy (Fraction otherFraction)
{
Fraction reducedFraction;
Fraction quotient(numerator * otherFraction.denominator,
denominator * otherFraction.numerator);
Reduce(quotient.numerator, quotient.denominator, reducedFraction);
return reducedFraction;
}
Fraction Fraction :: AddedTo(Fraction otherFraction)
{
Fraction reducedFraction;
Fraction sum(numerator * otherFraction.denominator +
otherFraction.numerator * denominator, denominator *
otherFraction.denominator);
Reduce(sum.numerator, sum.denominator, reducedFraction);
return reducedFraction;
}
Fraction Fraction :: Subtract(Fraction otherFraction)
{
Fraction reducedFraction;
Fraction difference(numerator * otherFraction.denominator -
otherFraction.numerator * denominator, denominator *
otherFraction.denominator);
Reduce(difference.numerator,difference.denominator, reducedFraction);
return reducedFraction;
}
void Fraction :: Reduce (int& numerator,int& denominator,Fraction&
reducedFraction)
{
int gcf;
GCF(numerator, denominator,gcf);
reducedFraction.set(numerator/gcf,denominator/gcf);
}
void Fraction :: GCF(int numerator,int denominator,int& gcf)
{
int remainder = 1;
while(remainder!=0)
{
remainder = denominator%numerator;
denominator = numerator;
numerator = remainder;
}
gcf = denominator;
}
void Fraction :: set(int inNumerator, int inDenominator)
{
numerator = inNumerator;
denominator = inDenominator;
}
bool Fraction :: isEqualTo(Fraction otherFraction)
{
if(numerator==otherFraction.numerator &&
denominator == otherFraction.denominator)
return true;
else
return false;
}
HEADER FILE
#ifndef Fraction_H
#define Fraction_H
class Fraction
{
public:
Fraction();
Fraction(int inNumerator, int inDenominator);
void print() ;
Fraction MultipliedBy(Fraction otherFraction);
Fraction DividedBy(Fraction otherFraction);
Fraction AddedTo ( Fraction otherFraction);
Fraction Subtract ( Fraction otherFraction);
bool isEqualTo (Fraction otherFraction) ;
private:
int numerator;
int denominator;
void GCF(int numerator, int denominator,int& gcf);
void set(int numerator, int denominator);
void Reduce (int& numerator,int& denominator,Fraction& reducedFraction);
};
#endif
my wy the section of the client program which says :
const Fraction f3(12, 8);
const Fraction f4(202, 303);
result = f3.MultipliedBy(f4);
cout << "The product of ";
f3.print();
cout << " and ";
f4.print();
cout << " is ";
result.print();
cout << endl;
does not work for me? I know that it is because they have const, so private
members cannot be changed, but some of my function require this. DO i have
to make new functions such as Fraction Fraction <function>()const and it's
counterpart without the const? or do i simply have to change all of my
functions to value returning?
below is the client program with the implmentation file below it, then the
header file.
#include <iostream>
#include "fraction.h"
using namespace std;
#include <iostream>
#include "fraction.h"
using namespace std;
int main()
{
Fraction f1(9,8);
Fraction f2(2,3);
Fraction result;
cout << "The result starts off at ";
result.print();
cout << endl;
cout << "The product of ";
f1.print();
cout << " and ";
f2.print();
cout << " is ";
result = f1.MultipliedBy(f2);
result.print();
cout << endl;
cout << "The quotient of ";
f1.print();
cout << " and ";
f2.print();
cout << " is ";
result = f1.DividedBy(f2);
result.print();
cout << endl;
cout << "The sum of ";
f1.print();
cout << " and ";
f2.print();
cout << " is ";
result = f1.AddedTo(f2);
result.print();
cout << endl;
cout << "The difference of ";
f1.print();
cout << " and ";
f2.print();
cout << " is ";
result = f1.Subtract(f2);
result.print();
cout << endl;
if (f1.isEqualTo(f2)){
cout << "The two fractions are equal." << endl;
} else {
cout << "The two fractions are not equal." << endl;
}
const Fraction f3(12, 8);
const Fraction f4(202, 303);
result = f3.MultipliedBy(f4);
cout << "The product of ";
f3.print();
cout << " and ";
f4.print();
cout << " is ";
result.print();
cout << endl;
}
Fraction :: Fraction()
{
numerator = 0;
denominator = 1;
}
Fraction :: Fraction (int inNumerator, int inDenominator)
{
numerator = inNumerator;
denominator = inDenominator;
}
void Fraction :: print()
{
cout << numerator << "/" << denominator <<endl;
}
Fraction Fraction::MultipliedBy (Fraction otherFraction)
{
Fraction reducedFraction;
Fraction product(numerator * otherFraction.numerator,
denominator * otherFraction.denominator);
Reduce(product.numerator, product.denominator, reducedFraction);
return reducedFraction;
}
Fraction Fraction :: DividedBy (Fraction otherFraction)
{
Fraction reducedFraction;
Fraction quotient(numerator * otherFraction.denominator,
denominator * otherFraction.numerator);
Reduce(quotient.numerator, quotient.denominator, reducedFraction);
return reducedFraction;
}
Fraction Fraction :: AddedTo(Fraction otherFraction)
{
Fraction reducedFraction;
Fraction sum(numerator * otherFraction.denominator +
otherFraction.numerator * denominator, denominator *
otherFraction.denominator);
Reduce(sum.numerator, sum.denominator, reducedFraction);
return reducedFraction;
}
Fraction Fraction :: Subtract(Fraction otherFraction)
{
Fraction reducedFraction;
Fraction difference(numerator * otherFraction.denominator -
otherFraction.numerator * denominator, denominator *
otherFraction.denominator);
Reduce(difference.numerator,difference.denominator, reducedFraction);
return reducedFraction;
}
void Fraction :: Reduce (int& numerator,int& denominator,Fraction&
reducedFraction)
{
int gcf;
GCF(numerator, denominator,gcf);
reducedFraction.set(numerator/gcf,denominator/gcf);
}
void Fraction :: GCF(int numerator,int denominator,int& gcf)
{
int remainder = 1;
while(remainder!=0)
{
remainder = denominator%numerator;
denominator = numerator;
numerator = remainder;
}
gcf = denominator;
}
void Fraction :: set(int inNumerator, int inDenominator)
{
numerator = inNumerator;
denominator = inDenominator;
}
bool Fraction :: isEqualTo(Fraction otherFraction)
{
if(numerator==otherFraction.numerator &&
denominator == otherFraction.denominator)
return true;
else
return false;
}
HEADER FILE
#ifndef Fraction_H
#define Fraction_H
class Fraction
{
public:
Fraction();
Fraction(int inNumerator, int inDenominator);
void print() ;
Fraction MultipliedBy(Fraction otherFraction);
Fraction DividedBy(Fraction otherFraction);
Fraction AddedTo ( Fraction otherFraction);
Fraction Subtract ( Fraction otherFraction);
bool isEqualTo (Fraction otherFraction) ;
private:
int numerator;
int denominator;
void GCF(int numerator, int denominator,int& gcf);
void set(int numerator, int denominator);
void Reduce (int& numerator,int& denominator,Fraction& reducedFraction);
};
#endif