In my assignment, I have been stuck on one area know matter how many times I read the text book or try and search for help. I feel really confused in this spot, and could really use some advice so I can get over this hurdle as well as learn what is proper. I am still new to programming. So bear with me. We have two .cpp files and one .h file. One of the .cpp has a switch statement that calls different functions depending on users choice. In the old assignment, it was just calling functions, but we are using the same assignment and changing it to overloaded operators. We are instructed to manipulate the .cpp file to replace function calls with our operators. But, I can not figure out how to call those operators, so that they print out their prompts and take in the users values through the operators rather than the old functions. TIA for any help.
.h file
#ifndef _FRACTION_H_
#define _FRACTION_H_
#include "iostream"
using namespace std;
class fraction
{
private:
int numerator;
int denominator;
public:
fraction(int n = 0, int d = 1) : numerator(n), denominator(d) {};
friend fraction operator+(fraction f1, fraction f2);
friend fraction operator-(fraction f1, fraction f2);
friend fraction operator*(fraction f1, fraction f2);
friend fraction operator/(fraction f1, fraction f2);
friend ostream& operator<<(ostream& out, fraction& f);
friend istream& operator>>(istream& in, fraction& f);
};
#endif
.cpp file
#include <iostream>
#include "fraction.h"
using namespace std;
int gcd(int u, int v);
fraction::fraction(int n, int d) : numerator(n), denominator(d)
{
int common = gcd(numerator, denominator);
numerator /= common;
denominator /= common;
}
fraction operator+(fraction f1, fraction f2)
{
int n = f1.numerator * f2.denominator + f2.numerator * f1.denominator;
int d = f1.denominator * f2.denominator;
return fraction(n, d);
}
fraction operator-(fraction f1, fraction f2)
{
int n = f1.numerator * f2.denominator - f2.numerator * f1.denominator;
int d = f1.denominator * f2.denominator;
return fraction(n, d);
}
fraction operator*(fraction f1, fraction f2)
{
int n = f1.numerator * f2.numerator;
int d = f1.denominator * f2.denominator;
return fraction(n, d);
}
fraction operator/(fraction f1, fraction f2)
{
int n = f1.numerator * f2.denominator;
int d = f1.denominator * f2.numerator;
return fraction(n, d);
}
ostream& operator<<(ostream& out, fraction& f)
{
out << f.numerator << "/" << f.denominator << endl;
return out;
}
istream& operator>>(istream& in, fraction& f)
{
cout << "Please enter the numerator: ";
cin >> f.numerator;
cout << "Please enter the denominator: ";
cin >> f.denominator;
return in;
}
// Euclid's Algorithm for finding the greatest common divisor
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; // swap u and v
u = v;
v = t;
}
u -= v;
}
return v; // the GCD of u and v
}
.cpp file (this is the one I need to replace function calls with operators) This is where I am stuck, and not sure how to replace my function calls with my operators.
#include "fraction.h"
#include <iostream>
using namespace std;
int main()
{
char choice;
fraction left;
fraction right;
do
{
cout << "A\tAdd\n";
cout << "S\tSub\n";
cout << "M\tMult\n";
cout << "D\tDiv\n";
cout << "E\tExit\n";
cout << "\nChoice?: ";
cin >> choice;
cin.ignore();
switch (choice)
{
case 'A':
case 'a':
cout << "Adding" << endl;
cout << "enter the first operand: ";
left.read();
cout << "enter the second operand: ";
right.read();
left.add(right).print();
break;
case 'S':
case 's':
cout << "enter the first operand: ";
left.read();
cout << "enter the second operand: ";
right.read();
left.sub(right).print();
break;
case 'M':
case 'm':
cout << "enter the first operand: ";
left.read();
cout << "enter the second operand: ";
right.read();
left.mult(right).print();
break;
case 'D':
case 'd':
cout << "enter the first operand: ";
left.read();
cout << "enter the second operand: ";
right.read();
left.div(right).print();
break;
case 'E' :
case 'e' :
break;
default:
cerr << "Unrecognized choice: " << choice << endl;
break;
}
} while (choice != 'e' && choice != 'E');
return 0;
}