Question

In: Computer Science

Written in C++ Define a class for a type called Fraction. This class is used to...

Written in C++

  • Define a class for a type called Fraction. This class is used to represent a ratio of two integers. Include mutator functions that allow the user to set the numerator and denominator.

  • Also include a member function that returns the values of the numerator divided by the denominator as a double.

  • Include an additional member function that outputs the value of the fraction reduced to lowest terms. This will require finding the greatest common divisor for the numerator and denominator, and dividing by that number.

Solutions

Expert Solution

Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change.

If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.

Thank You!
===========================================================================

#include<iostream>
using namespace std;

class Fraction{
   private:
       int num, den;
   public:
       Fraction(int num=1, int den=1);
       void setNumerator(int num);
       void setDenominator(int den);
       double toDouble() const;
       void displayToLowestTerm() const;
};

Fraction::Fraction(int num, int den): num(num), den(den){}
void Fraction::setNumerator(int num){this->num =num;}
void Fraction::setDenominator(int den){this->den=den;}
double Fraction::toDouble() const{
   return num*1.0/den;
}
void Fraction::displayToLowestTerm() const{
   int gcd = 1;
   for(int f=1; f<num && f<den; f++){
       if(num%f==0 && den%f==0) gcd = f;
   }
  
   cout<<"Reduced Fraction: " << num/gcd<<"/"<<den/gcd <<endl;
}

int main(){
  
   Fraction f1(56,88);
  
   cout<<"To double: " <<f1.toDouble() << endl;
   f1.displayToLowestTerm();

   return 0;
}
===================================================================


Related Solutions

(C++ ONLY) You will define your own data type. It will be a struct called Fraction....
(C++ ONLY) You will define your own data type. It will be a struct called Fraction. The struct will have 2 integer fields: numerator and denominator. You will write a function called reduce that takes a Fraction as a parameter and returns that Fraction in its reduced form. For example, if the fraction 2/4 is passed to the function, the fraction 1/2 will be returned. Consider any fraction with a denominator of 1 to be in reduced form. You will...
Declare and define a class for a fraction number. A fraction in mathematics is defined as...
Declare and define a class for a fraction number. A fraction in mathematics is defined as a/b, where a and b are integers and called numerator and denominator. Requirements Task1    Define a fraction class that has the following member functions: constructor that initializes the fraction by default arguments. set function that sets the numerator of the fraction. set function that sets the denominator of the fraction. get function that returns the numerator of the fraction. get function that returns...
What is an enumerated type in C? Define such a type (called chess_value) to store the...
What is an enumerated type in C? Define such a type (called chess_value) to store the possible values of a chess piece – pawn, rook, knight, bishop, queen and king. Create a structure called struct piece to store a chess piece. This structure should have two fields, one of which is the value of the piece, and should be of the enumerated type defined in Q4. The second field is colour, which should be either 1 (white) or 0 (black)....
Create a c++ class called Fraction in which the objects will represent fractions. Remember:   do not...
Create a c++ class called Fraction in which the objects will represent fractions. Remember:   do not reduce fractions, do not use "const," do not provide any constructors, do not use three separate files. You will not receive credit for the assignment if you do any of these things. In your single file, the class declaration will come first, followed by the definitions of the class member functions, followed by the client program. It is required that you provide these member...
C++ file=.class definitions.h Define a Fraction class with num and den as its private data. Include...
C++ file=.class definitions.h Define a Fraction class with num and den as its private data. Include a constructor to initialize the fraction to 0/1, a copy constructor, a destructor, and overloading functions to overload the assignment operator =, the comparison operators <, >, ==, !=, arithmetic operators +, +=, -, -=, *, *=, /, /=, as well as friend functions (non-member) to overload << and >> to output and input a fraction (see book example). Also, include a private member...
Define a class called Goals that has the following requirements in c++: a function called set...
Define a class called Goals that has the following requirements in c++: a function called set that takes 3 int parameters that are goals for "fame", "happiness" and "money". The function returns true and saves the values if they add up to exactly 60, and returns false otherwise (you may assume the parameters are not negative) a functions satisfies that takes 3 int parameters and returns true if each parameter is at least as large as the saved goal, false...
1. Define a class called Odometer that will be used to track fuel and mileage for...
1. Define a class called Odometer that will be used to track fuel and mileage for an automobile. The class should have instance variables to track the miles driven and the fuel efficiency of the vehicle in miles per gallon. Include a mutator method to reset the odometer to zero miles, a mutator method to set the fuel efficiency, a mutator method that accepts miles driven for a trip and adds it to the odometer’s total, and an accessor method...
Java Define a class called BlogEntry that could be used to store an entry for a...
Java Define a class called BlogEntry that could be used to store an entry for a Web log. The class should have instance variables to store : - the poster’s username, - text of the entry, - and the date of the entry using the Date class Date class is: class Date { private int day, year; private String mon; public Date() { mon=" "; day=0; year=0; } public String toString() { return (mon+"/"+day+"/"+year); } public set_date(String m, int d,...
In C++ Define a base class called Person. The class should have two data members to...
In C++ Define a base class called Person. The class should have two data members to hold the first name and last name of a person, both of type string. The Person class will have a default constructor to initialize both data members to empty strings, a constructor to accept two string parameters and use them to initialize the first and last name, and a copy constructor. Also include appropriate accessor and mutator member functions. Overload the operators == and...
***Define a class called Pizza that has member variables to track the type of pizza (either...
***Define a class called Pizza that has member variables to track the type of pizza (either deep dish, hand tossed, or pan) along with the size (either small, medium, or large) and the number of pepperoni or cheese toppings. You can use constants to represent the type and size. Include mutator and accessor functions for your class. Create a void function, outputDescription( ) , that outputs a textual description of the pizza object. Also include a function, computePrice( ) ,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT