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...
/ 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...
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...
Write two classes Class A and Class B in class A you should read from the...
Write two classes Class A and Class B in class A you should read from the keyboard a number and you should call a public method of the class B that will print on the screen whether the number that was read from the keyboard in class A is multiple of 7 and has the last digit 5.
​ Before you start to type an answer, read the articles provided and think about all...
​ Before you start to type an answer, read the articles provided and think about all of things you have heard and read since the recent tax reform legislation. You will be awarded points for grammar, punctuation, spelling, and overall neatness. In your response, you must reference at least one of the articles provided. Here are the links to the articles: https://www.journalofaccountancy.com/news/2017/dec/tax-reform-bill-changes-for-individuals-201718070.html https://www.journalofaccountancy.com/news/2017/dec/congress-passes-tax-reform-201718091.html https://www.journalofaccountancy.com/news/2017/dec/president-signs-tax-cuts-jobs-act-201718112.html https://tax.thomsonreuters.com/checkpoint/individual-tax-changes-tax-reform-2017/?utm_campaign=SR.TR.INDIV.EL_1217&utm_medium=email&utm_source=Eloqua&site_id=82769734&elqTrackId=48423f0738ac41938fd5563259cb2922&elq=9cd800d5c2604b50b31a0405eb6373eb&elqaid=16613&elqat=1&elqCampaignId=12437 You are not required to give specific details about your financial situation, but please ask...
Write a Java application "WellFormedExpression.java". Start with the file attached here. Please read the comments and...
Write a Java application "WellFormedExpression.java". Start with the file attached here. Please read the comments and complete the function 'isWellFormed()'. It also has the main() to test the function with several input strings. Feel free to change and add more tests with different kinds of input data for correct implementation. Note that you need MyStack.java and Stackinterface.java in the same package. WellFormedExpression.java package apps; public class WellFormedExpression {    static boolean isWellFormed(String s) {        /**** Create a stack...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT