ok i cam writing a class named fraction to add subtract multiply and divide fractions and it also compares them. This is my first time writing a class and also the operators are suppose to be overloaded. I think i did them right. I just need to figure out why it wont work.
Here is my Fraction.h file
#pragma once
#include <iostream>
#include <fstream>
using namespace std;
class Fraction
{
int num,denom;
public:
Fraction( );
Fraction(int num,int denom);
void setfraction(int num,int denom);
double getnumerator() const;
double getdenominator() const;
void print() const;
Fraction operator+(const Fraction & fract) const;
Fraction operator-(const Fraction & fract) const;
Fraction operator*(const Fraction & fract) const;
Fraction operator/(const Fraction & fract) const;
bool operator==(const Fraction & fract) const;
bool operator!=(const Fraction & fract) const;
bool operator> (const Fraction & fract) const;
bool operator>=(const Fraction & fract) const;
bool operator< (const Fraction & fract) const;
bool operator<=(const Fraction & fract) const;
private:
int numerator, denominator;
};
Here is my Fraction.cpp file
#include "StdAfx.h"
#include "Fraction.h"
Fraction::Fraction()
{
}
Fraction::Fraction(int num,int denom)
{
numerator=num;
denominator=denom;
}
void Fraction::setfraction(int num,int denom)
{
numerator=num;
denominator=denom;
}
Fraction Fraction::operator+(const Fraction & fract) const
{
Fraction tempFract;
tempFract.num=num+fract.num;
tempFract.denom=denom+fract.denom;
return tempFract;
}
Fraction Fraction::operator-(const Fraction & fract) const
{
Fraction tempFract;
tempFract.num=num-fract.num;
tempFract.denom=denom-fract.denom;
return tempFract;
}
Fraction Fraction::operator*(const Fraction & fract) const
{
Fraction tempFract;
tempFract.num=num*fract.num;
tempFract.denom=denom*fract.denom;
return tempFract;
}
Fraction Fraction::operator/(const Fraction & fract) const
{
Fraction tempFract;
tempFract.num=num*fract.denom;
tempFract.denom=denom*fract.num;
return tempFract;
}
bool Fraction::operator==(const Fraction & fract) const
{
return (num==fract.num)&&(denom==fract.denom);
}
bool Fraction::operator!=(const Fraction & fract) const
{
return (num!=fract.num)||(denom!=fract.denom);
}
bool Fraction::operator> (const Fraction & fract) const
{
return (num> fract.num)&&(denom> fract.denom);
}
bool Fraction::operator>=(const Fraction & fract) const
{
return (num>=fract.num)&&(denom>=fract.denom);
}
bool Fraction::operator< (const Fraction & fract) const
{
return (num< fract.num)&&(denom< fract.denom);
}
bool Fraction::operator<=(const Fraction & fract) const
{
return (num<=fract.num)&&(denom<=fract.denom);
}
void Fraction::print() const
{
cout<<numerator<<"/"<<denominator;
}
Here is my main File it simple for now just one addition and multiplication but i cant get it to work.
#include "stdafx.h"
#include "Fraction.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
Fraction fract1(1,2);
Fraction fract2(1,4);
Fraction fract3;
Fraction fract4;
cout<<"fract1: ";
fract1.print();
cout<<endl;
cout<<"fract2: ";
fract2.print();
cout<<endl;
fract3=fract1 + fract2;
cout<<"fract3: ";
fract3.print();
cout<<endl;
fract4=fract1 * fract2;
cout<<"fract4: ";
fract4.print();
cout<<endl;
return 0;
}