Program 2
Overloading = in rationalType:
In order to include a statement such as
rat3=rat1+rat2;
which inherits + from fraction, you need to be able to assign a fraction (rat1+rat2 returns
fraction) to a rational (rat3).
This means overloading assignmant (=) as follows
In rational.h:
//assign a fraction to a rational
rationalType operator=(const fractionType&) const;
In rational.cpp
const rationalType rationalType::operator=(const fractionType& otherFraction)
{
numerator =otherFraction.getNumerator();
denominator=otherFraction.getDenominator();
return *this;
}
// This allows a fraction to be assigned to a rational. Otherwise we
// get "no operator defined which takes a right-hand operand of type
// 'class fractionType' (or there is no acceptable conversion)"
Other:
- Derived classses do not inherit the constructors.
You need to add one to rationalType to use it.
- Remember ; at the end of the class header.
- When overloading << and >> use #include <iostream.h>
and remove using namespace std;
- Overload operators in fraction before tackling inheritance.
- If you get " 'class' type redefinition" use the compiler directives "#ifndef...
- gcd is a separate file, not part of fraction or rational.
- Put your name on all pieces.