Question

In: Computer Science

Write a Fraction Class in C++ PLEASE READ THE ASSINGMENT CAREFULY BEFORE YOU START, AND PLEASE,...

Write a Fraction Class in C++

PLEASE READ THE ASSINGMENT CAREFULY BEFORE YOU START, AND PLEASE, DON'T ANSWER IT IF YOU'RE NOT SURE WHAT YOU'RE DOING. I APPRECIATE IF YOU WRITE COMMENTS AS WELL. WRONG ANSWER WILL GET A DOWNVOTE

Thank in Advance.

Must do;

The class must have 3 types of constructors; default, overloaded with initializer list, copy constructor

You must overload the assignment operator

You must declare the overloaded output operator as a friend rather than part of the class

The Fraction class should implement addition, subtraction, multiplication, division and simplify operations.

Write a main function in a separate file to test each functionality and show that they work.

Solutions

Expert Solution

fraction.h

#ifndef H_FRACTION
#define H_FRACTION

#include <iostream>
using namespace std;

//Fraction class
class Fraction
{
   //private members
   private:
//nm for numerator and dm for denominator
int nm, dm;
//public members
public:
   Fraction();
Fraction(int n,int d);
Fraction(const Fraction& fx);
void operator=(Fraction fx);
Fraction operator+(Fraction fx);
Fraction operator-(Fraction fx);
Fraction operator*(Fraction fx);
Fraction operator/(Fraction fx);
void simplify();
       friend ostream& operator<<(ostream& os,const Fraction& ob);
       friend istream& operator>>(istream& is,Fraction& ob);
};

#endif

fraction.cpp

#include <iostream>
#include<math.h>>
#include"fraction.h"
using namespace std;

//default constructor
Fraction::Fraction()
{
   nm = 0;
   dm = 1;
}

//parameterized constructor
Fraction::Fraction(int n, int d)
{
   nm = n;
   dm = d;
}

//copy constructor
Fraction::Fraction(const Fraction& t)
{
nm = t.nm;
dm = t.dm;
}

//= operator overloading
void Fraction::operator=(Fraction fx)
{
nm = fx.nm;
dm = fx.dm;
}

//+ operator overloading
Fraction Fraction::operator+(Fraction fx)
{
   int k, j;

k=fx.dm *dm;
j=fx.nm * dm + fx.dm * nm;
Fraction tx(j,k);
tx.simplify();
return tx;
}

//- operator overloading
Fraction Fraction::operator-(Fraction fx)
{
   int j, k;

k=fx.dm *dm;
j=fx.nm * dm - fx.dm * nm;
Fraction tx(j,k);
tx.simplify();
return tx;
}

//* operator overloading
Fraction Fraction::operator*(Fraction fx)
{
   int j, k;

   j = fx.nm * nm;
   k = fx.dm *dm;
Fraction tx(j,k);
tx.simplify();
return tx;
}

// / operator overloading
Fraction Fraction::operator/(Fraction fx)
{
   int j, k;
  
   j = fx.dm * nm;
   k = fx.nm * dm;
Fraction tx(j,k);
tx.simplify();
return tx;
}

// << operator overloading
ostream& operator<<(ostream& os,const Fraction& ob)
{
os<<ob.nm<<"/"<<ob.dm<<"\n";
return os;
}

// >> operator overloading
istream& operator>>(istream& is, Fraction& ob)
{
is>>ob.nm>>ob.dm;
return is;
}

//function to simplify the fraction
void Fraction::simplify()
{
   int i, j, k;

   j = abs(nm);
   k = abs(dm);

   //calculate the gcd
   for(i=j; j%i!=0 || k%i!=0; i--);
  
   nm = nm/i;
   dm = dm/i;
  
   if(nm<0 && dm<0 ||dm<0)
   {
       nm = -nm;
       dm = -dm;
   }
}

main.cpp

#include <iostream>
#include"fraction.h"

using namespace std;

//main function
int main()
{
   Fraction f1(2,3), f2(1,3), f3, f4;
  
   cout<<"f1 = "<<f1<<endl;
cout<<"f2 = "<<f2<<endl;
  
f3=f1+f2;
cout<<"Result of addition: "<<f3<<endl;
  
f3=f1-f2;
cout<<"Result of subtraction: "<<f3<<endl;
  
f4=f1*f2;
cout<<"Result of multiplication: "<<f4<<endl;
  
   f4=f1/f2;
cout<<"Result of division: "<<f4<<endl;
  
   return 0;
}

Output:

f1 = 2/3

f2 = 1/3

Result of addition: 1/1

Result of subtraction: -1/3

Result of multiplication: 2/9

Result of division: 2/1


Related Solutions

C# please! A friend wants you to start writing a video game. Write a class called...
C# please! A friend wants you to start writing a video game. Write a class called “Player” that includes the location of the player, her/his current score, and the ancestry of the player (e.g. “Elf”, “Goblin”, “Giant”, “Human”). A player will always start at location (0, 0) and have a score of 0. Always. Write accessors/modifiers (or “setters/getters”) for each of those characteristics. Write an “appropriate” constructor for the class (based on what’s described above). Write methods moveNorth(), moveSouth(), moveEast()...
Java Program to write a Fraction class that models a mathematical fraction. The fraction class needs...
Java Program to write a Fraction class that models a mathematical fraction. The fraction class needs to support simple arithmetic functions including taking the sum, difference, and the product of the division of the two. Do not include a main() method in the Fraction class. The Fraction class will implement the following class methods: Fraction add (Fraction f1, Fraction f2); // f1 + f2 and returns a new Fraction Fraction sub (Fraction f1, Fraction f2); // f1 - f2 and...
Hello, I need this is C++. Thank you! (1B) Write a Fraction class whose objects will...
Hello, I need this is C++. Thank you! (1B) Write a Fraction class whose objects will represent Fractions. Note: this is the first part of a multi-part assignment. For this week you should not simplify (reduce) fractions, you should not use "const," and all of your code should be in a single file. In this single file, the class declaration will come first, followed by the definitions of the class member functions, followed by the client program. You must provide...
Please Read Carefully Before start answering this question. Please follow the instructions. This Question is from...
Please Read Carefully Before start answering this question. Please follow the instructions. This Question is from 'BSBFIM501 Manage budgets and financial plans' course. There are no parts missing for this Question; guaranteed!. This is the original Screenshot direct from the question. Therefore, there are nothing any further information can be provided. Thanks for your understanding and Cooperation. Please answer the following questions from the topics discussed for Prepare, implement, monitor and modify contingency plans: 1.a. Explain the process of preparing...
FOR C++ A friend wants you to start writing a video game. Write a class called...
FOR C++ A friend wants you to start writing a video game. Write a class called “Player” that includes the location of the player, her/his current score, and the ancestry of the player (e.g. “Elf”, “Goblin”, “Giant”, “Human”). A player will always start at location (0, 0) and have a score of 0. Write accessors/modifiers (or “setters/getters”) for each of those characteristics. Write an “appropriate” constructor for the class (based on what’s described above). Write methods moveNorth(), moveSouth(), moveEast() and...
Please solve in C++ only class is not templated You need to write a class called...
Please solve in C++ only class is not templated You need to write a class called LinkedList that implements the following List operations: public void add(int index, Object item); // adds an item to the list at the given index, that index may be at start, end or after or before the // specific element 2.public void remove(int index); // removes the item from the list that has the given index 3.public void remove(Object item); // finds the item from...
/ READ BEFORE YOU START: // You are given a partially completed program which consist of...
/ READ BEFORE YOU START: // You are given a partially completed program which consist of a class 'Employee' defined in employee.h // The definitions of class member functions are to be filled in employee.cpp (this file). // You should start completing the program from from Q1. Question numbers are given around line 24. // To begin, you should trace through the given code and understand how it works. // Please read the instructions above each required function and follow...
Matrix Multiplication with Threads - C/C++ **Please read entire question before answering** **Please don't copy /...
Matrix Multiplication with Threads - C/C++ **Please read entire question before answering** **Please don't copy / paste code** In this assignment you will use the Pthreads library to write a program that multiplies two square arrays and compare the difference between the imperative and parallel implementations of this algorithm. Use the matrix mulltiplication algorithm. Write a program that contains three functions: (1) A function that has an integer as a parameter and returns a pointer to square array of integers...
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...
c++ Programming For this assignment you will be building on your Fraction class. However, the changes...
c++ Programming For this assignment you will be building on your Fraction class. However, the changes will be significant, so I would recommend starting from scratch and using your previous version as a resource when appropriate. You'll continue working on your Fraction class for one more week, next week. For this week you are not required to provide documentation and not required to simplify Fractions. Please keep all of your code in one file for this week. We will separate...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT