c++ - Classes overloading operators function -
in code below i'm having trouble function named normalize. in program, input fraction 2 integers. normalize function supposed make denominator positive if input negative, , make numerator & denominator positive if both input negative. , supposed convert fraction lowest terms form. works inputs not others , don't understand why.
also in main function, near end of end of try test normalize function , don't know how use oop principles this. did referring member variables directly. how can oop way?
here's code class:
class rational { public: rational(int numer, int denom);//initialize object 1 arguement numerator , 1 denominator rational(int wholenumber);//initialize object 1 argument numerator. sets denominator 1. rational();//initialize object 0 numerator , 1 denominator friend ostream& operator <<(ostream& outputstream, const rational& number);//overloading of insertion operator friend istream& operator >>(istream& inputstream, rational& number);//overloading of extraction operator friend bool operator ==(const rational& number1, const rational& number2);//overloading of boolean "==" friend bool operator <(const rational& number1, const rational& number2);//overloading of inequality sign friend bool operator <=(const rational& number1, const rational& number2);//overloading of inequality sign friend bool operator >(const rational& number1, const rational& number2);//overloading of inequality sign friend bool operator >=(const rational numerator, const rational& denominator);//overloading of inequality sign friend const rational operator +(const rational& number1, const rational& number2);//overloading of + operator friend const rational operator -(const rational& number1, const rational& number2);//overloading of - operator friend const rational operator *(const rational number1, const rational& number2);//overloading of * operator friend const rational operator /(const rational& number1, const rational& number2);//overloading of / operator rational normalize();//normalizes number fraction in lowest terms , denominator positive int calcgcd(int numerator, int denominator);//finds greatest common divisor between numerator , denominator private: int numerator, denominator; };
and normalizing function:
rational rational::normalize() { if ((numerator > 0 && denominator < 0) || (numerator < 0 && denominator < 0)) { denominator = -denominator; numerator = -numerator; } int gcd = calcgcd(numerator, denominator); numerator = numerator/gcd; denominator = denominator/gcd; return rational(numerator, denominator); }
here few simple ways handle normalize. note haven't checked op's logic beyond removing howlers. looks enough answer op's question how use in oo environment.
1 free-floating function
this option passed reference rational , creates , returns different rational
rational normalize(const rational & in) { // values rational work on don't have keep getting // them on , on int num = in.getnumerator(); int den = in.getdenominator(); // numerator , denominator public, getters aren't strictly necessary, // data members should private unless given damn reason // , not then. if you're going learn, might learn // right, go private , use getters. if (den < 0) { // if denominator negative, flip signs of both den , num den *= -1; num *= -1; } int gcd = calcgcd(num, den); //grabbing gcd , string in temp // note changed name of function. int gcd = gcd() in poor taste // if compile, , function should state does. // doesn't gcd, calculates gcd. num = num / gcd; den = den / gcd; return rational(num, den); // create , return new rational. // compiler should smart enough avoid copying }
using it:
rational notnormalized(245, 49); rational normalized = normalize(notnormalized);
2 non-destructive method
in variant, rational uses make different, normalized rational.
because class method, declaration needs added class.
rational normalize() const;
the const
tag means rational object not changed in way using method. trying change object inside normalize method cause compiler error.
and implementation:
rational rational::normalize() const { // copies of numerator , denominator don't damage rational int num = numerator; int den = denominator; if (den < 0) { den *= -1; num *= -1; } int gcd = calcgcd(num, den); num = num / gcd; den = den / gcd; return rational(num, den); }
using it:
rational notnormalized(245, 49); rational normalized = notnormalized.normalize();
3 modifying method
in variant, rational normalizes itself
because class method, declaration needs added class.
rational normalize();
and implementation:
void rational::normalize() { if (denominator < 0) { denominator *= -1; numerator *= -1; } int gcd = calcgcd(numerator, denominator); numerator /= gcd; denominator /= gcd; }
using it:
rational soontobenormalized(245, 49); soontobenormalized.normalize();
addendum
to question answered quickly, cut question down absolute minimum. class definition, normalize function, , "how make normalize function work rational class?" have sufficed. remainder chaff sucked interest , out of people.
Comments
Post a Comment