Ok so you know what classes are. Basically you're making a class (@ a quick glance through your specs)
Your class contains those functions you listed as public members.
Your class also contains private members in the form of:
int Numerator;
int Denominator;
Classes are written as (this would be in your Fraction.h file)
class <class name>
{
public:
private:
};
IE:
//---------
Fraction.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
//1*
#ifndef _FRACTION_
#define _FRACTION_
class Fraction
{
public:
Fraction(int n = 0, int d = 1); //your constructor
int getNum() const;
//...
Fraction operator+(const Fraction& f2) const;
//etc down your list until
friend ostream& operator<<(ostream& out, const Fraction& f);
private:
int Num,Denom; //2*
};
#endif
|
1* These are precompile headers meaning these are executed before the code is compiled. The reason we do this is because in most cases, a .h file is included many times throughout a program. If compiled without these headers the compiler will throw an error that is something like "This blahblha has already been defined in faction.obj" - basically it doesn't like it that it has already seen it before and seems to be forced into compiling it again.
#ifndef _FRACTION_
This means if not defined, it is paired with #endif and functions like a C++ 'if'. I am asking the compiler if it has NOT seen this before, if it is true meaning this is the first time it ran into this block of code, then compile the following code up to the point of #endif.
#define _FRACTION_
If #ifndef is true, then I tell the compiler to define _FRACTION_ thus, the compiler now knows that _FRACTION_ exists and will continue on to the rest of the code. The reason for the #define is to make #ifndef untrue. This is how we guarantee that our header file is compiled only once.
2* The reason I suspect you have these two variables as members of your class is because of
Fraction(int n = 0, int d = 1);
Which appears to be the constructor. It passes in two variables and since I see you have two other functions whose purpose is to return the numerator and denominator then those members are likely private.
2. int getNum() const;
3. int getDenom() const;
//------------------
To your Fraction.cpp file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
#include "Fraction.h"
#include <iostream>
using namespace std; //don't worry about this, just put it in so cout works
//declaring our constructor : Fraction(int n = 0, int d = 1);
Fraction::Fraction(int n, int d)
{
Num = n;
Denom = d;
//probably a good idea to do some error catching here like
if( d == 0 )
cout<<"Hey don't make blackholes by dividing by zero!";
}
int Fraction::getNum() const{ return Num; }
//same thing down to the end, here is another example
//here we're overloading
Fraction Fraction::operator+(const Fraction& f2) const
{
Fraction ReturnObj;
//fractions have to be the same denominator to be added together
if( Denom == f2.Denom )
{
ReturnObj.Num = Num + f2.Num;
ReturnObj.Denom = Denom;
}
//uh - oh they're not the same denom
else
{
//this would solve the problem but not the most efficient way
//you would have to see if it is possible to reduce the fraction after
//adding, ie 4/8 reduced is 1/2
ReturnObj.Num = Num*f2.Denom + f2.Num*Denom;
ReturnObj.Denom = Denom*f2.Denom;
}
return ReturnObj;
}
|
//----------
Onto FractApp.cpp
1 2 3 4 5 6 7 8 9 10 11
|
#include "Fraction.h"
int main()
{
Fraction f1(1,2); //initalizing your object
Fraction f2(2,3);
cout<< "This is my numorator!"<< f1.getNum() << endl;
cout<< "Adding f1 and f2, my numerator should be 7!" << (f1+f2).getNum() <<endl;
return 0;
}
|
If you create 3 files and compile these, they should work fine and compile cleanly. Well for the most part...I coded this in the terribly small edit box so there may be compile errors. This should be more than enough to get you started. .