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

  Program to print out invalid number to character

The integer must contain 3 distinct non-zero number, or the program will print out invalid number.it should print out invalid query.

  Iterative programming problem solving approaches

Write a recursive function void reverse ( ) that reverse a sentence

  Public data members and private data members.

Explain the difference between public data members and private data members. Give an example in the main function to show that one C++ statement is valid and the other is invalid (you can use Grade class in Problem 5.

  Draws a single level for a "rogue­like" computer game

You will write a program that draws a single level for a "Rogue­like" computer game. The program will parse a line of input text from an input file (room.txt), use the parsed text to determine the shape of the room and its contents and then draw the ..

  Implement circular linked list in c program

implement circular linked list in c program in the best possible easiest way.

  An integer on this system is 4 bytes long

Assume the following for the program below. An integer on this system is 4 bytes long. The memory address where the array "a[]" was created begins at location 8000. a. #include

  Perform operations on arrays

Perform operations on arrays execute tests and repetitions

  Add a copy constructor

Take the code below and add a copy constructor, assignment operator definition and destructor to the class. These functions have already been started for you.

  Test a program that prompts for the user

Write and test a program that prompts for the user to input a file name and uses two functions head() and tail() - head() displays the first 10 lines of a file

  C assignment of curl library

C Assignment Curl library must be used To be done on linux, it should be compiled using the following command: gcc -Wall -ansi -pedantic NameOfFile.c -lncurses Please make sure it is commented with clarification Variable names in camel case

  Create a class called point3d

Create a class called Point3D to represent 3D point in space, a class called Sphere and a class called Cylinder. The sphere is characterized by its center point and the radius while the Cylinder can be characterized by its center point, radius and he..

  You should use pure recursive calls

You should use pure recursive calls completely and count the number of calls the program makes. You should count how many times recursive calls were made.

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