#include <iostream>
using namespace std;
class Fraction
{
private:
int numerator;
int denominator;
public:
Fraction() //default constructor
{
numerator = 0;
denominator = 1;
}
Fraction(int num,int den) //argument constructor
{
if(denominator != 0)
{
numerator=num;
denominator=den;
}
simplify();
}
friend std::ostream& operator<<(
std::ostream &output, const Fraction &F )
{
output << F.numerator
<< " / " << F.denominator;
return
output;
}
double toDouble()
{
return
(double)numerator/denominator;
}
int gcd(int x, int y) // compute
greatest common divisor
{
int t;
while (y>0) {
t = x % y;
x = y;
y = t;
}
return x;
}
void simplify() {
int factor;
factor = gcd(numerator, denominator);
numerator = numerator / factor;
denominator = denominator / factor;
}
int getNumerator()
{
return numerator;
}
int getDenominator()
{
return denominator;
}
};
int main()
{
Fraction r1(20,60);
Fraction r2(30,40);
cout<<"Fraction f1 :
"<<r1.getNumerator()<<"/"<<r1.getDenominator();
cout<<"\nFraction f1 in decimal : ";
cout<<r1.toDouble();
cout<<"\nFraction f2 :
"<<r2.getNumerator()<<"/"<<r2.getDenominator();
cout<<"\nFraction f2 in decimal : ";
cout<<r2.toDouble();
return 0;
}
Output:
Fraction f1 : 1/3
Fraction f1 in decimal : 0.333333
Fraction f2 : 3/4
Fraction f2 in decimal : 0.75
Do ask if any doubt. Please upvote.
Submission Instruction Complete the following C++ programs. The assignment contains only one file with all different...
c++
format
Goals . Understand how to implement operator overloading in C++ Warning: Programs must compile using gt+ on the Computer Science Linux systems. If your code does not compile on CS machines you will get 0 for the assignment. Organize your files into folders by lab assignment. For this assignment, create a folder called Lab9. A. Read Chapter 18 B. Create a new class called RationalNumber to represent fractions. The class should have the following capabilities: 1) It should...
Design a class named Fraction. This class is used to represent a ratio of two integers, such as 6 / 9. Include accessors and mutators that allow the user to get and set the numerator and the denominator. Also include a member method that returns the value of the numerator divided by the denominator as double (for example, 0.666…). Include an additional member method that returns the value of the fraction reduced to lowest terms as string. For example, instead...
Construct a class named Fractions containing two integer data members named num and denom, used to store the numerator and denominator of a fraction having the form num/denom. Your class should include a default constructor that initializes num and denom to 1 if there's no user initialization, and it must prohibit a 0 denominator value. In addition, create member functions for displaying an object's data values and overloaded operator functions for adding, subtracting, multiplying, and dividing two Fraction objects, as...
Header file for the Rational class:
#ifndef RATIONAL_H
#define RATIONAL_H
class Rational
{
public:
Rational( int = 0, int = 1 ); // default constructor
Rational addition( const Rational & ) const; // function
addition
Rational subtraction( const Rational & ) const; // function
subtraction
Rational multiplication( const Rational & ) const; // function
multi.
Rational division( const Rational & ) const; // function
division
void printRational () const; // print rational format
void printRationalAsDouble() const; // print rational as...
Give answer according to fill in the blanks. 1.Define a class called Fraction . This class is used to represent a ratio of two integers.Include mutator methods that allow the user to set the numerator and the denominator. Also include a method that returns the value of numerator divided by denominator as a double . Include an additional method that outputs the value of the fraction reduced to lowest terms (e.g., instead of outputting 20/60, the method should output 1/3)....
Refer to this header file: // Fraction class // This class represents a fraction a / b class Fraction { public: // Constructors Fraction(); // sets numerator to 1 and denominator to 1 Fraction(int num, int denom); // Setters void setNumerator(int num); void setDenominator(int denom); // getters int getNumerator()const {return num;} int getDenominator()const {return denom;} double getDecimal(){return static_cast<double> num / denom;} private: int num, denom; }; 1.Write the code for the non-member overloaded << operator that will display all of...
Do the following: 1) Add a constructor with two parameters, one for the numerator, one for the denominator. If the parameter for the denominator is 0, set the denominator to 1 2) Add a function that overloads the < operator 3) Add a function that overloads the * operator 4) Modify the operator<< function so that if the numerator is equal to the denominator it just prints 1 5) Modify the operator>> function so that after it reads the denominator...
C++ Problem 1 Write a function to calculate the greatest common divisor (GCD) of two integers using Euclid’s algorithm (also known as the Euclidean algorithm). Write a main () function that requests two integers from the user, calls your function to compute the GCD, and outputs the return value of the function (all user input and output should be done in main ()). In particular, you will find this pseudocode for calculating the GCD, which should be useful to you:...
C++ For this assignment you will be building on the Original Fraction class you began last week. You'll be making four major changes to the class. [15 points] Delete your set() function. Add two constructors, a default constructor that assigns the value 0 to the Fraction, and a constructor that takes two parameters. The first parameter will represent the initial numerator of the Fraction, and the second parameter will represent the initial denominator of the Fraction. Since Fractions cannot have...
This in in C# There are two classes, class Fraction and class FractionDemo The user needs to be able to input a value for the numerator and the denominator Create a Fraction class with private fields that hold a positive int numerator and a positive int denominator. In addition, create Properties for each field with the set mutator such that the numerator is greater than or equal to 0 and the denominator is greater than 0 (illegal values should be...