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

  Create program that uses functions and reference parameters

Create program that uses functions and reference parameters, and asks user for the outside temperature.

  Write a program using vectors and iterators

Write a program using vectors and iterators that allows a user to maintain a personal list of DVD titles

  Write the code required to analyse and display the data

Calculate and store the average for each row and column. Determine and store the values for the Average Map.

  Write a webservices application

Write a webservices application that does a simple four function calculator

  Iimplement a client-server of the game

Iimplement a client-server version of the rock-paper-scissors-lizard-Spock game.

  Model-view-controller

Explain Model-View-Controller paradigm

  Design a nested program

How many levels of nesting are there in this design?

  Convert celsius temperatures to fahrenheit temperatures

Write a C++ program that converts Celsius Temperatures to Fahrenheit Temperatures.

  Evaluate and output the value in the given base

Write C program that will input two values from the user that are a Value and a Base with which you will evaluate and output the Value in the given Base.

  Design a base class shape with virtual functions

Design a base class shape with virtual functions

  Implementation of classes

Implementation of classes Chart and BarChart. Class barChart chould display a simple textual representation of the data

  Technical paper: memory management

Technical Paper: Memory Management, The intent of this paper is to provide you with an in depth knowledge of how memory is used in executing, your programs and its critical support for applications.

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