Thank you
Last edited on
You have to work backwards. Look at it like this.

I'd expect to write code like:
 ``123`` ``````Frac a(3, 9); Frac b(10, 6); Frac c = a + b;``````

And that implies that it's declared like:
 `` `` ``Frac operator+(const Frac& a, const Frac& b);``

Using your existing interface, it'd be implemented as:
 ``123456`` ``````Frac operator+(const Frac& a, const Frac& b) { Frac result; result.add(a, b); return result; }``````
Last edited on
 And also overload the pre-increment and post-increment operators ++ which will add 1 to the value of the fraction.

These are a bit tricky, so I'll help.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647`` ``````class Frac { public: // pre-increment // return a reference to *this Frac & operator ++ (); // post-increment (note int dummy parameter) // return a copy of Frac before updating Frac operator ++ (int); // operator== and operator!= are best implemented externally to Frac // this is so that the first parameter doesn't need to be Frac friend bool operator == (const Frac &, const Frac &); friend bool operator != (const Frac &, const Frac &); }; Frac & Frac::operator ++ () { num += den; // just like Frac::addone() return *this; } Frac Frac::operator ++ (int) { Frac temp(*this); // make a copy of current Frac num += den; return temp; } // operator== and operator!= are best implemented externally to Frac // this is so that the first parameter doesn't need to be Frac if you // want to compare a Frac to another type bool operator == (const Frac &lhs, const Frac &rhs) { // lhs - left hand side // rhs - right hand side if (lhs.num == rhs.num) return lhs.den == rhs.den; return false; }``````

Last edited on
Thank you
Last edited on
 @Catfish666 could you please help me with the first portion as well? That is the one I don't understand at all.

What exactly? How to overload the >> << + - * / == != operators?
Thank you
Last edited on
Well kbw already showed you how to overload operator+.
Operators - * / are overloaded the same way as +, you only need to change the operation accordingly: subtraction, multiplication, division.

Then I showed you post- and pre- increment, and equality and inequality operators.

So what's left is only the >> and << operators.

They are used to input and output your Frac. See these links:
http://www.parashift.com/c++-faq/input-operator.html
http://www.parashift.com/c++-faq/output-operator.html
Thank you
Last edited on
 ``1234567891011121314151617181920212223242526272829303132333435363738`` ``````#include #include #include class Frac { friend std::ostream & operator << (std::ostream &, const Frac &); friend std::istream & operator >> (std::istream &, Frac &); }; std::ostream & operator << (std::ostream &os, const Frac &f) { os << "Fraction is: " << f.num << '/' << f.den << std::endl; return os; } std::istream & operator >> (std::istream &is, Frac &f) { // printing shouldn't happen here, just reading! // so this is just an example. std::clog << "Function reading!\n"; std::clog << "Enter num: "; is >> f.num; std::clog << "Enter den: "; is >> f.den; return is; } int main() { Frac f; // now these should work std::cin >> f; std::cout << f; }``````
Thank you
Last edited on
 But could I have just done only for the top part and would it still work out the same?

I don't understand your question.

Do you mean `using namespace std;` so that you can remove the std:: prefixes?
That is a bad habit that you should unlearn.

http://www.parashift.com/c++-faq/using-namespace-std.html

 And as for the - operator, is this correct? And that's all I need to do, correct?

It looks correct to me, yes.
Last edited on
Topic archived. No new replies allowed.