SlideShare a Scribd company logo
1 of 12
Download to read offline
#ifndef RATIONAL_H // if this compiler macro is not defined
#define RATIONAL_H // then define it so this file will not be processed again
#include "stdafx.h" // use only for Microsoft Visual Studio C++
#include
using namespace std;
class Rational
{
// Friend functions are actually declared outside the scope of the
// class but have the right to access public and private data and
// member function members that belong to the class. The friend
// function below gives the << operator for ostreams (including cout)
// the ability to output a Rational object by accessing its member data.
friend ostream &operator<< (ostream &out, Rational const &r);
public:
Rational(int num = 0, int denom = 1); // also provides default constructor
Rational add(Rational right);
Rational operator+ (Rational right); // + addition operator
Rational operator+= (Rational right); // += addition assignment operator
Rational operator- (Rational right); // + addition operator
Rational operator-= (Rational right); // += addition assignment operator
void display();
operator double() const; // convert Rational to double
private:
int numerator;
int denominator;
// helper functions are private and not accessible by the main program
int LCD(int v1, int v2);
Rational setRational(int n, int d);
};
#endif
#include "stdafx.h"
#include
#include "Rational.h"
using namespace std;
// By using the default parameter settings in Rational.h, this
// constructor also provides the default constructor Rational()
Rational::Rational(int num, int denom)
{
setRational(num, denom); // set numerator and denominator, reduce fraction, fix the sign
}
// Helper function to fix a zero denominator and fix the sign if denominator is negative
Rational Rational::setRational(int n, int d) // helper function
{
numerator = n;
denominator = d;
// if denominator == 0 then set it = 1
if (denominator == 0)
denominator = 1;
if (denominator < 0) // if denominator is neg, multiply num and denom by -1
{
numerator = -numerator; // fix sign of numerator +/-
denominator = -denominator; // denominator always +
}
int lcd = LCD(numerator, denominator);
if (denominator != 0)
{
numerator /= lcd;
denominator /= lcd;
}
return *this; // return the current object
}
// find the lowest common divisor using a recursive function
int Rational::LCD(int v1, int v2)
{
if (v2 == 0) return v1;
else return LCD(v2, v1%v2);
}
Rational Rational::add(Rational right)
{
int newNumerator;
int newDenominator;
newNumerator = numerator*right.denominator + right.numerator*denominator;
newDenominator = denominator * right.denominator;
// create a new Rational object and return it
return setRational(newNumerator, newDenominator);
}
// the operator+ method does the same thing as the add method
Rational Rational::operator+ (Rational right)
{
int newNumerator;
int newDenominator;
newNumerator = numerator*right.denominator + right.numerator*denominator;
newDenominator = denominator * right.denominator;
// create a new Rational object and return it
return setRational(newNumerator, newDenominator);
}
Rational Rational::operator+= (Rational right)
{
numerator = numerator*right.denominator + right.numerator*denominator;
denominator = denominator * right.denominator;
// fix the sign, reduce the fraction and return the current object
return setRational(numerator, denominator);
}
// the operator- method does the same thing as the add method
Rational Rational::operator- (Rational right)
{
int newNumerator;
int newDenominator;
newNumerator = numerator*right.denominator - right.numerator*denominator;
newDenominator = denominator * right.denominator;
// create a new Rational object and return it
return setRational(newNumerator, newDenominator);
}
Rational Rational::operator-= (Rational right)
{
numerator = numerator*right.denominator - right.numerator*denominator;
denominator = denominator * right.denominator;
// fix the sign, reduce the fraction and return the current object
return setRational(numerator, denominator);
}
Rational::operator double() const // convert Rational to double and return
{
return double(numerator) / double(denominator);
}
// Display a Rational number using the display() member method
void Rational::display()
{
cout << numerator << '/' << denominator;
}
// Display a Rational number using << and a friend function.
// Friend functions are not part of the class and their code must be
// declared outside of the class with no :: Scope Resolution Operator.
// All function arguments must have their type/class defined
ostream &operator<< (ostream &out, Rational const &r)
{
out << r.numerator << '/' << r.denominator;
return out;
}
// Rational.cpp : Defines the entry point for the console application.
// Create a Rational class defination
// Rational(numerator, denominator)
//
#include "stdafx.h" // only for Microsoft Visual Studio C++
#include "Rational.h" // double quotes = find file in project folder
#include // angle brackets = find file in compiler folder
using namespace std;
// function prototypes
void displayNumbers(double, Rational, Rational, Rational, Rational);
void display(Rational);
int main(int argc, char* argv[])
{
// class object
// | |
// V V
double num1 = 1.5; // sample definition of a double number
Rational num2; // call the constructor with no arguments
Rational num3(3, 4); // call the constructor setting num3 to 3/4
Rational num4(2, 3); // call the constructor setting num4 to 2/3
Rational num5; // call the constructor with no arguments
display(num1);
displayNumbers(num1, num2, num3, num4, num5);
cout << "verify that addition works correctly" << endl;
// use the add member method (without overloading)
num2 = num3.add(num4); // num3 + num4 = 3/4 + 2/3 = 9/12 + 8/12 = 17/12
cout << "num2 = num3.add(num4)" << endl << "num2,display();" << endl;
num2.display(); // using the display( ) member function
cout << endl << endl;
// use the operator+ method
num2 = num3.operator+(num4); // num3 + num4 = 3/4 + 2/3 = 9/12 + 8/12 = 17/12
cout << "num3.operator+(num4)" << endl;
displayNumbers(num1, num2, num3, num4, num5);
// use the + overloaded operator, use the friend operator << to display the result
num2 = num3 + num4; // num3 + num4 = 3/4 + 2/3 = 9/12 + 8/12 = 17/12
cout << "num2 = num3 + num4" << endl;
displayNumbers(num1, num2, num3, num4, num5);
// use the += overloaded operator, use the friend operator << to display the result
num5 = num3 += num4; // num3 + num4 = 3/4 + 2/3 = 9/12 + 8/12 = 17/12
cout << "num5 = num3 += num4" << endl;
displayNumbers(num1, num2, num3, num4, num5);
num3 = Rational(3, 4);
cout << "Reset num3 back to 3/4" << endl;
displayNumbers(num1, num2, num3, num4, num5);
cout << endl << "---------------------------------------" << endl;
cout << "verify that subtraction works correctly" << endl;
// use the - overloaded operator, use the friend operator << to display the result
num2 = num3 - num4; // num3 + num4 = 3/4 - 2/3 = 9/12 - 8/12 = 1/12
cout << "num2 = num3 - num4" << endl;
displayNumbers(num1, num2, num3, num4, num5);
// use the -= overloaded operator, use the friend operator << to display the result
num5 = num3 -= num4; // num3 - num4 = 3/4 - 2/3 = 9/12 - 8/12 = 1/12
cout << "num5 = num3 += num4" << endl;
displayNumbers(num1, num2, num3, num4, num5);
num3 = Rational(3, 4);
cout << "Reset num3 back to 3/4" << endl;
displayNumbers(num1, num2, num3, num4, num5);
// convert the Rational number to double
cout << "double(num2) = " << double(num2) << endl; // 17/12 = 1.4166
cout << endl;
return 0;
}
void display(Rational num1)
{
cout << "num1 = " << num1 << endl;
}
void displayNumbers(double num1, Rational num2, Rational num3,
Rational num4, Rational num5)
{
cout << "num1t num2t num3t num4t num5 ";
cout << num1 << "t " << num2 << "t " << num3 << "t "
<< num4 << "t " << num5 << endl << endl;
}
Solution
#ifndef RATIONAL_H
#define RATIONAL_H
#include "stdafx.h"
#include
using namespace std;
class Rational
{
friend ostream &operator<< (ostream &out, Rational const &r);
public:
Rational(int num = 0, int denom = 1); // also provides default constructor
Rational add(Rational right);
Rational operator+ (Rational right); // + addition operator
Rational operator+= (Rational right); // += addition assignment operator
Rational operator- (Rational right); // + addition operator
Rational operator-= (Rational right); // += addition assignment operator
void display();
operator double() const; // convert Rational to double
private:
int numerator;
int denominator;
// helper functions are private and not accessible by the main program
int LCD(int v1, int v2);
Rational setRational(int n, int d);
};
#endif
#include "stdafx.h"
#include
#include "Rational.h"
using namespace std;
Rational::Rational(int num, int denom)
{
setRational(num, denom);
}
Rational Rational::setRational(int n, int d)
{
numerator = n;
denominator = d;
// if denominator == 0 then set it = 1
if (denominator == 0)
denominator = 1;
if (denominator < 0) // if denominator is neg, multiply num and denom by -1
{
numerator = -numerator; // fix sign of numerator +/-
denominator = -denominator; // denominator always +
}
int lcd = LCD(numerator, denominator);
if (denominator != 0)
{
numerator /= lcd;
denominator /= lcd;
}
return *this; // return the current object
}
// find the lowest common divisor using a recursive function
int Rational::LCD(int v1, int v2)
{
if (v2 == 0) return v1;
else return LCD(v2, v1%v2);
}
Rational Rational::add(Rational right)
{
int newNumerator;
int newDenominator;
newNumerator = numerator*right.denominator + right.numerator*denominator;
newDenominator = denominator * right.denominator;
// create a new Rational object and return it
return setRational(newNumerator, newDenominator);
}
// the operator+ method does the same thing as the add method
Rational Rational::operator+ (Rational right)
{
int newNumerator;
int newDenominator;
newNumerator = numerator*right.denominator + right.numerator*denominator;
newDenominator = denominator * right.denominator;
// create a new Rational object and return it
return setRational(newNumerator, newDenominator);
}
Rational Rational::operator+= (Rational right)
{
numerator = numerator*right.denominator + right.numerator*denominator;
denominator = denominator * right.denominator;
// fix the sign, reduce the fraction and return the current object
return setRational(numerator, denominator);
}
// the operator- method does the same thing as the add method
Rational Rational::operator- (Rational right)
{
int newNumerator;
int newDenominator;
newNumerator = numerator*right.denominator - right.numerator*denominator;
newDenominator = denominator * right.denominator;
// create a new Rational object and return it
return setRational(newNumerator, newDenominator);
}
Rational Rational::operator-= (Rational right)
{
numerator = numerator*right.denominator - right.numerator*denominator;
denominator = denominator * right.denominator;
// fix the sign, reduce the fraction and return the current object
return setRational(numerator, denominator);
}
Rational::operator double() const // convert Rational to double and return
{
return double(numerator) / double(denominator);
}
// Display a Rational number using the display() member method
void Rational::display()
{
cout << numerator << '/' << denominator;
}
// Display a Rational number using << and a friend function.
// Friend functions are not part of the class and their code must be
// declared outside of the class with no :: Scope Resolution Operator.
// All function arguments must have their type/class defined
ostream &operator<< (ostream &out, Rational const &r)
{
out << r.numerator << '/' << r.denominator;
return out;
}
#include "stdafx.h" // only for Microsoft Visual Studio C++
#include "Rational.h" // double quotes = find file in project folder
#include // angle brackets = find file in compiler folder
using namespace std;
// function prototypes
void displayNumbers(double, Rational, Rational, Rational, Rational);
void display(Rational);
int main(int argc, char* argv[])
{
// class object
// | |
// V V
double num1 = 1.5; // sample definition of a double number
Rational num2; // call the constructor with no arguments
Rational num3(3, 4); // call the constructor setting num3 to 3/4
Rational num4(2, 3); // call the constructor setting num4 to 2/3
Rational num5; // call the constructor with no arguments
display(num1);
displayNumbers(num1, num2, num3, num4, num5);
cout << "verify that addition works correctly" << endl;
// use the add member method (without overloading)
num2 = num3.add(num4); // num3 + num4 = 3/4 + 2/3 = 9/12 + 8/12 = 17/12
cout << "num2 = num3.add(num4)" << endl << "num2,display();" << endl;
num2.display(); // using the display( ) member function
cout << endl << endl;
// use the operator+ method
num2 = num3.operator+(num4); // num3 + num4 = 3/4 + 2/3 = 9/12 + 8/12 = 17/12
cout << "num3.operator+(num4)" << endl;
displayNumbers(num1, num2, num3, num4, num5);
// use the + overloaded operator, use the friend operator << to display the result
num2 = num3 + num4; // num3 + num4 = 3/4 + 2/3 = 9/12 + 8/12 = 17/12
cout << "num2 = num3 + num4" << endl;
displayNumbers(num1, num2, num3, num4, num5);
// use the += overloaded operator, use the friend operator << to display the result
num5 = num3 += num4; // num3 + num4 = 3/4 + 2/3 = 9/12 + 8/12 = 17/12
cout << "num5 = num3 += num4" << endl;
displayNumbers(num1, num2, num3, num4, num5);
num3 = Rational(3, 4);
cout << "Reset num3 back to 3/4" << endl;
displayNumbers(num1, num2, num3, num4, num5);
cout << endl << "---------------------------------------" << endl;
cout << "verify that subtraction works correctly" << endl;
// use the - overloaded operator, use the friend operator << to display the result
num2 = num3 - num4; // num3 + num4 = 3/4 - 2/3 = 9/12 - 8/12 = 1/12
cout << "num2 = num3 - num4" << endl;
displayNumbers(num1, num2, num3, num4, num5);
// use the -= overloaded operator, use the friend operator << to display the result
num5 = num3 -= num4; // num3 - num4 = 3/4 - 2/3 = 9/12 - 8/12 = 1/12
cout << "num5 = num3 += num4" << endl;
displayNumbers(num1, num2, num3, num4, num5);
num3 = Rational(3, 4);
cout << "Reset num3 back to 3/4" << endl;
displayNumbers(num1, num2, num3, num4, num5);
// convert the Rational number to double
cout << "double(num2) = " << double(num2) << endl; // 17/12 = 1.4166
cout << endl;
return 0;
}
void display(Rational num1)
{
cout << "num1 = " << num1 << endl;
}
void displayNumbers(double num1, Rational num2, Rational num3,
Rational num4, Rational num5)
{
cout << "num1t num2t num3t num4t num5 ";
cout << num1 << "t " << num2 << "t " << num3 << "t "
<< num4 << "t " << num5 << endl << endl;
}

More Related Content

Similar to #ifndef RATIONAL_H   if this compiler macro is not defined #def.pdf

C++ Function
C++ FunctionC++ Function
C++ FunctionHajar
 
CPP Programming Homework Help
CPP Programming Homework HelpCPP Programming Homework Help
CPP Programming Homework HelpC++ Homework Help
 
C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdf
C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdfC++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdf
C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdfandreaplotner1
 
2.overview of c++ ________lecture2
2.overview of c++  ________lecture22.overview of c++  ________lecture2
2.overview of c++ ________lecture2Warui Maina
 
Assignment 13assg-13.cppAssignment 13assg-13.cpp   @auth.docx
Assignment 13assg-13.cppAssignment 13assg-13.cpp   @auth.docxAssignment 13assg-13.cppAssignment 13assg-13.cpp   @auth.docx
Assignment 13assg-13.cppAssignment 13assg-13.cpp   @auth.docxbraycarissa250
 
PROGRAM 2 – Fraction Class Problem For this programming as.pdf
PROGRAM 2 – Fraction Class Problem For this programming as.pdfPROGRAM 2 – Fraction Class Problem For this programming as.pdf
PROGRAM 2 – Fraction Class Problem For this programming as.pdfezonesolutions
 
C++aptitude questions and answers
C++aptitude questions and answersC++aptitude questions and answers
C++aptitude questions and answerssheibansari
 
Practical basics on c++
Practical basics on c++Practical basics on c++
Practical basics on c++Marco Izzotti
 

Similar to #ifndef RATIONAL_H   if this compiler macro is not defined #def.pdf (20)

7 functions
7  functions7  functions
7 functions
 
C++ Function
C++ FunctionC++ Function
C++ Function
 
Computer Science Assignment Help
 Computer Science Assignment Help  Computer Science Assignment Help
Computer Science Assignment Help
 
CPP Programming Homework Help
CPP Programming Homework HelpCPP Programming Homework Help
CPP Programming Homework Help
 
CP 04.pptx
CP 04.pptxCP 04.pptx
CP 04.pptx
 
C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdf
C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdfC++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdf
C++ code only(Retrieve of Malik D., 2015, p. 742) Programming Exer.pdf
 
C++ Functions.ppt
C++ Functions.pptC++ Functions.ppt
C++ Functions.ppt
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
operator overloading
operator overloadingoperator overloading
operator overloading
 
2.overview of c++ ________lecture2
2.overview of c++  ________lecture22.overview of c++  ________lecture2
2.overview of c++ ________lecture2
 
Assignment 13assg-13.cppAssignment 13assg-13.cpp   @auth.docx
Assignment 13assg-13.cppAssignment 13assg-13.cpp   @auth.docxAssignment 13assg-13.cppAssignment 13assg-13.cpp   @auth.docx
Assignment 13assg-13.cppAssignment 13assg-13.cpp   @auth.docx
 
PROGRAM 2 – Fraction Class Problem For this programming as.pdf
PROGRAM 2 – Fraction Class Problem For this programming as.pdfPROGRAM 2 – Fraction Class Problem For this programming as.pdf
PROGRAM 2 – Fraction Class Problem For this programming as.pdf
 
C++
C++C++
C++
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 
Array Cont
Array ContArray Cont
Array Cont
 
C++aptitude questions and answers
C++aptitude questions and answersC++aptitude questions and answers
C++aptitude questions and answers
 
C++ aptitude
C++ aptitudeC++ aptitude
C++ aptitude
 
Fp201 unit5 1
Fp201 unit5 1Fp201 unit5 1
Fp201 unit5 1
 
Practical basics on c++
Practical basics on c++Practical basics on c++
Practical basics on c++
 
CppTutorial.ppt
CppTutorial.pptCppTutorial.ppt
CppTutorial.ppt
 

More from exxonzone

An employee is assigned to one department and a department may have m.pdf
An employee is assigned to one department and a department may have m.pdfAn employee is assigned to one department and a department may have m.pdf
An employee is assigned to one department and a department may have m.pdfexxonzone
 
10 Points ntilfy cach of the the contour signatures shown on the map .pdf
10 Points ntilfy cach of the the contour signatures shown on the map .pdf10 Points ntilfy cach of the the contour signatures shown on the map .pdf
10 Points ntilfy cach of the the contour signatures shown on the map .pdfexxonzone
 
Describe the principal cash transfer tools-Wire Transfer-EDT-E.pdf
Describe the principal cash transfer tools-Wire Transfer-EDT-E.pdfDescribe the principal cash transfer tools-Wire Transfer-EDT-E.pdf
Describe the principal cash transfer tools-Wire Transfer-EDT-E.pdfexxonzone
 
Which sentence contains an infinitive AWill you take the package to.pdf
Which sentence contains an infinitive AWill you take the package to.pdfWhich sentence contains an infinitive AWill you take the package to.pdf
Which sentence contains an infinitive AWill you take the package to.pdfexxonzone
 
Whats the Schema Table Column Row Attribute Entity Pri.pdf
Whats the Schema  Table  Column  Row  Attribute  Entity  Pri.pdfWhats the Schema  Table  Column  Row  Attribute  Entity  Pri.pdf
Whats the Schema Table Column Row Attribute Entity Pri.pdfexxonzone
 
Which of the following groups produce sporangia on leaves (Mark all.pdf
Which of the following groups produce sporangia on leaves (Mark all.pdfWhich of the following groups produce sporangia on leaves (Mark all.pdf
Which of the following groups produce sporangia on leaves (Mark all.pdfexxonzone
 
What is tax research What is the purpose of conducting tax research.pdf
What is tax research What is the purpose of conducting tax research.pdfWhat is tax research What is the purpose of conducting tax research.pdf
What is tax research What is the purpose of conducting tax research.pdfexxonzone
 
What is a social networking analysisa) represents the interconnec.pdf
What is a social networking analysisa) represents the interconnec.pdfWhat is a social networking analysisa) represents the interconnec.pdf
What is a social networking analysisa) represents the interconnec.pdfexxonzone
 
BiopsychologyWhy sleep (psychology) is interestingSolutionS.pdf
BiopsychologyWhy sleep (psychology) is interestingSolutionS.pdfBiopsychologyWhy sleep (psychology) is interestingSolutionS.pdf
BiopsychologyWhy sleep (psychology) is interestingSolutionS.pdfexxonzone
 
The following question does not require any knowledge of statistics t.pdf
The following question does not require any knowledge of statistics t.pdfThe following question does not require any knowledge of statistics t.pdf
The following question does not require any knowledge of statistics t.pdfexxonzone
 
Question I need help with c++ Simple Classes Assigment. i get this .pdf
Question I need help with c++ Simple Classes Assigment. i get this .pdfQuestion I need help with c++ Simple Classes Assigment. i get this .pdf
Question I need help with c++ Simple Classes Assigment. i get this .pdfexxonzone
 
Do antidepressants help A researcher studied the effect of an an.pdf
Do antidepressants help A researcher studied the effect of an an.pdfDo antidepressants help A researcher studied the effect of an an.pdf
Do antidepressants help A researcher studied the effect of an an.pdfexxonzone
 
Shensen and colleagues were interested in studying older adults.pdf
Shensen and colleagues were interested in studying older adults.pdfShensen and colleagues were interested in studying older adults.pdf
Shensen and colleagues were interested in studying older adults.pdfexxonzone
 
Much of the heat that drives convection in the mantlea. Is generat.pdf
Much of the heat that drives convection in the mantlea. Is generat.pdfMuch of the heat that drives convection in the mantlea. Is generat.pdf
Much of the heat that drives convection in the mantlea. Is generat.pdfexxonzone
 
In explaining what we now call evolution, Darwin often used the phra.pdf
In explaining what we now call evolution, Darwin often used the phra.pdfIn explaining what we now call evolution, Darwin often used the phra.pdf
In explaining what we now call evolution, Darwin often used the phra.pdfexxonzone
 
Las Homework 4 Spotlight Figure 27.18 Diagnosis of Acid-Base Disorde.pdf
Las Homework 4 Spotlight Figure 27.18 Diagnosis of Acid-Base Disorde.pdfLas Homework 4 Spotlight Figure 27.18 Diagnosis of Acid-Base Disorde.pdf
Las Homework 4 Spotlight Figure 27.18 Diagnosis of Acid-Base Disorde.pdfexxonzone
 
Check all that are characteristics of cardiac muscle. Cells are .pdf
Check all that are characteristics of cardiac muscle.  Cells are .pdfCheck all that are characteristics of cardiac muscle.  Cells are .pdf
Check all that are characteristics of cardiac muscle. Cells are .pdfexxonzone
 
How electrical devices produce complex waveforms. Describe.Solut.pdf
How electrical devices produce complex waveforms. Describe.Solut.pdfHow electrical devices produce complex waveforms. Describe.Solut.pdf
How electrical devices produce complex waveforms. Describe.Solut.pdfexxonzone
 
Goldstein and brown were lucky. If they had used the JD allele to co.pdf
Goldstein and brown were lucky. If they had used the JD allele to co.pdfGoldstein and brown were lucky. If they had used the JD allele to co.pdf
Goldstein and brown were lucky. If they had used the JD allele to co.pdfexxonzone
 

More from exxonzone (19)

An employee is assigned to one department and a department may have m.pdf
An employee is assigned to one department and a department may have m.pdfAn employee is assigned to one department and a department may have m.pdf
An employee is assigned to one department and a department may have m.pdf
 
10 Points ntilfy cach of the the contour signatures shown on the map .pdf
10 Points ntilfy cach of the the contour signatures shown on the map .pdf10 Points ntilfy cach of the the contour signatures shown on the map .pdf
10 Points ntilfy cach of the the contour signatures shown on the map .pdf
 
Describe the principal cash transfer tools-Wire Transfer-EDT-E.pdf
Describe the principal cash transfer tools-Wire Transfer-EDT-E.pdfDescribe the principal cash transfer tools-Wire Transfer-EDT-E.pdf
Describe the principal cash transfer tools-Wire Transfer-EDT-E.pdf
 
Which sentence contains an infinitive AWill you take the package to.pdf
Which sentence contains an infinitive AWill you take the package to.pdfWhich sentence contains an infinitive AWill you take the package to.pdf
Which sentence contains an infinitive AWill you take the package to.pdf
 
Whats the Schema Table Column Row Attribute Entity Pri.pdf
Whats the Schema  Table  Column  Row  Attribute  Entity  Pri.pdfWhats the Schema  Table  Column  Row  Attribute  Entity  Pri.pdf
Whats the Schema Table Column Row Attribute Entity Pri.pdf
 
Which of the following groups produce sporangia on leaves (Mark all.pdf
Which of the following groups produce sporangia on leaves (Mark all.pdfWhich of the following groups produce sporangia on leaves (Mark all.pdf
Which of the following groups produce sporangia on leaves (Mark all.pdf
 
What is tax research What is the purpose of conducting tax research.pdf
What is tax research What is the purpose of conducting tax research.pdfWhat is tax research What is the purpose of conducting tax research.pdf
What is tax research What is the purpose of conducting tax research.pdf
 
What is a social networking analysisa) represents the interconnec.pdf
What is a social networking analysisa) represents the interconnec.pdfWhat is a social networking analysisa) represents the interconnec.pdf
What is a social networking analysisa) represents the interconnec.pdf
 
BiopsychologyWhy sleep (psychology) is interestingSolutionS.pdf
BiopsychologyWhy sleep (psychology) is interestingSolutionS.pdfBiopsychologyWhy sleep (psychology) is interestingSolutionS.pdf
BiopsychologyWhy sleep (psychology) is interestingSolutionS.pdf
 
The following question does not require any knowledge of statistics t.pdf
The following question does not require any knowledge of statistics t.pdfThe following question does not require any knowledge of statistics t.pdf
The following question does not require any knowledge of statistics t.pdf
 
Question I need help with c++ Simple Classes Assigment. i get this .pdf
Question I need help with c++ Simple Classes Assigment. i get this .pdfQuestion I need help with c++ Simple Classes Assigment. i get this .pdf
Question I need help with c++ Simple Classes Assigment. i get this .pdf
 
Do antidepressants help A researcher studied the effect of an an.pdf
Do antidepressants help A researcher studied the effect of an an.pdfDo antidepressants help A researcher studied the effect of an an.pdf
Do antidepressants help A researcher studied the effect of an an.pdf
 
Shensen and colleagues were interested in studying older adults.pdf
Shensen and colleagues were interested in studying older adults.pdfShensen and colleagues were interested in studying older adults.pdf
Shensen and colleagues were interested in studying older adults.pdf
 
Much of the heat that drives convection in the mantlea. Is generat.pdf
Much of the heat that drives convection in the mantlea. Is generat.pdfMuch of the heat that drives convection in the mantlea. Is generat.pdf
Much of the heat that drives convection in the mantlea. Is generat.pdf
 
In explaining what we now call evolution, Darwin often used the phra.pdf
In explaining what we now call evolution, Darwin often used the phra.pdfIn explaining what we now call evolution, Darwin often used the phra.pdf
In explaining what we now call evolution, Darwin often used the phra.pdf
 
Las Homework 4 Spotlight Figure 27.18 Diagnosis of Acid-Base Disorde.pdf
Las Homework 4 Spotlight Figure 27.18 Diagnosis of Acid-Base Disorde.pdfLas Homework 4 Spotlight Figure 27.18 Diagnosis of Acid-Base Disorde.pdf
Las Homework 4 Spotlight Figure 27.18 Diagnosis of Acid-Base Disorde.pdf
 
Check all that are characteristics of cardiac muscle. Cells are .pdf
Check all that are characteristics of cardiac muscle.  Cells are .pdfCheck all that are characteristics of cardiac muscle.  Cells are .pdf
Check all that are characteristics of cardiac muscle. Cells are .pdf
 
How electrical devices produce complex waveforms. Describe.Solut.pdf
How electrical devices produce complex waveforms. Describe.Solut.pdfHow electrical devices produce complex waveforms. Describe.Solut.pdf
How electrical devices produce complex waveforms. Describe.Solut.pdf
 
Goldstein and brown were lucky. If they had used the JD allele to co.pdf
Goldstein and brown were lucky. If they had used the JD allele to co.pdfGoldstein and brown were lucky. If they had used the JD allele to co.pdf
Goldstein and brown were lucky. If they had used the JD allele to co.pdf
 

Recently uploaded

Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 

Recently uploaded (20)

Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 

#ifndef RATIONAL_H   if this compiler macro is not defined #def.pdf

  • 1. #ifndef RATIONAL_H // if this compiler macro is not defined #define RATIONAL_H // then define it so this file will not be processed again #include "stdafx.h" // use only for Microsoft Visual Studio C++ #include using namespace std; class Rational { // Friend functions are actually declared outside the scope of the // class but have the right to access public and private data and // member function members that belong to the class. The friend // function below gives the << operator for ostreams (including cout) // the ability to output a Rational object by accessing its member data. friend ostream &operator<< (ostream &out, Rational const &r); public: Rational(int num = 0, int denom = 1); // also provides default constructor Rational add(Rational right); Rational operator+ (Rational right); // + addition operator Rational operator+= (Rational right); // += addition assignment operator Rational operator- (Rational right); // + addition operator Rational operator-= (Rational right); // += addition assignment operator void display(); operator double() const; // convert Rational to double private: int numerator; int denominator; // helper functions are private and not accessible by the main program int LCD(int v1, int v2); Rational setRational(int n, int d); }; #endif #include "stdafx.h" #include #include "Rational.h" using namespace std; // By using the default parameter settings in Rational.h, this
  • 2. // constructor also provides the default constructor Rational() Rational::Rational(int num, int denom) { setRational(num, denom); // set numerator and denominator, reduce fraction, fix the sign } // Helper function to fix a zero denominator and fix the sign if denominator is negative Rational Rational::setRational(int n, int d) // helper function { numerator = n; denominator = d; // if denominator == 0 then set it = 1 if (denominator == 0) denominator = 1; if (denominator < 0) // if denominator is neg, multiply num and denom by -1 { numerator = -numerator; // fix sign of numerator +/- denominator = -denominator; // denominator always + } int lcd = LCD(numerator, denominator); if (denominator != 0) { numerator /= lcd; denominator /= lcd; } return *this; // return the current object } // find the lowest common divisor using a recursive function int Rational::LCD(int v1, int v2) { if (v2 == 0) return v1; else return LCD(v2, v1%v2); } Rational Rational::add(Rational right) { int newNumerator; int newDenominator;
  • 3. newNumerator = numerator*right.denominator + right.numerator*denominator; newDenominator = denominator * right.denominator; // create a new Rational object and return it return setRational(newNumerator, newDenominator); } // the operator+ method does the same thing as the add method Rational Rational::operator+ (Rational right) { int newNumerator; int newDenominator; newNumerator = numerator*right.denominator + right.numerator*denominator; newDenominator = denominator * right.denominator; // create a new Rational object and return it return setRational(newNumerator, newDenominator); } Rational Rational::operator+= (Rational right) { numerator = numerator*right.denominator + right.numerator*denominator; denominator = denominator * right.denominator; // fix the sign, reduce the fraction and return the current object return setRational(numerator, denominator); } // the operator- method does the same thing as the add method Rational Rational::operator- (Rational right) { int newNumerator; int newDenominator; newNumerator = numerator*right.denominator - right.numerator*denominator; newDenominator = denominator * right.denominator; // create a new Rational object and return it return setRational(newNumerator, newDenominator); } Rational Rational::operator-= (Rational right) { numerator = numerator*right.denominator - right.numerator*denominator; denominator = denominator * right.denominator;
  • 4. // fix the sign, reduce the fraction and return the current object return setRational(numerator, denominator); } Rational::operator double() const // convert Rational to double and return { return double(numerator) / double(denominator); } // Display a Rational number using the display() member method void Rational::display() { cout << numerator << '/' << denominator; } // Display a Rational number using << and a friend function. // Friend functions are not part of the class and their code must be // declared outside of the class with no :: Scope Resolution Operator. // All function arguments must have their type/class defined ostream &operator<< (ostream &out, Rational const &r) { out << r.numerator << '/' << r.denominator; return out; } // Rational.cpp : Defines the entry point for the console application. // Create a Rational class defination // Rational(numerator, denominator) // #include "stdafx.h" // only for Microsoft Visual Studio C++ #include "Rational.h" // double quotes = find file in project folder #include // angle brackets = find file in compiler folder using namespace std; // function prototypes void displayNumbers(double, Rational, Rational, Rational, Rational); void display(Rational); int main(int argc, char* argv[]) { // class object // | |
  • 5. // V V double num1 = 1.5; // sample definition of a double number Rational num2; // call the constructor with no arguments Rational num3(3, 4); // call the constructor setting num3 to 3/4 Rational num4(2, 3); // call the constructor setting num4 to 2/3 Rational num5; // call the constructor with no arguments display(num1); displayNumbers(num1, num2, num3, num4, num5); cout << "verify that addition works correctly" << endl; // use the add member method (without overloading) num2 = num3.add(num4); // num3 + num4 = 3/4 + 2/3 = 9/12 + 8/12 = 17/12 cout << "num2 = num3.add(num4)" << endl << "num2,display();" << endl; num2.display(); // using the display( ) member function cout << endl << endl; // use the operator+ method num2 = num3.operator+(num4); // num3 + num4 = 3/4 + 2/3 = 9/12 + 8/12 = 17/12 cout << "num3.operator+(num4)" << endl; displayNumbers(num1, num2, num3, num4, num5); // use the + overloaded operator, use the friend operator << to display the result num2 = num3 + num4; // num3 + num4 = 3/4 + 2/3 = 9/12 + 8/12 = 17/12 cout << "num2 = num3 + num4" << endl; displayNumbers(num1, num2, num3, num4, num5); // use the += overloaded operator, use the friend operator << to display the result num5 = num3 += num4; // num3 + num4 = 3/4 + 2/3 = 9/12 + 8/12 = 17/12 cout << "num5 = num3 += num4" << endl; displayNumbers(num1, num2, num3, num4, num5); num3 = Rational(3, 4); cout << "Reset num3 back to 3/4" << endl; displayNumbers(num1, num2, num3, num4, num5); cout << endl << "---------------------------------------" << endl; cout << "verify that subtraction works correctly" << endl; // use the - overloaded operator, use the friend operator << to display the result num2 = num3 - num4; // num3 + num4 = 3/4 - 2/3 = 9/12 - 8/12 = 1/12 cout << "num2 = num3 - num4" << endl; displayNumbers(num1, num2, num3, num4, num5); // use the -= overloaded operator, use the friend operator << to display the result
  • 6. num5 = num3 -= num4; // num3 - num4 = 3/4 - 2/3 = 9/12 - 8/12 = 1/12 cout << "num5 = num3 += num4" << endl; displayNumbers(num1, num2, num3, num4, num5); num3 = Rational(3, 4); cout << "Reset num3 back to 3/4" << endl; displayNumbers(num1, num2, num3, num4, num5); // convert the Rational number to double cout << "double(num2) = " << double(num2) << endl; // 17/12 = 1.4166 cout << endl; return 0; } void display(Rational num1) { cout << "num1 = " << num1 << endl; } void displayNumbers(double num1, Rational num2, Rational num3, Rational num4, Rational num5) { cout << "num1t num2t num3t num4t num5 "; cout << num1 << "t " << num2 << "t " << num3 << "t " << num4 << "t " << num5 << endl << endl; } Solution #ifndef RATIONAL_H #define RATIONAL_H #include "stdafx.h" #include using namespace std; class Rational { friend ostream &operator<< (ostream &out, Rational const &r); public: Rational(int num = 0, int denom = 1); // also provides default constructor
  • 7. Rational add(Rational right); Rational operator+ (Rational right); // + addition operator Rational operator+= (Rational right); // += addition assignment operator Rational operator- (Rational right); // + addition operator Rational operator-= (Rational right); // += addition assignment operator void display(); operator double() const; // convert Rational to double private: int numerator; int denominator; // helper functions are private and not accessible by the main program int LCD(int v1, int v2); Rational setRational(int n, int d); }; #endif #include "stdafx.h" #include #include "Rational.h" using namespace std; Rational::Rational(int num, int denom) { setRational(num, denom); } Rational Rational::setRational(int n, int d) { numerator = n; denominator = d; // if denominator == 0 then set it = 1 if (denominator == 0) denominator = 1; if (denominator < 0) // if denominator is neg, multiply num and denom by -1 { numerator = -numerator; // fix sign of numerator +/- denominator = -denominator; // denominator always +
  • 8. } int lcd = LCD(numerator, denominator); if (denominator != 0) { numerator /= lcd; denominator /= lcd; } return *this; // return the current object } // find the lowest common divisor using a recursive function int Rational::LCD(int v1, int v2) { if (v2 == 0) return v1; else return LCD(v2, v1%v2); } Rational Rational::add(Rational right) { int newNumerator; int newDenominator; newNumerator = numerator*right.denominator + right.numerator*denominator; newDenominator = denominator * right.denominator; // create a new Rational object and return it return setRational(newNumerator, newDenominator); } // the operator+ method does the same thing as the add method Rational Rational::operator+ (Rational right) { int newNumerator; int newDenominator; newNumerator = numerator*right.denominator + right.numerator*denominator; newDenominator = denominator * right.denominator; // create a new Rational object and return it return setRational(newNumerator, newDenominator); } Rational Rational::operator+= (Rational right) {
  • 9. numerator = numerator*right.denominator + right.numerator*denominator; denominator = denominator * right.denominator; // fix the sign, reduce the fraction and return the current object return setRational(numerator, denominator); } // the operator- method does the same thing as the add method Rational Rational::operator- (Rational right) { int newNumerator; int newDenominator; newNumerator = numerator*right.denominator - right.numerator*denominator; newDenominator = denominator * right.denominator; // create a new Rational object and return it return setRational(newNumerator, newDenominator); } Rational Rational::operator-= (Rational right) { numerator = numerator*right.denominator - right.numerator*denominator; denominator = denominator * right.denominator; // fix the sign, reduce the fraction and return the current object return setRational(numerator, denominator); } Rational::operator double() const // convert Rational to double and return { return double(numerator) / double(denominator); } // Display a Rational number using the display() member method void Rational::display() { cout << numerator << '/' << denominator; } // Display a Rational number using << and a friend function. // Friend functions are not part of the class and their code must be // declared outside of the class with no :: Scope Resolution Operator. // All function arguments must have their type/class defined ostream &operator<< (ostream &out, Rational const &r)
  • 10. { out << r.numerator << '/' << r.denominator; return out; } #include "stdafx.h" // only for Microsoft Visual Studio C++ #include "Rational.h" // double quotes = find file in project folder #include // angle brackets = find file in compiler folder using namespace std; // function prototypes void displayNumbers(double, Rational, Rational, Rational, Rational); void display(Rational); int main(int argc, char* argv[]) { // class object // | | // V V double num1 = 1.5; // sample definition of a double number Rational num2; // call the constructor with no arguments Rational num3(3, 4); // call the constructor setting num3 to 3/4 Rational num4(2, 3); // call the constructor setting num4 to 2/3 Rational num5; // call the constructor with no arguments display(num1); displayNumbers(num1, num2, num3, num4, num5); cout << "verify that addition works correctly" << endl; // use the add member method (without overloading) num2 = num3.add(num4); // num3 + num4 = 3/4 + 2/3 = 9/12 + 8/12 = 17/12 cout << "num2 = num3.add(num4)" << endl << "num2,display();" << endl; num2.display(); // using the display( ) member function cout << endl << endl; // use the operator+ method num2 = num3.operator+(num4); // num3 + num4 = 3/4 + 2/3 = 9/12 + 8/12 = 17/12 cout << "num3.operator+(num4)" << endl; displayNumbers(num1, num2, num3, num4, num5); // use the + overloaded operator, use the friend operator << to display the result
  • 11. num2 = num3 + num4; // num3 + num4 = 3/4 + 2/3 = 9/12 + 8/12 = 17/12 cout << "num2 = num3 + num4" << endl; displayNumbers(num1, num2, num3, num4, num5); // use the += overloaded operator, use the friend operator << to display the result num5 = num3 += num4; // num3 + num4 = 3/4 + 2/3 = 9/12 + 8/12 = 17/12 cout << "num5 = num3 += num4" << endl; displayNumbers(num1, num2, num3, num4, num5); num3 = Rational(3, 4); cout << "Reset num3 back to 3/4" << endl; displayNumbers(num1, num2, num3, num4, num5); cout << endl << "---------------------------------------" << endl; cout << "verify that subtraction works correctly" << endl; // use the - overloaded operator, use the friend operator << to display the result num2 = num3 - num4; // num3 + num4 = 3/4 - 2/3 = 9/12 - 8/12 = 1/12 cout << "num2 = num3 - num4" << endl; displayNumbers(num1, num2, num3, num4, num5); // use the -= overloaded operator, use the friend operator << to display the result num5 = num3 -= num4; // num3 - num4 = 3/4 - 2/3 = 9/12 - 8/12 = 1/12 cout << "num5 = num3 += num4" << endl; displayNumbers(num1, num2, num3, num4, num5); num3 = Rational(3, 4); cout << "Reset num3 back to 3/4" << endl; displayNumbers(num1, num2, num3, num4, num5); // convert the Rational number to double cout << "double(num2) = " << double(num2) << endl; // 17/12 = 1.4166 cout << endl; return 0; } void display(Rational num1) { cout << "num1 = " << num1 << endl; } void displayNumbers(double num1, Rational num2, Rational num3, Rational num4, Rational num5) { cout << "num1t num2t num3t num4t num5 ";
  • 12. cout << num1 << "t " << num2 << "t " << num3 << "t " << num4 << "t " << num5 << endl << endl; }