457,903 Members | 1,593 Online Need help? Post your question and get tips & solutions from a community of 457,903 IT Pros & Developers. It's quick & easy.

 P: n/a Folks, I am having a terrible time overloading operators. I have tried what I thought was the correct way, I tried the cheating (friend declarations), all to no avail. Sorry for posting tons of code but I really need help. thanks, Zenon #ifndef Rational__hpp #define Rational__hpp #include #include #include #include #include #include class Rational { // Define data members int numerator; int denominator; // define function prototypes public: Rational::Rational(int num, int denom); // Creates a fraction Rational::Rational(); // Default constructor Rational::~Rational(); // Default destructor void Rational::signAdjuster(); int Rational::reducer(); // helper function to reduce fracion if possible int Rational::reducer(int numerator, int denominator); // helper function to reduce fracion if possible Rational Rational::operator += (const Rational& fraction); Rational Rational::operator -= (const Rational& fraction); Rational Rational::operator *= (const Rational& fraction); Rational Rational::operator /= (const Rational& fraction); friend const Rational operator << (std::ostream& os, const Rational& fraction); friend const Rational operator + (const Rational& lhs, const Rational& rhs); friend const Rational operator - (const Rational& lhs, const Rational& rhs); friend const Rational operator * (const Rational& lhs, const Rational& rhs); friend const Rational operator / (const Rational& lhs, const Rational& rhs); // friend const Rational operator << (std::ostream& os, const Rational& fraction); // Rational Rational::operator + (const Rational& lhs, const Rational& rhs); // Rational Rational::operator + (const Rational& lhs, const Rational& rhs); /* Rational::operator - (const Rational lhs, const Rational rhs); Rational::operator * (const Rational lhs, const Rational rhs); Rational::operator / (const Rational lhs, const Rational rhs); */ Rational::operator double(); // Typecast to double Rational::string(); // Typecast to string /* bool Rational::get(istream& is); void Rational::put(ostream& os);*/ }; //std::ostream& operator<< (std::ostream& os, const Rational& fraction); // Output std::istream& operator>> (std::istream& is, const Rational& fraction); // Input #endif #include #include #include #include #include #include #include #include "Rational.hpp" using namespace std; Rational::Rational(int num, int denom) // Set a fractions numerator and denominator { numerator = num; denominator = denom; cout << "constructor numerator " + num ; signAdjuster(); } Rational::Rational() {} // Default constructor Rational::~Rational() {} // Destructor // std::operator <<(std::ostream& os, const Rational& fraction); // Output /* Rational::ostream& operator <<(std::ostream& os, const Rational& fraction) // Output { os << setw(0) << numerator << " / " << denominator << "\n"; // fraction.put(os); // Rational::put(os); }*/ /* std::istream& operator >>(std::istream& is, const Rational& fraction) // Input { fraction.get(is); }*/ Rational Rational::operator += (const Rational& fraction) { int tempNumOne = 0; int tempNumTwo = 0; int tempDenom = 0; tempNumOne = fraction.numerator * denominator; tempDenom = fraction.denominator * denominator; tempNumTwo = numerator * fraction.denominator; numerator = tempNumOne + tempNumTwo; denominator = tempDenom; int divisor = reducer (); if (divisor != 1) { numerator /= divisor; denominator /= divisor; } return *this; } Rational Rational::operator -= (const Rational& fraction) { int tempNumOne = 0; int tempNumTwo = 0; int tempDenom = 0; tempNumOne = fraction.numerator * denominator; tempDenom = fraction.denominator * denominator; tempNumTwo = numerator * fraction.denominator; numerator = tempNumOne - tempNumTwo; denominator = tempDenom; int divisor = reducer (); if (divisor != 1) { numerator /= divisor; denominator /= divisor; } return *this; } Rational Rational::operator *= (const Rational& fraction) { numerator *= fraction.numerator; denominator *= fraction.denominator; int divisor = reducer (); if (divisor != 1) { numerator /= divisor; denominator /= divisor; } return *this; } Rational Rational::operator /= (const Rational& fraction) { numerator *= fraction.denominator; denominator *= fraction.numerator; int divisor = reducer (); if (divisor != 1) { numerator /= divisor; denominator /= divisor; } return *this; } const Rational operator + (const Rational& fractionLeft, const Rational& fractionRight) { int tempNumOne = 0; int tempNumTwo = 0; int tempDenom = 0; tempNumOne = fractionLeft.numerator * fractionRight.denominator; tempDenom = fractionLeft.denominator * fractionRight.denominator; tempNumTwo = fractionRight.numerator * fractionLeft.denominator; int numerator = tempNumOne + tempNumTwo; int denominator = tempDenom; Rational obj; int divisor = obj.reducer(numerator, denominator); if (divisor != 1) { numerator /= divisor; denominator /= divisor; } return Rational(numerator, denominator); } const Rational operator - (const Rational& fractionLeft, const Rational& fractionRight) { int tempNumOne = 0; int tempNumTwo = 0; int tempDenom = 0; tempNumOne = fractionLeft.numerator * fractionRight.denominator; tempDenom = fractionLeft.denominator * fractionRight.denominator; tempNumTwo = fractionRight.numerator * fractionLeft.denominator; int numerator = tempNumOne - tempNumTwo; int denominator = tempDenom; Rational obj; int divisor = obj.reducer(numerator, denominator); if (divisor != 1) { numerator /= divisor; denominator /= divisor; } return Rational(numerator, denominator); } const Rational operator * (const Rational& fractionLeft, const Rational& fractionRight) { int numerator = fractionLeft.numerator * fractionRight.numerator; int denominator = fractionLeft.denominator * fractionRight.denominator; Rational obj; int divisor = obj.reducer(numerator, denominator); if (divisor != 1) { numerator /= divisor; denominator /= divisor; } return Rational(numerator, denominator); } const Rational operator / (const Rational& fractionLeft, const Rational& fractionRight) { int numerator = fractionLeft.numerator * fractionRight.denominator; int denominator = fractionLeft.denominator * fractionRight.numerator; Rational obj; int divisor = obj.reducer(numerator, denominator); if (divisor != 1) { numerator /= divisor; denominator /= divisor; } return Rational(numerator, denominator); } const Rational operator << (std::ostream& os, const Rational& fraction) { cout << "fraction.numerator " + fraction.numerator << "\n"; cout << fraction.numerator + " / " + fraction.denominator; return Rational(fraction.numerator, fraction.denominator); } /* Rational::operator - (const Rational& fractionLeft, const Rational& fractionRight) { int tempNumOne = 0; int tempNumTwo = 0; int tempDenom = 0; tempNumOne = fractionLeft.numerator * fractionRight.denominator; tempDenom = fractionLeft.denominator * fractionRight.denominator; tempNumTwo = fractionRight.numerator * fractionLeft.denominator; numerator = tempNumOne - tempNumTwo; denominator = tempDenom; int divisor = reducer (); if (divisor != 1) { numerator /= divisor; denominator /= divisor; } }*/ /* Rational::operator * (const Rational& fractionLeft, const Rational& fractionRight) { numerator = fractionLeft.numerator * fractionRight.numerator; denominator = fractionLeft.denominator * fractionRight.denominator; int divisor = reducer (); if (divisor != 1) { numerator /= divisor; denominator /= divisor; } }*/ /* Rational::operator / (const Rational& fractionLeft, const Rational& fractionRight) { numerator = fractionLeft.numerator * fractionRight.denominator; denominator = fractionLeft.denominator * fractionRight.numerator; int divisor = reducer (); if (divisor != 1) { numerator /= divisor; denominator /= divisor; } } */ Rational::operator double() // Typecast to double { return double (numerator/denominator); } Rational::string() // Typecast to string { return (char (numerator) + char(" / ") + char (denominator)) ; } int Rational::reducer() // helper function to reduce fraction if possible { int small = 0; int num = 0; int den = 0; num = numerator; den = denominator; if (num == 0) return den; if (den == 0) return num; // Only absolute value is important if (den < 0) den = den * -1; if (num < 0) num = num * -1; // Smaller of the two becomes small if (num > den) small = den; else small = num; // Continue till no remainder while (num % small != 0 || den % small != 0) { if (num > small) { num = num - (num % small); small = num; } else { den = den - (den % small); small = den; } } // Return thr divisor which leaves no remainder return small; } int Rational::reducer(int numerator, int denominator) // helper function to reduce fraction if possible { int small = 0; int num = 0; int den = 0; num = numerator; den = denominator; if (num == 0) return den; if (den == 0) return num; // Only absolute value is important if (den < 0) den = den * -1; if (num < 0) num = num * -1; // Smaller of the two becomes small if (num > den) small = den; else small = num; // Continue till no remainder while (num % small != 0 || den % small != 0) { if (num > small) { num = num - (num % small); small = num; } else { den = den - (den % small); small = den; } } // Return thr divisor which leaves no remainder return small; } void Rational::signAdjuster() // Make sure that numerator has negative sign if applicable { if (numerator >= 0 && denominator < 0) // Positive numerator, negative denominator { numerator *= -1; denominator *= -1; } if (numerator < 0 && denominator < 0) // Negative numerator, negative denominator { numerator *= -1; denominator *= -1; } } /* bool Rational::get(istream& is) { if (!is) { return false; } string streamString; getline(is, streamString); int position = 0; int streamConverter = 0; stringstream ssOne(streamString.substr(position, 1)); ssOne >> streamConverter; numerator = streamConverter; position++; // skip over any spaces and slashes while (ssOne(streamString.substr(position, 1) == " " || ssOne(streamString.substr(position, 1) == "/") { position++; } stringstream ssOne(streamString.substr(position, 1)); ssOne >> streamConverter; denominator = streamConverter; if (inputString == "") { return false; } else return true; } // Write string to output file void Rational::put(ostream& os) { if (!os) { return false; } if (os) { os << setw(0) << numerator << " / " << denominator << "\n"; } return true; }*/ Jul 19 '05 #1
6 Replies

 P: n/a "Zenon" wrote in message news:b1*************************@posting.google.co m... I am having a terrible time overloading operators. I have tried what I thought was the correct way, I tried the cheating (friend declarations), all to no avail. Sorry for posting tons of code but I really need help. [...] I don't know any psychics, and most people aren't going to simply try out the code without some idea of what is wrong. So why don't you summarize what problems you are having, and save us the trouble of looking through all your code. Dave --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.521 / Virus Database: 319 - Release Date: 9/23/2003 Jul 19 '05 #2

 P: n/a Perhaps a (small) sample of the errors you are getting would be usefull. Jul 19 '05 #3

 P: n/a go******@comcast.net (gooch) wrote in message news:... Perhaps a (small) sample of the errors you are getting would be usefull. My errors all related to the binary operators and stated that these operators must be declared with only one parameter. The other bigest problem was the string() function which is supposed to cast to a string. I cannot use te keyword operator there lik I can in the double() cast.... what is the right way to do it? thanks, Zenon Jul 19 '05 #4

 P: n/a Zenon wrote: I am having a terrible time overloading operators. Ok. I see two superficial things. //... class Rational { // Define data members int numerator; int denominator; // define function prototypes public: Rational::Rational(int num, int denom); // Creates a fraction Rational::Rational(); // Default constructor Rational::~Rational(); // Default destructor void Rational::signAdjuster(); int Rational::reducer(); // helper function to reduce fracion if possible int Rational::reducer(int numerator, int denominator); // helper function to reduce fracion if possible //... }; Inside the class definition, it's obvious your defining members of Rational. The "Rational::" qualifications are unnecessry, not usual, and not a pretty sight. I'm not sure if they are harmless. Then you broke around the lines in your post -- e.g., the word "possible" is no longer in comments, and it causes the compiler to complain on the next token: zenon.cc:25: syntax error before `int' zenon.cc:26: parse error before `fracion' Obviously that's not what you meant, but it makes it more work to get to your real problem. You may need to temporarily change the preferences of you news reader/poster. E.g., in Mozilla, Edit -> Preferences -> Mail&Newsgroups -> Composition: wrap plain text messages at XXX characters. More when I get past these errors. :-) Christian Jul 19 '05 #5 