### c++

#### Overloading + operator to add fractions

I've spent the last couple of hours trying to find a solution to my problem online, and couldn't. For the record, I am a beginner and probably make loads of mistakes, I would like to ask for a help with one issue: I'm trying to write program, that will add two fractions and show me the results. For example, I have entered "5/11" and "3/11", and I want it to dispay "8/11". However, it gives me "0/1" I've overloaded "+" and ">>" operators, but I'm not sure if I did it correctly. Here's the code: #include <iostream> using namespace std; class fraction { public: int num; int den; int sign; fraction (int top); fraction (int top, int bot); }; ostream& operator<< (ostream& z , fraction u) { if (u.sign==-1) z<<"-"; z<<u.num<<"/"<<u.den; return z; } fraction operator+ (fraction x, fraction y) { x.num*y.den+y.num*x.den; return 0; } fraction::fraction (int top, int bot) { den=bot; num=top; sign=1; if (bot<0) { sign=-sign; den=-den; } if (top<0) { sign=-sign; num=-num; } if (bot==0) { num=0; den=1; sign=1; } } fraction::fraction (int top) { den=1; num=top; sign=1; if (top<0) { sign=-sign; num=-num; } } int main() { fraction x (5,11),y (4,11); cout<<x<<" "<<y; cout<<" x+y= "<<x+y; return 0; } Specifically, I believe the problem lies in two spots: fraction operator+ (fraction x, fraction y) { x.num*y.den+y.num*x.den; return 0; } And: int main() { fraction x (5,11),y (4,11); cout<<x<<" "<<y; cout<<" x+y= "<<x+y; return 0; } I know "return 0" in the overload is wrong, but I have no idea what to replace it with. Any help would be really appreciated!

fraction operator+ (fraction x, fraction y) { return fraction(x.num*y.den+y.num*x.den,x.den*y.den); }

This doesn't do anything: x.num*y.den+y.num*x.den; This just multiplies some numbers together, but does nothing with the result. You need to create a fraction object, update its members, and then return that: fraction result; result.num = ...; result.den = ...; return result;

### Related Links

When is the correct time to call restoreState for QHeaderView?

How does it pass lvalue reference to std::forward with CRTP?

Using lower_bound function in loop is giving runtime error?

Problems with rand(), always taking the same random generated number [duplicate]

Reading Credentials file fails with error 32

The mechanism of shared_ptr

C++ specialized function template

Compiler errors with cmath on Mac OS X

Array of fstream in C++

Error: (E107) bind interface to port failed: interface already bound to port: port 'MC8051_ALU().AM.port_27' (sc_in)

Error LINK2019 when I try to make a training for face detection [duplicate]

Quickest way to iterate in a C++ vector

How can I use snmp++ to enable/disable a switch port?

i wan't to use char* in this code instead of string how? [closed]

Using inRange() in OpenCV to detect colors in a range

The string.h 's function does not work in C++ but string does [on hold]