In this lesson, we’ll implement the code for the missing three fraction functions: subtraction, multiplication, and division.

Your implementation

Following the instructions, write your own implementation here:

Press + to interact
main.cpp
MixedFrction.cpp
MixedFraction.h
#ifndef MIXEDFRACTION_H
#define MIXEDFRACTION_H
#include <string>
using namespace std;
class MixedFraction {
private:
int s; // +1 for positive, -1 for negative
int w; // Whole part
int n; // Numerator
int d; // Denominator
int GCD(int a, int b);
void improperFraction();
void sameDenominator(MixedFraction& other);
void computeSign();
void convertToMixedFraction();
public:
MixedFraction(int sign=1, int whole=0, int numerator=0, int denominator=1);
void print(const string& msg) const;
MixedFraction add(const MixedFraction& other) const;
// Write prorotypes of subtract, multiply and divide functions
};
#endif // MIXEDFRACTION_H

Exercise: Complete the MixedFraction class

The first exercise is to add three prototypes to the MixedFraction.h file inside the MixedFraction class declaration.

Implement the following three functions.

Exercise 1: Implement the subtract() function

Write the subtract() function (in MixedFraction.cpp) as a member function in the MixedFraction class and uncomment the lines in main() where subtract() is called.

Exercise 2: Implement the multiply() function

Write the multiply() function (in MixedFraction.cpp) as a member function in the MixedFraction class and uncomment the lines in main() where multiply() is called.

Exercise 3: Implement the division() function

Write the division() function (in MixedFraction.cpp) as a member function in the MixedFraction class and uncomment the lines in main() where division() is called.

If you have trouble getting to the solution, you can click the “Show Solution” button to see how to implement it.