Reduce the numerator and denominator

Assignment Help C/C++ Programming
Reference no: EM13167785

Write the function  in C ++ to reduce the numerator and denominator in the Rat class to lowest terms.

// class for rational numbers

#include <iostream>

using namespace std;

class Rat{

private:

int n;

int d;

public:

// constructors

// default constructor

Rat(){

n=0;

d=1;

}

// 2 parameter constructor

Rat(int i, int j){

n=i;

d=j;

}

// conversion constructor

Rat(int i){

n=i;

d=1;

}

//accessor functions (usually called get() and set(...) )

int getN(){ return n;}

int getD(){ return d;}

void setN(int i){ n=i;}

void setD(int i){ d=i;}

//arithmetic operators

Rat operator+(Rat r){

Rat t;

t.n=n*r.d+d*r.n;

t.d=d*r.d;

return t;

}

2

// 2 overloaded i/o operators

friend ostream& operator<<(ostream& os, Rat r);

friend istream& operator>>(istream& is, Rat& r);

}; //end Rat

// operator<<() is NOT a member function but since it was declared a friend of Rat

// it has access to its private parts.

ostream& operator<<(ostream& os, Rat r){

os<<r.n<<" / "<<r.d<<endl;

return os;}

// operator>>() is NOT a member function but since it was declared a friend of Rat

// it has access to its provate parts.

istream& operator>>(istream& is, Rat& r){

is>>r.n>>r.d;

return is;

}

int main(){

Rat x(1,2), y(2,3), z;

z=x+y;

cout<<z;

x.setN(3);

y.setD(2);

z=x+y;

cout<<z;

cin>>x;

cout<<x;

z= x+5;

cout<<z;

system("pause");

return 0;

}

Reference no: EM13167785

Questions Cloud

Generates numbers in the fibonacci sequence : Write a class called fibs that generates numbers in the Fibonacci sequence (each number is the sum of the previous two). The sequence starts 1, 1, 2, 3, 5, ...
Write a program in which the program print out the input : use (switch statement) to write a program in which the program print out the input (single character) if the character is not '2','t', or 'w'. Use 'default' and 'break' wisely.
State the candy inside a bomb calorimeter : A researcher studying the nutritional value of a new candy places a 5.9 gram sample of the candy inside a bomb calorimeter and combusts it in excess oxygen.
Temperature conversions : Temperature Conversions. The following problems generate temperature- conversion tables. Use following equations that give relationships between temperatures in degrees Fahrenheit(Tf), degree Celsius(Tc), degrees Kelvin(Tk), and degrees Rankin(Tr);
Reduce the numerator and denominator : Write the function  in C ++ to reduce the numerator and denominator in the Rat class to lowest terms.
Unit conversions : Unit Conversions. The following problem generate tables of unit conversions. Include a table heading and column headings for the tables. Choose the number of decimal place based on the values to be printed.
What is the median of the reported blood pressure values : What is the median of the reported blod pressure values?
Saddle point is an element : For a square nXn a array, a saddle point is an element that is the maximum in its row and the minimum in its column.
What is the magnitude of the accumulated difference : The difference is accumulated every 1/10th of a second for one day. What is the magnitude of the accumulated difference

Reviews

Write a Review

C/C++ Programming Questions & Answers

  Use the getint and getintwithinrange methods

Use the getInt and getIntWithinRange methods to validate that each score ranges from 1 through 100 and add code that discards any extra entries at the prompt that asks if you want to enter another score to the application below.

  Represent an instruction supported by simpletron

Implementation contains a Simpletron class and several supporting Instruction classes

  The fibonacci sequence

In the novel "The DaVinci Code", a scrambled version 13, 3, 2, 21, 1, 1, 8, 5 of the first eight Fibonacci numbers appear as one of the clues left by murdered museum curator Jacques Sauniere. In this assignment, we will do something related to t..

  C++ class that has a dynamic array of a string

show  an example of a C++ Class that has a dynamic array of a string that takes in names by user input??

  Counts all occurrences of a given string from such a bag

Write a C++ function that removes and counts all occurrences of a given string from such a bag.

  Build a traffic light system - microcontroller system

Build the whole system with 3 RAG units and three puffin crossing units and build a team to work on this mini-project, be careful in selecting your team member.

  A c++ program using the concept of function overloading

write a c++ program using the concept of function overloading for the following options : select the options from [1-3], 1- Area of rectangle 2- Area of square 3- Exit ,

  Write a c program that reads a data file of floating numbers

Write a program that reads a data file of floating numbers into an array and prints the array elements along with the number of items in the array

  The main program should create an ifstream

For decryption, the main program should create an ifstream for the file to be decrypted. It should use the getline method of the ifstream to read lines from the file, call the encryption / decryption function with the line to be decrypted, and dis..

  Create an array of objects from the provided code

Create an array of objects from the provided code and run a for loop to assign strings and numbers. In this assignment, I will need to see the main function. Make sure there is a default constructor.

  The first will be a structure called point

Create two structures. The first will be a structure called Point and will have as its data members an x and y coordinate. The second will be called Line and will have two Point structures as its data members.

  Use selection sort to sort a[48] into increasing order

Use selection sort to sort A[48] into increasing order, and then print out the sorted list in four rows. There may be duplicates, but that's OK. (65 and 53 appear twice.) Duplicates will appear next to each other in the sorted list.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd