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);
};

Recommended Answers

All 2 Replies

>>Could someone help me to understand more on class objects and help me on this code

Sure, what exactly do you want help on? The functions fadd(), fsub(), fmul() and fdiv() all take parameters but you failed to include them in the class declaration.

In the class function implementation code you did not specify the class, for example

float calculate::fadd(float a, float b, float c, float d)
{
return (a*d + b*c)/(b*d);
};

also what happends when (b*d) is zero? The function above will cause a divide by zero exception. Here is a better way of handling that

float calculate::fadd(float a, float b, float c, float d)
{
      float denominator = b * d;
      if( demoniator != 0)
          return (a*d + b*c)/denominator;
      else
         return 0;
};

or like this using exception handling try/catch block

float calculate::fadd(float a, float b, float c, float d)
{
      float value = 0;
      try
      {
          value = (a*d + b*c) / (b * d);
       }
       catch(...)
       {
            // oops! divide by zero error
        }
        return value;
};

Thank you for replying to my problem. I understand what you wrote on the code. I see where the top code you wrote. I need to change that part of my code on the bottom to somewhat reflect to that somewhat. Right!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.