I am working on Exercise 11.11 on page 498 of C++ How to program 8th edition.
The question is this: create a class RationalNumber (fractions) with the following capabilities: a) create a constructor that prevents a 0 denominator in a fraction, reduces or simplifies fractions that are not in reduced form and avoids negative denominators b) overload the addition, substraction, multiplication and division operators for this class c) overload the relational and equality operators for this class
I'm working on overloading the addition operator.
I have completed a, but I am not getting anywhere on overloading. I have searched the internet for a better explanation, but I am just not grasping the concept of how to overload operators with user defined types.
Each RationalNumber has a numerator and a denominator that combine to make a fraction. I need to add them together, find the common denominator, etc.
My RationalNumber.h is thus:
// RationalNumber.h
// RationalNumber class definition
#ifndef RATIONALNUMBER_H
#define RATIONALNUMBER_H
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
class RationalNumber
{
public:
RationalNumber( int numerator, int denominator);
void setNumerator(int numerator);
int getNumerator();
void setDenominator(int denominator);
int getDenominator();
void printRationalNumber();
RationalNumber operator+(Rationalnumber a, RationalNumber b);
private:
int numerator; //top number
int denominator; //bottom number
};
#endif
Now, just focusing on my operator+ function:
RationalNumber RationalNumber::operator+(RationalNumber a, RationalNumber b)
{
}
Visual Studio is telling me that class Rationaloperator has no member "operator+"
Any help would be appreciated.
Thanks, Eric
UPDATE:
I now have:
RationalNumber operator+(Rationalnumber a);
and
RationalNumber RationalNumber::operator+(RationalNumber a)
{
}
I'm getting error: deceleration is incompatible.....( < error_type > a)