I am in Advance C++ and trying to add a class to this program that I wrote for my lab. Could someone help me to understand more on class objects and help me on this code cause I am losing my lunch on this.
Thanks,
Melissa
#include <iostream>
using namespace std;
struct fraction
{
float num;
float den;
};
class calculate
{
private:
fraction f1;
fraction f2;
char oper;
float fadd();
float fsub();
float fmul();
float fdiv();
public:
int getdate();
double calc();
int main()
{
fraction fra;
fraction fra2;
char oper, frac, frac2, ch;
float ans;
do {
cout << "\nEnter the first fraction, then an operator(+,-,*,/), ";
cout <<"then your second fraction: \n\n";
cin >> fra.num >> frac >> fra.den >> oper >> fra2.num >> frac2 >> fra2.den;
switch (oper)
{
case '+': ans = fadd(fra.num,fra.den,fra2.num,fra2.den); break;
case '-': ans = fsub(fra.num,fra.den,fra2.num,fra2.den); break;
case '*': ans = fmul(fra.num,fra.den,fra2.num,fra2.den); break;
case '/': ans = fdiv(fra.num,fra.den,fra2.num,fra2.den); break;
}
cout << "\n\nThe answer is: " << ans;
cout << "\n\nWould you like to do another (Enter 'y' or 'n')? ";
cin >> ch;
} while (ch != 'n' );
return 0;
}
float fadd(float a, float b, float c, float d)
{
return (a*d + b*c)/(b*d);
};
float fsub(float a, float b, float c, float d)
{
return (a*d - b*c)/(b*d);
};
float fmul(float a, float b, float c, float d)
{
return (a*c)/(b*d);
};
float fdiv(float a, float b, float c, float d)
{
return (a*d)/(b*c);
};