As you can see, I'm struggling with classes. How do I call my fraction plus(fraction second), fraction minus(fraction second), etc. functions?
#include<iostream>
using namespace std;
class fraction
{
private:
void reduce() //I will do this later.
{}
int num, denom;
public:
fraction()
{
num = 0;
denom = 1;
}
fraction(int n, int d)
{
num = n;
if (d = 0)
{ cout << "Can't have a zero denominator!" << endl;
}
else
{denom = d;
}
}
fraction plus(fraction second)
{second.num = (num * second.denom) + (second.num *denom);
second.denom = denom * second.denom;
second;
return second;
}
fraction minus(fraction second)
{second.num = (num * second.denom) - (second.num * denom);
second.denom = denom * second.denom;
return second;
}
fraction times(fraction second)
{second.num = num * second.num;
second.denom = denom * second.denom;
return second;
}
fraction divides(fraction second)
{second.num = num * second.denom;
second.denom = denom * second.num;
return second;
}
double toDecimal()
{ double decimal = (double)num/denom;
cout << decimal;
return decimal;}
void input()
{char div, op;
fraction f2;
cout << "Please enter a fraction in x/y form: ";
cin >> num >> div >> denom;
cout << endl;
cout << "Please enter an operation (*, -, *, /): ";
cin >> op;
if (op == '+')
{plus(f2);} //******I don't think this is right. How do I call fraction plus(second fraction) here?******
else if (op == '-')
{minus(f2);}
else if (op == '*')
{times(f2);}
else if (op == '/')
{divides(f2);}
else {cout << "That is not a valid operation!" << endl;
exit(1);}
}
void output()
{
cout << num << "/" << denom;
}
};
int main()
{ int myFraction = 1/10;
fraction f, sec;
fraction::fraction(1, 10);
f.input();
cout << "1/10 + ";
f.output();
cout << " = ";
//I just want to get this calculation printed out. (1/10 + the fraction the user enters) So I need to call fraction plus(second fraction), right? How do I do this?
cout << ". " << "which is also ";
f.toDecimal();
cout << endl;
return 0;
}