Mixed Fractions: Classes
Learn the implementation of mixed fractions using the principles of OOP.
We'll cover the following
- Your implementation
- Implementation of the mixed fraction program
- Step 1: Define the MixedFraction class
- Step 2: Implement the constructor
- Step 3: Implement the utility functions
- Step 4: Implement member functions
- Step 5: Implement the utility functions
- Step 6: Implement the print() function
- Step 7: Complete the member function implementations
- Step 8: Implement the reduce() function (optional)
- Complete implementation of the mixed fractions
In this lesson, we’ll learn how to implement the MixedFraction
class using the principles of OOP. We’ll follow a step-by-step approach to designing a modular and powerful class, incorporating key concepts such as constructors, destructors, static functions and attributes, utility functions, private and public access modifiers, abstraction, and encapsulation.
Your implementation
By following the given steps, write your own implementation in MixedFraction.h
and MixedFraction.cpp
here:
#ifndef MIXEDFRACTION_H#define MIXEDFRACTION_H// Write your class declaration (ADT) here#endif // MIXEDFRACTION_H
Implementation of the mixed fraction program
Follow these steps:
Step 1: Define the MixedFraction
class
Start by defining the MixedFraction
class, which will encapsulate the data and functionality related to mixed fractions. We’ll also declare private member variables for the sign (s
), whole part (w
), numerator (n
), and denominator (d
).
// MixedFraction.hclass MixedFraction{private:int s;int w;int n;int d;public:// ConstructorMixedFraction(int sign, int whole, int numerator, int denominator);// Utility functions// ...// Member functions// ...};
Step 2: Implement the constructor
Next, implement the constructor of the MixedFraction
class. The constructor will initialize the object with the provided values and set the private member variables accordingly.
// MixedFraction.cpp#include "MixedFraction.h"MixedFraction::MixedFraction(int sign, int whole, int numerator, int denominator): s(sign), w(whole), n(numerator), d(denominator) // initialized using member initialization{}
Step 3: Implement the utility functions
Now, implement some utility functions, such as GCD()
, that will be used internally within the class. Make these functions private because they’re not meant to be accessed from outside the class.
// MixedFraction.hclass MixedFraction {// ...private:int GCD(int a, int b);// ...};
Add the definition in the MixedFraction.cpp
file.
int MixedFraction::GCD(int a, int b){while (a % b != 0){int r = a % b;a = b;b = r;}return b;}
Step 4: Implement member functions
Next, implement member functions for various operations on mixed fractions, such as addition, subtraction, multiplication, and division. These functions will perform calculations using the private member variables of the class.
// MixedFraction.cppclass MixedFraction{// ...public:// ...MixedFraction add(const MixedFraction& other);MixedFraction subtract(const MixedFraction& other);MixedFraction multiply(const MixedFraction& other);MixedFraction divide(const MixedFraction& other);};
Add dummy definitions in the MixedFraction.cpp
file.
MixedFraction MixedFraction::add(const MixedFraction& other){// Perform addition operationMixedFraction result;// write your code herereturn result;}// The remaining 3 we will write in the next lessonMixedFraction MixedFraction::subtract(const MixedFraction& other){// Perform subtraction operationMixedFraction result;// write your code here...//...return result;}MixedFraction MixedFraction::multiply(const MixedFraction& other){// Perform multiplication operationMixedFraction result;// write your code here//...return result;}MixedFraction MixedFraction::divide(const MixedFraction& other){// Perform division operationMixedFraction result;//...return result;}
Step 5: Implement the utility functions
Within the member functions, we can use private utility functions to perform intermediate steps or calculations. These functions will aid in the encapsulation of logic and promote code reusability.
class MixedFraction {// ...private:void improperFraction();void sameDenominator(MixedFraction& other);void computeSign();void convertToMixedFraction();};
Add their implementation like so:
void MixedFraction::improperFraction(){// Convert to improper fraction// ...}void MixedFraction::sameDenominator(MixedFraction& other){// Make denominators same// ...}void MixedFraction::computeSign(){// Compute sign// ...}void MixedFraction::convertToMixedFraction(){// Convert back to mixed fraction// ...}
Step 6: Implement the print()
function
We can also implement a member function to print the mixed fraction along with an appropriate message. This function will accept a string parameter to display the message.
#include<string>using namespace std;class MixedFraction{// ...public:// ...void print(const string& msg) const;};
Add its implementation to the MixedFraction.cpp
file.
void MixedFraction::print(const string& msg) const{cout << msg << " = ";if (sign == -1)cout << "-";cout << whole << "<" << numerator << "/" << denominator << ">\n";}
Step 7: Complete the member function implementations
Complete the implementation of the member functions by incorporating the previously defined utility functions. Each member function will perform the required calculations, ensuring that the original objects passed to the functions are not modified.
MixedFraction MixedFraction::add(const MixedFraction& other){MixedFraction result = (*this);result.improperFraction();MixedFraction otherCopy(other);otherCopy.improperFraction();result.sameDenominator(otherCopy);result.n += otherCopy.n;result.computeSign();// Reduce the fraction// ...result.convertToMixedFraction();return result;}// Implement subtract, multiply, and divide functions using a similar approach
Step 8: Implement the reduce()
function (optional)
To reduce the fraction to its simplest form, we can implement a private member function called the reduce()
function. This function can be called from other member functions after performing the required calculations.
class MixedFraction {// ...private:void reduce();// ...};
In the MixedFraction.cpp
file, add the following:
void MixedFraction::reduce(){int gcd = GCD(n, d);n /= gcd;d /= gcd;}
And with that, we’re done with the entire implementation. Feel free to test the code. If you have trouble implementing or running the code, look at the complete implementation below.
Complete implementation of the mixed fractions
Here’s the complete implementation, which you may explore. In the next exercise lesson, you will be implementing the remaining three functions.
#include "MixedFraction.h"#include <iostream>#include <cstdlib>using namespace std;int MixedFraction::GCD(int a, int b){while (a % b != 0) {int r = a % b;a = b;b = r;}return b;}void MixedFraction::improperFraction(){this->n = (this->d * this->w) + this->n;this->w = 0;}void MixedFraction::sameDenominator(MixedFraction& other){this->n = this->n * other.d * this->s;other.n = other.n * this->d * other.s;d = other.d = this->d * other.d;}void MixedFraction::computeSign(){if (this->n < 0)this->s = -1;elsethis->s = 1;this->n = abs(this->n);}void MixedFraction::convertToMixedFraction(){this->w = this->n / this->d;this->n = this->n % this->d;}MixedFraction::MixedFraction(int sign, int whole, int numerator, int denominator): s(sign), w(whole), n(numerator), d(denominator){}void MixedFraction::print(const std::string& msg) const{cout << msg << ": ";if (s == -1)cout << "-";cout << this->w << "<" << this->n << "/" << this->d << ">\n";}void MixedFraction::reduce(){int gcd = GCD(this->n, this->d);this->n /= gcd;this->d /= gcd;}MixedFraction MixedFraction::add(const MixedFraction& other) const{MixedFraction result(0, 0, 0, 0);MixedFraction leftCopy = *this;MixedFraction rightCopy = other;leftCopy.improperFraction();rightCopy.improperFraction();leftCopy.sameDenominator(rightCopy);result.n = leftCopy.n + rightCopy.n;result.d = leftCopy.d;result.computeSign();result.reduce();result.convertToMixedFraction();return result;}MixedFraction MixedFraction::subtract(const MixedFraction& other) const{MixedFraction result;// Write your implementation herereturn result;}MixedFraction MixedFraction::multiply(const MixedFraction& other) const{MixedFraction result;// Write your implementation herereturn result;}MixedFraction MixedFraction::divide(const MixedFraction& other) const{MixedFraction result;// Write your implementation herereturn result;}