Hello.
I'm a student in an introductory C++ course. For one of our past assignments we have had to create a simple program to add fractions. Each new lab is just an application of new skills learned to making the same program. Now I need to make one using objects from a class definition.
After tooling with a multiplying example my professor gave us, I finally got the code to add fractions properly.
#include <iostream>
using namespace std;
class Fraction
{
private:
float numer;
float denom;
public:
void Init(float n, float d);
void Multi(Fraction *, Fraction *);
void Print();
Fraction()
{
numer = 0.0;
denom = 0.0;
}
void setNumer( float n ) { numer = n; }
void setDenom( float d ) { denom = d; }
float getNumer() { return numer; }
float getDenom() { return denom; }
};
main()
{
Fraction x;
Fraction y;
Fraction f;
x.Init( 1.0, 4.0 );
y.Init( 3.0, 4.0 );
f.Init( 0.0, 0.0 );
f.Multi( &x, &y );
x.Print();
y.Print();
f.Print();
}
void Fraction::Init(float n, float d)
{
numer = n;
denom = d;
}
void Fraction::Multi(Fraction *x, Fraction *y)
{
numer = ( x->numer*y->denom) + (x->denom*y->numer);
denom = ( x->denom*y->denom);
}
void Fraction::Print()
{
cout << "Fraction:" << endl;
cout << " Numerator: " << numer << endl;
cout << " Denominator: " << denom << endl;
}
Stackoverflow cut off my code. :/ (Not too sure why. I'm kinda new to the site)
Anyways, what I'd really like to do is set this program up so it could take user input for what the x and y fractions would be. In my past assignments I've just used cin and cout commands, but now have no idea what to do. Once I get that I know I can make it reduce the fractions properly and display properly, but I have no idea how make it take input.
Does anyone have any suggestions? (Or even better if you can direct me to a site that has more information like cplusplus.com I would be very grateful!)
Thanks you in advance.