I need help understanding how to overload my arithmetic functions in my code, im not asking for it to be done, but i need help. here is what I have:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133`` ``````//Fraction.cpp #include #include #include "fraction.h" using namespace std; int gcd(int u, int v) int main() { int anum; int bden; int cnum; int dden; int u; int v; char choice; fraction add(fraction f2); { do { cout << "a\tadd\n"; cout << "s\tsub\n"; cout << "m\tmult\n"; cout << "d\tdiv\n"; cout << "e\texit\n"; cin >> choice; cin.ignore(); switch (choice) { case 'A': case 'a': cout << "enter the left numerator: "; cin >> anum; cout << "enter the left denominator: "; cin >> bden; cout << "enter the right numerator: "; cin >> cnum; cout << "enter the right denominator: "; cin >> dden; cout << ((anum * dden) + (bden * cnum)) / (bden * dden) << endl; break; case 'S': case 's': cout << "enter the left numerator: "; cin >> anum; cout << "enter the left denominator: "; cin >> bden; cout << "enter the right numerator: "; cin >> cnum; cout << "enter the right denominator: "; cin >> dden; cout << ((anum * dden) - (bden * cnum)) / (bden * dden) << endl; break; case 'M': case 'm': cout << "enter the left numerator: "; cin >> anum; cout << "enter the left denominator: "; cin >> bden; cout << "enter the right numerator: "; cin >> cnum; cout << "enter the right denominator: "; cin >> dden; cout << (anum * cnum) / (bden * dden) << endl; break; case 'D': case 'd': cout << "enter the left numerator: "; cin >> anum; cout << "enter the left denominator: "; cin >> bden; cout << "enter the right numerator: "; cin >> cnum; cout << "enter the right denominator: "; cin >> dden; cout << (anum * dden) / (bden * cnum) << endl; break; case 'E': case 'e': break; default: cerr << "unrecognized choice: " << choice << endl; } } while(choice != 'e' && choice != 'E'); int gcd (int u, int v); u = (u < 0) ? -u : u; v = (v < 0) ? -v : v; while (u > 0) if (u < v) { int t = u; u = v; v = t; } u -= v; } return v; } void print() { int numerator; int denominator; cout << numerator << '/' << denominator; } void read() { int numerator; int denominator; cout << "Please enter the numerator: "; cin >> numerator; cout << "Please enter the denominator: "; cin >> denominator; } ``````

 ``123456789101112131415161718`` ``````#include using namespace std; class fraction { private: int numerator; int denominator; public: fraction() : numerator(), denominator(){} void print(); fraction add(fraction f2); void read(); };``````
You'll want to use the operator keyword, and its usage is almost exactly the same:

[return] operator [symbol] ([argument list])

 ``123456`` `````` Fraction& operator + (fraction& f2) { //do stuff here }; ``````

try this:

http://www.cplusplus.com/doc/tutorial/classes2/
Last edited on
Topic archived. No new replies allowed.