Rational Class in C++ (Object-oriented Programming)

Object-oriented Programming with C++

Assignment: Create a Rational class in c++ to perform arithmetic operation with fractional numbers.

  • Use integer variables to represent the private data of the class, the numerator and the denominator.
  • Provide a constructor that enables an object of this class to be initialized when it is declared.
  • The constructor should contain default values in case no initializers are provided and should store the fraction in reduced form. For example, the fraction 2/4 would be stored in the object as 1 in the numerator and 2 in the denominator.
  • Provide public member functions that perform each of the following tasks:

Adding two Rational numbers. The result should be stored in reduced form.

Subtracting two Rational numbers. The result should be stored in reduced form.

Multiplying two Rational numbers. The result should be stored in reduced form.

Dividing two Rational numbers. The result should be stored in reduced form.

Printing Rational numbers in the form a/b, where a is the numerator and b is the denominator.

Printing Rational numbers in floating-points format.

Leonhard EulerEuclid's Algorithm

Rational.h
/*

Author : William Chanrico
Date : 12/03/2016
Summary : Class representing rational number

Tested on gcc version 5.1.0 (i686-posix-dwarf-rev0, Built by MinGW-W64 project)
>> g++ Demo.cpp Rational.cpp -o a.exe

*/


#ifndef RATIONAL_H
#define RATIONAL_H

class Rational{

private:
 int numerator;
 int denominator;

 int GCD(int x, int y); // Finding greatest commond divisor to calculate reduced form of rationals

public:

 Rational(int numerator = 1, int denominator = 1); // Constructor

 void printFraction() const; // Print rational in form "numerator/denominator"
 void printDecimalFraction() const; // Print rational in decimal fraction format

 Rational add(const Rational &other) const; // Calculate addition
 Rational subtract(const Rational &other) const; // Calculate subtraction
 Rational multiply(const Rational &other) const; // Calculate multiplication
 Rational divide(const Rational &other) const; // Calculate division

 // Operator overloading for convenient use
 Rational operator+(const Rational &other) const;
 Rational operator-(const Rational &other) const;
 Rational operator*(const Rational &other) const;
 Rational operator/(const Rational &other) const;

 operator std::string() const;
 operator float() const;
 
};

#endif /* RATIONAL_H */
Rational.cpp
#include <iostream>
#include <sstream>
#include "Rational.h"

Rational::Rational(int numerator, int denominator){

 this->numerator = numerator;
 this->denominator = denominator;

 int reducer = GCD(numerator, denominator);

 this->numerator /= reducer;
 this->denominator /= reducer;

 if(this->denominator < 0){

 this->numerator *= -1;
 this->denominator *= -1;

 }

}

int Rational::GCD(int x, int y){ return (y == 0) ? x : GCD(y, x % y); }

void Rational::printFraction() const{

 std::cout << (std::string) *this;

}

void Rational::printDecimalFraction() const{

 std::cout << (float) *this;

}

Rational Rational::add(const Rational &other) const{

 int ansNumerator = (numerator * other.denominator) + (other.numerator * denominator);
 int ansDenominator = denominator * other.denominator;

 Rational ans(ansNumerator, ansDenominator);

 return ans;

}

Rational Rational::subtract(const Rational &other) const{

 int ansNumerator = (numerator * other.denominator) - (other.numerator * denominator);
 int ansDenominator = denominator * other.denominator;

 Rational ans(ansNumerator, ansDenominator);

 return ans;

}

Rational Rational::multiply(const Rational &other) const{

 int ansNumerator = (numerator * other.numerator);
 int ansDenominator = (denominator * other.denominator);

 Rational ans(ansNumerator, ansDenominator);

 return ans;

}

Rational Rational::divide(const Rational &other) const{

 Rational divider(other.denominator, other.numerator);

 int ansNumerator = (numerator * divider.numerator);
 int ansDenominator = (denominator * divider.denominator);

 Rational ans(ansNumerator, ansDenominator);

 return ans;

}

Rational Rational::operator+(const Rational &other) const{

 return (*this).add(other);

}

Rational Rational::operator-(const Rational &other) const{

 return (*this).subtract(other);

}

Rational Rational::operator*(const Rational &other) const{

 return (*this).multiply(other);

}

Rational Rational::operator/(const Rational &other) const{

 return (*this).divide(other);

}

Rational::operator std::string() const{

 std::stringstream ss;

 ss << numerator << "/" << denominator;

 return ss.str();

}

Rational::operator float() const{

 return (float) numerator / denominator;

}
DemoRational.cpp
#include <iostream>
#include "Rational.h"

int main(){

 Rational obj1(-5, 9);
 Rational obj2(-17, 2);

 Rational obj3 = obj1 * obj2; // obj3 = obj1.multiply(obj2)

 
 std::cout << (std::string) obj3 << "\n"; // obj3.printFraction()
 std::cout << (float) obj3 << "\n"; // obj3.printDecimalFraction()

}

Leave a Reply

Your email address will not be published. Required fields are marked *