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

This line is the problem:

fraction::fraction(1, 10);

If I understand correctly, you assumed that this line would construct sec with the given parameters (numerator, denominator). However, this is not how you call a constructor, in fact, I don't think the compiler should allow you to do that at all. The constructor is a special function which is called implicitely when you create an object. In this case, you need to declare the object sec with the parameters that construct it, as so:

fraction sec(1, 10);

So, that should work:

int main()
{ 

  fraction f;
  fraction sec(1, 10);

  f.input();

  cout << "1/10 + ";  

  f.output();

  cout << " = ";

  fraction result = f.plus(sec);

  result.output();

  cout << ". " << "which is also ";

  result.toDecimal(); 

  cout << endl;

  return 0;
}

Thanks. I at least get a sum to print out, even though it's wrong. That's progress. :)
What about the calling here? I'm guessing I called the functions wrong, and that's why the sum isn't right.

if (op == '+')
{plus(f2);}
else if (op == '-')
{minus(f2);}
else if (op == '*')
{times(f2);}
else if (op == '/')
{divides(f2);}

Well, in that input() function, the object f2 is 0 (default-constructed). And then, by calling plus(f2);, you are actually adding f2 (which is 0) to this (the object on which the input() function was called), but within the plus() function, the result of the addition is stored within a local object (called second in that function) and then returned. However, in your call plus(f2);, you do not keep the resulting value of the function, and thus, in effect, the call is useless (has no effect except wasting computing time).

I'm not sure if you understand that C++ uses what is called "value-semantics" meaning that the parameters (fraction second) that you have on all those functions are copies of whatever object is passed to the function calls. In other words, calling plus(f2); does not modify the variable f2, it just makes a copy of it, passes it to the plus() function, and the plus() function just works on the copy, not the original variable. If you want that kind of semantics, you need to mark the parameters as references, i.e. with ampersand & character following the type, as in:

fraction plus(fraction& second)

With the above, any modification to second will also be applied to the variable passed at the function call (in technical terms, second becomes an "alias" for the passed variable, i.e., the variable is passed by reference).

This article has been dead for over six months. Start a new discussion instead.