Question
c++ format

Goals . Understand how to implement operator overloading in C++ Warning: Programs must compile using gt+ on the Computer Scie
0 0
Add a comment Improve this question Transcribed image text
Answer #1

RationalNumber.h

#ifndef RATIONALNUMBER_H
#define RATIONALNUMBER_H

class RationalNumber
{
public:
  
RationalNumber();

// Initializes the rational number to 0

RationalNumber(int wholeNumber);

// Initializes the rational number to wholeNumber/1

RationalNumber(int m, int n);

// Initializes the rational number to m/n if n is not 0;

// the sign of the rational is stored in the numerator

// (the denominator is always positive);

// exits if n = 0 (invalid rational number)

void output();

// Precondition: The rational number is defined.

// Postcondition: The rational number has been displayed on

// the screen in the form m/n.

friend bool operator ==(const RationalNumber& r1, const RationalNumber& r2);

// Precondition: r1 and r2 are valid rational numbers

// Returns true if r1 equals r2; false otherwise.

friend bool operator <(const RationalNumber& r1, const RationalNumber& r2);

// Precondition: r1 and r2 are valid rational numbers

// Returns true if r1 is less than r2; false otherwise.

friend bool operator >(const RationalNumber& r1, const RationalNumber& r2);

// Precondition: r1 and r2 are valid rational numbers

// Returns true if r1 is greater than r2; false otherwise.

friend bool operator <=(const RationalNumber& r1, const RationalNumber& r2);

// Precondition: r1 and r2 are valid rational numbers

// Returns true if r1 is less than or equal to r2; false otherwise.

friend bool operator >=(const RationalNumber& r1, const RationalNumber& r2);

// Precondition: r1 and r2 are valid rational numbers

// Returns true if r1 is greater than or equal to r2; false otherwise.

friend bool operator !=(const RationalNumber& r1, const RationalNumber& r2);

// Precondition: r1 and r2 are valid rational numbers

// Returns true if r1 is not equal to r2; false otherwise.


friend RationalNumber operator+(RationalNumber a, RationalNumber b);

friend RationalNumber operator-(RationalNumber a, RationalNumber b);

friend RationalNumber operator*(RationalNumber a, RationalNumber b);

friend RationalNumber operator/(RationalNumber a, RationalNumber b);

void printRational();

int gcd(int a, int b);

private:
// Declaring variables
int numer; // the numerator of the number

int denom; // the denominator of the number

};
#endif

________________

RationalNumber.cpp

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

// Initializes the rational number to 0
RationalNumber::RationalNumber()
{
this->numer = 0;
this->denom = 1;
}

// Initializes the rational number to wholeNumber/1
RationalNumber::RationalNumber(int wholeNumber)
{
this->numer=wholeNumber;
this->denom=1;
}

// Initializes the rational number to m/n if n is not 0;

// the sign of the rational is stored in the numerator

// (the denominator is always positive);

// exits if n = 0 (invalid rational number)


RationalNumber::RationalNumber(int m, int n)
{
if(n==0)
{
cout<<"** Denominator Must nor be zero **"<<endl;
exit(0);
}
else if(denom<0)
{
this->numer=-(m);
this->denom=-(n);
}
else
{
this->numer=(m);
this->denom=(n);
}
}

// Precondition: r1 and r2 are valid rational numbers

// Returns true if r1 equals r2; false otherwise.
bool operator ==(const RationalNumber& r1, const RationalNumber& r2)
{
int a = r1.numer;
int b = r1.denom;
int c = r2.numer;
int d = r2.denom;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
if ((n1 == n2) && (d1 == d2))
return true;
else
return false;

}


// Precondition: r1 and r2 are valid rational numbers

// Returns true if r1 is less than r2; false otherwise.
bool operator <(const RationalNumber& r1, const RationalNumber& r2)
{
int a = r1.numer;
int b = r1.denom;
int c = r2.numer;
int d = r2.denom;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
if ((n1 / d1) < (n2 / d2))
return true;
else
return false;

}


// Precondition: r1 and r2 are valid rational numbers

// Returns true if r1 is greater than r2; false otherwise.
bool operator >(const RationalNumber& r1, const RationalNumber& r2)
{
int a = r1.numer;
int b = r1.denom;
int c = r2.numer;
int d = r2.denom;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
if ((n1 / d1) > (n2 / d2))
return true;
else
return false;

}


// Precondition: r1 and r2 are valid rational numbers

// Returns true if r1 is less than or equal to r2; false otherwise.
bool operator <=(const RationalNumber& r1, const RationalNumber& r2)
{
int a = r1.numer;
int b = r1.denom;
int c = r2.numer;
int d = r2.denom;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
if ((n1 / d1) <= (n2 / d2))
return true;
else
return false;

}


// Precondition: r1 and r2 are valid rational numbers

// Returns true if r1 is greater than or equal to r2; false otherwise.
bool operator >=(const RationalNumber& r1, const RationalNumber& r2)
{
int a = r1.numer;
int b = r1.denom;
int c = r2.numer;
int d = r2.denom;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;

if ((n1 / d1) >= (n2 / d2))
return true;
else
return false;

}


bool operator !=(const RationalNumber& r1, const RationalNumber& r2)
{
int a = r1.numer;
int b = r1.denom;
int c = r2.numer;
int d = r2.denom;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
// cout<<f1<<" "<<f2<<endl;
if ((n1 != n2) || (d1 != d2))
return true;
else
return false;

}

RationalNumber operator+(RationalNumber r1, RationalNumber r2) {

int a, b, c, d;
a =r1.numer;
b = r1.denom;
c = r2.numer;
d = r2.denom;
int sumnumer = (a * d + b * c);
int sumdenom = (b * d);
RationalNumber r(sumnumer, sumdenom);


return r;

}

RationalNumber operator-(RationalNumber r1, RationalNumber r2) {

int a, b, c, d;
a = r1.numer;
b = r1.denom;
c = r2.numer;
d = r2.denom;
int subnumer = (a * d - b * c);
int subdenom = (b * d);
RationalNumber r(subnumer, subdenom);
return r;

}

RationalNumber operator*(RationalNumber a, RationalNumber b) {

RationalNumber result((a.numer*b.numer) , (a.denom*b.denom));

return result;

}

RationalNumber operator/(RationalNumber a, RationalNumber b) {

RationalNumber result((a.numer*b.denom) , (a.denom*b.numer));

return result;

}

void RationalNumber::printRational()
{
int g=gcd(numer,denom);
if(denom!=1)
cout<<numer/g<<"/"<<denom/g;
else
cout<<numer/g;
}

int RationalNumber::gcd(int a, int b)
{
// % is modulus which is the remainder of a division
// base case
if ((a % b) == 0)
{
return b;
}
// recursive case
else
{
return gcd(b, a % b);
}
}

_____________________

main.cpp

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

int main()

{

RationalNumber c( 7, 3 ), d( 3, 9 ), x;

c.printRational();

cout << " + " ;

d.printRational();

cout << " = ";

x = c + d; // test overloaded operators + and =

x.printRational();

cout << '\n';

c.printRational();

cout << " - " ;

d.printRational();

cout << " = ";

x = c - d; // test overloaded operators - and =

x.printRational();

cout << '\n';

c.printRational();

cout << " * " ;

d.printRational();

cout << " = ";

x = c * d; // test overloaded operators * and =

x.printRational();

cout << '\n';

c.printRational();

cout << " / " ;

d.printRational();

cout << " = ";

x = c / d; // test overloaded operators / and =

x.printRational();

cout << '\n';

c.printRational();

cout << " is:\n";

// test overloaded greater than operator

cout << ( ( c > d ) ? " > " : " <= " );

d.printRational();

cout << " according to the overloaded > operator\n";

// test overloaded less than operator

cout << ( ( c < d ) ? " < " : " >= " );

d.printRational();

cout << " according to the overloaded < operator\n";

// test overloaded greater than or equal to operator

cout << ( ( c >= d ) ? " >= " : " < " );

d.printRational();

cout << " according to the overloaded >= operator\n";

// test overloaded less than or equal to operator

cout << ( ( c <= d ) ? " <= " : " > " );

d.printRational();

cout << " according to the overloaded <= operator\n";

// test overloaded equality operator

cout << ( ( c == d ) ? " == " : " != " );

d.printRational();

cout << " according to the overloaded == operator\n";

// test overloaded inequality operator

cout << ( ( c != d ) ? " != " : " == " );

d.printRational();

cout << " according to the overloaded != operator" << endl;

cin.get();

// system("PAUSE");

return 0;

} // end main

________________

output:

RationalNumberOperatorOverloadAddSubMulDivComparisions /3-1/32/1 /3 1/37/9 /3/1/3?/1 7/3 is 1/3 according to the overloaded >

_______________Thank You

Add a comment
Know the answer?
Add Answer to:
Goals . Understand how to implement operator overloading in C++ Warning: Programs must compile us...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Submission Instruction Complete the following C++ programs. The assignment contains only one file with all different...

    Submission Instruction Complete the following C++ programs. The assignment contains only one file with all different class and functions from problem 1. The main function calls different functions as instructed in the problem description. Submit the CPP file during submission Problem 1. 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 the denominator (one for each data). Also...

  • C++ Create a Rational Number (fractions) class like the one in Exercise 9.6 of the textbook....

    C++ Create a Rational Number (fractions) class like the one in Exercise 9.6 of the textbook. Provide the following capabilities: Create a constructor that prevents a 0 denominator in a fraction, reduces or simplifies fractions (by dividing the numerator and the denominator by their greatest common divisor) that are not in reduced form, and avoids negative denominators. Overload the addition, subtraction, multiplication, and division operators for this class. Overload the relational and equality operators for this class. Provide a function...

  • Write a Fraction class. An example of a fraction is 1/2. Note that C/C++ will convert...

    Write a Fraction class. An example of a fraction is 1/2. Note that C/C++ will convert it to 0.5, but for this problem, it should still be displayed as 1/2. You should have at least the following two private member variables: numerator (top part), and denominator (bottom part). Overload the following operators: ==, +, << and >>. Also, implement the default constructor and a second constructor that takes two arguments for the numerator and the denominator. Make sure the denominator...

  • (Rational class) Create a class called Rational for performing arithmetic with fractions. Write a program to...

    (Rational class) Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integers variables to represent the private data of the class- the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should contain default values in case are no initializers are provided and should store the fraction in reduced form. For example, the fraction 3/6 would be...

  • If you have already answered this question, please do not repost your old solutions, you will be thumbs downed. I'm...

    If you have already answered this question, please do not repost your old solutions, you will be thumbs downed. I'm looking for NEW solutions only! Frac.h: // a Fraction object holds one Fraction number, one fraction #ifndef FRAC_H #define FRAC_H #include <iostream> using namespace std; //Creaing a Fraction class class Fraction { public: Fraction(int = 0, int = 1); // Function Declarations which performs operations on Fraction class Fraction add(const Fraction &); Fraction subtract(const Fraction& a); Fraction multiply(const Fraction& a);...

  • Header file for the Rational class: #ifndef RATIONAL_H #define RATIONAL_H class Rational { public: Rational( int...

    Header file for the Rational class: #ifndef RATIONAL_H #define RATIONAL_H class Rational { public: Rational( int = 0, int = 1 ); // default constructor Rational addition( const Rational & ) const; // function addition Rational subtraction( const Rational & ) const; // function subtraction Rational multiplication( const Rational & ) const; // function multi. Rational division( const Rational & ) const; // function division void printRational () const; // print rational format void printRationalAsDouble() const; // print rational as...

  • C++ Question Your class should support the following operations on Fraction objects: • Construction of a...

    C++ Question Your class should support the following operations on Fraction objects: • Construction of a Fraction from two, one, or zero integer arguments. If two arguments, they are assumed to be the numerator and denominator, just one is assumed to be a whole number, and zero arguments creates a zero Fraction. Use default parameters so that you only need a single function to implement all three of these constructors. You should check to make sure that the denominator is...

  • C++...please help follow exact steps output needs to be the exact same Create a class called...

    C++...please help follow exact steps output needs to be the exact same Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private data of the elass the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should contain default values in case no initializers are provided and should store the fraction in reduced...

  • PLEASE USE OBJECT ORIENTED PROGRAMMING IN C# USING MICROSOFT VISUAL. *****PLEASE USE C#(C SHARP)*...

    PLEASE USE OBJECT ORIENTED PROGRAMMING IN C# USING MICROSOFT VISUAL. *****PLEASE USE C#(C SHARP)***** Also please dont use BigInteger thanks it's really appreciated for all the help PLEASE MAKE IT SO ITS USER INPUT Create a class called Rational for prforming arithmetie with fractions. Write an app to test your class Use integer variables to represent the private instance variables of the class-the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized...

  • In C++ Fix any errors you had with HW5(Fraction class). Implement a function(s) to help with...

    In C++ Fix any errors you had with HW5(Fraction class). Implement a function(s) to help with Fraction addition. \**************Homework 5 code*****************************/ #include<iostream> using namespace std; class Fraction { private: int wholeNumber, numerator, denominator;    public: //get methods int getWholeNumber() { return wholeNumber; } int getNumerator() { return numerator; } int getDenominator() { return denominator; } Fraction()// default constructor { int w,n,d; cout<<"\nEnter whole number : "; cin>>w; cout<<"\nEnter numerator : "; cin>>n; cout<<"\nEnter denominator : "; cin>>d; while(d == 0)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT