Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Make copies of Rational.h , and Rational.cpp , call them Rational2.h and Rationa

ID: 3591393 • Letter: M

Question

Make copies of Rational.h, and Rational.cpp, call them Rational2.h and Rational2.cpp, respectively. Add the new functionality required by Items 1-to-3D below to the class RATIONAL, then use Problem6-2.cpp to “drive” the new functionality you’ve added.

Problem6.cpp is a simple application program used to “drive” the functionality of RATIONAL.

The code already works just please make the extenstions in steps 1-3D then compile it please with a screenshot.

1. Add a public non-static member function to the class RATIONAL that returns the multiplicative inverseof (*this) without side effect[1].

RATIONAL Inv() const;

The multiplicative inverse of the signed magnitude rational number (*this) is defined S*N/D when N 0 but is undefined when N = 0.

2. Add a public non-static member function to the class RATIONAL that returns the additive inverse of (*this) without side effect.

RATIONAL Neg() const;

The additive inverse of (*this) is defined (-1*S)*N/D.

3a. Add a public non-static member function to the class RATIONAL that returns the quotient (*this) divided-by RHS without side effect.

RATIONAL Div(const RATIONAL RHS) const;

The quotient of (*this) = S1*N1/D1 and RHS = S2*N2/D2, N2 0, is defined

S1*N1/D1 ¸ S2*N2/D2 = (S1*S2)*(N1*D2)/(D1*N2)

but is undefined when N2 = 0.

3b. Add a public non-static member function to the class RATIONAL that returns the quotient LHS divided-by RHS as result (as an OUTy-ness side effect), but without side effects to either LHS or RHS.

void Div(const RATIONAL RHS,RATIONAL &result) const;

3c. Add a public static member function to the class RATIONAL that returns the quotient LHS divided-by RHS without side effects to either LHS or RHS.

static RATIONAL Div2(const RATIONAL LHS,const RATIONAL RHS);

Note: The signature for this version of Div() is similar to the signature used for the static member function Mul(). Unfortunately, both the global-scope non-member function version of Div() (see Item 3d below) and the Item 3b version of Div() (see above) have signatures that are the same as this Item 3c version of Div(). The const-ness and/or the pass-by-reference/pass-by-value-ness of formal parameters are not used by the compiler to disambiguate function overloadings (!), but the 3 Div() Items 3a, 3b, and 3d are allowed to overload the function name Div(). Why? The Item 3a and Item 3b versions have different signatures while the Item 3d version has a scope different from the class-scoped Item 3a and Item 3b versions. So, all that said, the Item 3c version must have a different function name, hence, Div2().

3d. Add a global-scoped non-member function (is there any other kind of scoping for non-member functions?!) that returns the quotient LHS divided-by RHS without side effects to either LHS or RHS.

RATIONAL Div(const RATIONAL LHS,const RATIONAL RHS)

Hint The RATIONAL class does not define accessor/Get() or mutator/Set() functions for the data members S, N, and D, so the Item 3d version of Div() must use some other RATIONAL class public member functions to do division (or be declared as a friend of the RATIONAL class). Recall,

x ¸ y = x ´ (1/y), "x, "y 0. , “A friend function of a class is a non-member function that has the right to access the public and non-public class members. Standalone functions, entire classes, or member functions of other classes may be declared to be friends of another class.”

//-------------------------------------------------------

// Chapter #9, Problem #6

// Rational.h

//-------------------------------------------------------

#ifndef RATIONAL_H

#define RATIONAL_H

#define SHOWLIFETIME

//-------------------------------------------------------

class RATIONAL

//-------------------------------------------------------

{

private:

   int S;   // { +1,-1 }

   int N;   // (N)umerator   N >= 0

   int D;   // (D)enominator D > 0

private:

   void Normalize();

   static int GCD(int x,int y)

   {

      if ( y == 0 )

         return( x );

      else

         return( GCD(y,x%y) );

   }

public:

   static RATIONAL Mul(const RATIONAL LHS,const RATIONAL RHS);

public:

   RATIONAL(int N = 0,int D = 1);

   ~RATIONAL();

   RATIONAL Add(const RATIONAL RHS) const;

   void Sub(const RATIONAL RHS,RATIONAL &result) const;

   void Print() const;

   void Input();

// RATIONAL(const RATIONAL &LHS): S(LHS.S),N(LHS.N),D(LHS.D)

// {

//    cout << "   Copy construction of ", Print(), cout << endl;

// }

};

#endif

//-------------------------------------------------------

// Chapter #9, Problem #6

// Rational.cpp

//-------------------------------------------------------

#include <iostream>

#include <iomanip>

#include <cstdlib>

using namespace std;

#include ".Rational.h"

//-------------------------------------------------------

//private

void RATIONAL::Normalize()

//-------------------------------------------------------

{

   S = +1;

   if ( N < 0 )

   {

      S *= -1;

      N = -N;

   }

   if ( D < 0 )

   {

      S *= -1;

      D = -D;

   }

   int gcd = GCD(N,D);

  

   N /= gcd;

   D /= gcd;

}

//-------------------------------------------------------

RATIONAL::RATIONAL(int N,int D)

//-------------------------------------------------------

{

   this->N = N;

   this->D = D;

   Normalize();

  

#ifdef SHOWLIFETIME

   cout << "   Construction of ", Print(), cout << endl;

#endif

}

//-------------------------------------------------------

RATIONAL::~RATIONAL()

//-------------------------------------------------------

{

#ifdef SHOWLIFETIME

   cout << "   Destruction of ", Print(), cout << endl;

#endif

}

//-------------------------------------------------------

RATIONAL RATIONAL::Add(const RATIONAL RHS) const

//-------------------------------------------------------

{

   RATIONAL r;

   r.N = this->S*this->N*RHS.D + RHS.S*RHS.N*this->D;

   r.D = D*RHS.D;

   r.Normalize();

   return( r );

}

//-------------------------------------------------------

void RATIONAL::Sub(const RATIONAL RHS,RATIONAL &result) const

//-------------------------------------------------------

{

   RATIONAL r = RHS;

   r.S *= -1;

   result = this->Add(r);

}

//-------------------------------------------------------

//static

RATIONAL RATIONAL::Mul(const RATIONAL LHS,const RATIONAL RHS)

//-------------------------------------------------------

{

   RATIONAL r;

   r.N = LHS.S*LHS.N*RHS.S*RHS.N;

   r.D = LHS.D*RHS.D;

   r.Normalize();

   return( r );

}

//-------------------------------------------------------

void RATIONAL::Print() const

//-------------------------------------------------------

{

   if ( N == 0 )

      cout << '0';

   else

   {

      if ( S == -1 ) cout << '-';

      cout << N;

      if ( D != 1 ) cout << '/' << D;

   }

}

//-------------------------------------------------------

void RATIONAL::Input()

//-------------------------------------------------------

{

   cin >> N;

   cin.ignore(11,'/');

   cin >> D;

   Normalize();

}

//-------------------------------------------------------

// Chapter #9, Problem #6

// Problem6.cpp

//-------------------------------------------------------

#include <iostream>

#include <iomanip>

#include <cstdlib>

using namespace std;

#include ".Rational.h"

RATIONAL GLOBAL1(100,1);

//-------------------------------------------------------

int main()

//-------------------------------------------------------

{

   void MyMain();

   static RATIONAL STATIC1(200,1);

   RATIONAL *HEAP1 = new RATIONAL (300,1);

   MyMain();

   RATIONAL AUTO1(400,1);

   delete HEAP1;

   system("PAUSE");

   return( 0 );

}

RATIONAL GLOBAL2(500,1);

//-------------------------------------------------------

void MyMain()

//-------------------------------------------------------

{

   cout << "Beginning MyMain()" << endl;

  

   static RATIONAL STATIC2(600,1);

  

   RATIONAL AUTO2(700,1);

  

   RATIONAL *HEAP2 = new RATIONAL (800,1);

                                                         

   cout << "1. sizeof(RATIONAL) = " << sizeof(RATIONAL) << endl;

{

   cout << "2. Test Input() and Print()" << endl;

   RATIONAL r;

   cout << "r? "; r.Input();

   cout << "r = "; r.Print(); cout << endl;

}

{

   cout << "3. Test Add()" << endl;

   RATIONAL r,LHS(-23,75),RHS(-77,-150);

   r = LHS.Add(RHS);

   cout << "LHS+RHS = "; r.Print(); cout << endl;

   cout << "LHS+RHS = "; LHS.Add(RHS).Print(); cout << endl;

}

{

   cout << "4. Test Sub()" << endl;

   RATIONAL r,LHS(-23,75),RHS(-77,-150);

   LHS.Sub(RHS,r);

   cout << "LHS-RHS = "; r.Print(); cout << endl;

}

{

   cout << "5. Test Mul()" << endl;

  

   RATIONAL rs[] =

   {

      RATIONAL( -1, 2),

// If this RATIONAL constructor reference causes a compile time error, delete

//    the *& operator pair and make the constructor reference "look like" the one above.

      *&RATIONAL( 2, -4),

      *(new RATIONAL( 4, 10))

   };

   RATIONAL *product = new RATIONAL(-1,-1);

   for (int i = 0; i <= sizeof(rs)/sizeof(RATIONAL)-1; i++)

   {

      *product = RATIONAL::Mul(rs[i],*product);

      cout << " i = " << i << ", partial product = "; product->Print(); cout << endl;

   }

   delete product;

}

   delete HEAP2;

   cout << "Ending MyMain()" << endl;

}

//-------------------------------------------------------

// Chapter #9, Problem #6

// Problem6-2.cpp

//-------------------------------------------------------

#include <iostream>

#include <iomanip>

#include <cstdlib>

using namespace std;

#include ".Rational2.h"

//-------------------------------------------------------

int main()

//-------------------------------------------------------

{

{

   cout << "1. Test Inv()" << endl;

   RATIONAL r(-3,2);

   cout << "r       = ", r.Print(), cout << endl;

   cout << "r.Inv() = ", r.Inv().Print(), cout << endl;

}

{

   cout << "2. Test Neg()" << endl;

   RATIONAL r(-3,2);

   cout << "r       = ", r.Print(), cout << endl;

   cout << "r.Neg() = ", r.Neg().Print(), cout << endl;

}

{

//-------------------------------------------------------

// RATIONAL RATIONAL::Div(const RATIONAL RHS) const;

//-------------------------------------------------------

   cout << "3a. Test Div()" << endl;

   RATIONAL LHS(-3,-2),RHS(-4,3);

   cout << "LHS/RHS = "; LHS.Div(RHS).Print(); cout << endl;

}

{

//-------------------------------------------------------

// void RATIONAL::Div(const RATIONAL RHS,RATIONAL &result) const;

//-------------------------------------------------------

   cout << "3b. Test Div()" << endl;

   RATIONAL r,LHS(-3,-2),RHS(-4,3);

   LHS.Div(RHS,r);

   cout << "LHS/RHS = "; r.Print(); cout << endl;

}

{

//-------------------------------------------------------

// static RATIONAL RATIONAL::Div2(const RATIONAL LHS,const RATIONAL RHS);

//-------------------------------------------------------

   cout << "3c. Test Div2()" << endl;

   RATIONAL LHS(-3,2),RHS(4,3);

  

   cout << "LHS/RHS = "; RATIONAL::Div2(LHS,RHS).Print(); cout << endl;

}

{

//-------------------------------------------------------

// global RATIONAL Div(const RATIONAL LHS,const RATIONAL RHS);

//-------------------------------------------------------

   cout << "3d. Test Div()" << endl;

   RATIONAL LHS(-3,2),RHS(4,3);

  

   cout << "LHS/RHS = "; ::Div(LHS,RHS).Print(); cout << endl;

}

   system("PAUSE");

   return( 0 );

}

Explanation / Answer

#ifndef RATIONAL_H

#define RATIONAL_H

#define SHOWLIFETIME

//-------------------------------------------------------

class RATIONAL

//-------------------------------------------------------

{

private:

   int S;   // { +1,-1 }

   int N;   // (N)umerator   N >= 0

   int D;   // (D)enominator D > 0

private:

   void Normalize();

   static int GCD(int x,int y)

   {

      if ( y == 0 )

         return( x );

      else

         return( GCD(y,x%y) );

   }

public:

   static RATIONAL Mul(const RATIONAL LHS,const RATIONAL RHS);

public:

   RATIONAL(int N = 0,int D = 1);

   ~RATIONAL();

   RATIONAL Add(const RATIONAL RHS) const;

   void Sub(const RATIONAL RHS,RATIONAL &result) const;

   void Print() const;

   void Input();

// RATIONAL(const RATIONAL &LHS): S(LHS.S),N(LHS.N),D(LHS.D)

// {

//    cout << "   Copy construction of ", Print(), cout << endl;

// }

};

#endif

//-------------------------------------------------------

// Chapter #9, Problem #6

// Rational.cpp

//-------------------------------------------------------

#include <iostream>

#include <iomanip>

#include <cstdlib>

using namespace std;

#include ".Rational.h"

//-------------------------------------------------------

//private

void RATIONAL::Normalize()

//-------------------------------------------------------

{

   S = +1;

   if ( N < 0 )

   {

      S *= -1;

      N = -N;

   }

   if ( D < 0 )

   {

      S *= -1;

      D = -D;

   }

   int gcd = GCD(N,D);

  

   N /= gcd;

   D /= gcd;

}

//-------------------------------------------------------

RATIONAL::RATIONAL(int N,int D)

//-------------------------------------------------------

{

   this->N = N;

   this->D = D;

   Normalize();

  

#ifdef SHOWLIFETIME

   cout << "   Construction of ", Print(), cout << endl;

#endif

}

//-------------------------------------------------------

RATIONAL::~RATIONAL()

//-------------------------------------------------------

{

#ifdef SHOWLIFETIME

   cout << "   Destruction of ", Print(), cout << endl;

#endif

}

//-------------------------------------------------------

RATIONAL RATIONAL::Add(const RATIONAL RHS) const

//-------------------------------------------------------

{

   RATIONAL r;

   r.N = this->S*this->N*RHS.D + RHS.S*RHS.N*this->D;

   r.D = D*RHS.D;

   r.Normalize();

   return( r );

}

//-------------------------------------------------------

void RATIONAL::Sub(const RATIONAL RHS,RATIONAL &result) const

//-------------------------------------------------------

{

   RATIONAL r = RHS;

   r.S *= -1;

   result = this->Add(r);

}

//-------------------------------------------------------

//static

RATIONAL RATIONAL::Mul(const RATIONAL LHS,const RATIONAL RHS)

//-------------------------------------------------------

{

   RATIONAL r;

   r.N = LHS.S*LHS.N*RHS.S*RHS.N;

   r.D = LHS.D*RHS.D;

   r.Normalize();

   return( r );

}

//-------------------------------------------------------

void RATIONAL::Print() const

//-------------------------------------------------------

{

   if ( N == 0 )

      cout << '0';

   else

   {

      if ( S == -1 ) cout << '-';

      cout << N;

      if ( D != 1 ) cout << '/' << D;

   }

}

//-------------------------------------------------------

void RATIONAL::Input()

//-------------------------------------------------------

{

   cin >> N;

   cin.ignore(11,'/');

   cin >> D;

   Normalize();

}

//-------------------------------------------------------

// Chapter #9, Problem #6

// Problem6.cpp

//-------------------------------------------------------

#include <iostream>

#include <iomanip>

#include <cstdlib>

using namespace std;

#include ".Rational.h"

RATIONAL GLOBAL1(100,1);

//-------------------------------------------------------

int main()

//-------------------------------------------------------

{

   void MyMain();

   static RATIONAL STATIC1(200,1);

   RATIONAL *HEAP1 = new RATIONAL (300,1);

   MyMain();

   RATIONAL AUTO1(400,1);

   delete HEAP1;

   system("PAUSE");

   return( 0 );

}

RATIONAL GLOBAL2(500,1);

//-------------------------------------------------------

void MyMain()

//-------------------------------------------------------

{

   cout << "Beginning MyMain()" << endl;

  

   static RATIONAL STATIC2(600,1);

  

   RATIONAL AUTO2(700,1);

  

   RATIONAL *HEAP2 = new RATIONAL (800,1);

                                                         

   cout << "1. sizeof(RATIONAL) = " << sizeof(RATIONAL) << endl;

{

   cout << "2. Test Input() and Print()" << endl;

   RATIONAL r;

   cout << "r? "; r.Input();

   cout << "r = "; r.Print(); cout << endl;

}

{

   cout << "3. Test Add()" << endl;

   RATIONAL r,LHS(-23,75),RHS(-77,-150);

   r = LHS.Add(RHS);

   cout << "LHS+RHS = "; r.Print(); cout << endl;

   cout << "LHS+RHS = "; LHS.Add(RHS).Print(); cout << endl;

}

{

   cout << "4. Test Sub()" << endl;

   RATIONAL r,LHS(-23,75),RHS(-77,-150);

   LHS.Sub(RHS,r);

   cout << "LHS-RHS = "; r.Print(); cout << endl;

}

{

   cout << "5. Test Mul()" << endl;

  

   RATIONAL rs[] =

   {

      RATIONAL( -1, 2),

// If this RATIONAL constructor reference causes a compile time error, delete

//    the *& operator pair and make the constructor reference "look like" the one above.

      *&RATIONAL( 2, -4),

      *(new RATIONAL( 4, 10))

   };

   RATIONAL *product = new RATIONAL(-1,-1);

   for (int i = 0; i <= sizeof(rs)/sizeof(RATIONAL)-1; i++)

   {

      *product = RATIONAL::Mul(rs[i],*product);

      cout << " i = " << i << ", partial product = "; product->Print(); cout << endl;

   }

   delete product;

}

   delete HEAP2;

   cout << "Ending MyMain()" << endl;

}

//-------------------------------------------------------

// Chapter #9, Problem #6

// Problem6-2.cpp

//-------------------------------------------------------

#include <iostream>

#include <iomanip>

#include <cstdlib>

using namespace std;

#include ".Rational2.h"

//-------------------------------------------------------

int main()

//-------------------------------------------------------

{

{

   cout << "1. Test Inv()" << endl;

   RATIONAL r(-3,2);

   cout << "r       = ", r.Print(), cout << endl;

   cout << "r.Inv() = ", r.Inv().Print(), cout << endl;

}

{

   cout << "2. Test Neg()" << endl;

   RATIONAL r(-3,2);

   cout << "r       = ", r.Print(), cout << endl;

   cout << "r.Neg() = ", r.Neg().Print(), cout << endl;

}

{

//-------------------------------------------------------

// RATIONAL RATIONAL::Div(const RATIONAL RHS) const;

//-------------------------------------------------------

   cout << "3a. Test Div()" << endl;

   RATIONAL LHS(-3,-2),RHS(-4,3);

   cout << "LHS/RHS = "; LHS.Div(RHS).Print(); cout << endl;

}

{

//-------------------------------------------------------

// void RATIONAL::Div(const RATIONAL RHS,RATIONAL &result) const;

//-------------------------------------------------------

   cout << "3b. Test Div()" << endl;

   RATIONAL r,LHS(-3,-2),RHS(-4,3);

   LHS.Div(RHS,r);

   cout << "LHS/RHS = "; r.Print(); cout << endl;

}

{

//-------------------------------------------------------

// static RATIONAL RATIONAL::Div2(const RATIONAL LHS,const RATIONAL RHS);

//-------------------------------------------------------

   cout << "3c. Test Div2()" << endl;

   RATIONAL LHS(-3,2),RHS(4,3);

  

   cout << "LHS/RHS = "; RATIONAL::Div2(LHS,RHS).Print(); cout << endl;

}

{

//-------------------------------------------------------

// global RATIONAL Div(const RATIONAL LHS,const RATIONAL RHS);

//-------------------------------------------------------

   cout << "3d. Test Div()" << endl;

   RATIONAL LHS(-3,2),RHS(4,3);

  

   cout << "LHS/RHS = "; ::Div(LHS,RHS).Print(); cout << endl;

}

   system("PAUSE");

   return( 0 );

}

#ifndef RATIONAL_H

#define RATIONAL_H

#define SHOWLIFETIME

//-------------------------------------------------------

class RATIONAL

//-------------------------------------------------------

{

private:

   int S;   // { +1,-1 }

   int N;   // (N)umerator   N >= 0

   int D;   // (D)enominator D > 0

private:

   void Normalize();

   static int GCD(int x,int y)

   {

      if ( y == 0 )

         return( x );

      else

         return( GCD(y,x%y) );

   }

public:

   static RATIONAL Mul(const RATIONAL LHS,const RATIONAL RHS);

public:

   RATIONAL(int N = 0,int D = 1);

   ~RATIONAL();

   RATIONAL Add(const RATIONAL RHS) const;

   void Sub(const RATIONAL RHS,RATIONAL &result) const;

   void Print() const;

   void Input();

// RATIONAL(const RATIONAL &LHS): S(LHS.S),N(LHS.N),D(LHS.D)

// {

//    cout << "   Copy construction of ", Print(), cout << endl;

// }

};

#endif

//-------------------------------------------------------

// Chapter #9, Problem #6

// Rational.cpp

//-------------------------------------------------------

#include <iostream>

#include <iomanip>

#include <cstdlib>

using namespace std;

#include ".Rational.h"

//-------------------------------------------------------

//private

void RATIONAL::Normalize()

//-------------------------------------------------------

{

   S = +1;

   if ( N < 0 )

   {

      S *= -1;

      N = -N;

   }

   if ( D < 0 )

   {

      S *= -1;

      D = -D;

   }

   int gcd = GCD(N,D);

  

   N /= gcd;

   D /= gcd;

}

//-------------------------------------------------------

RATIONAL::RATIONAL(int N,int D)

//-------------------------------------------------------

{

   this->N = N;

   this->D = D;

   Normalize();

  

#ifdef SHOWLIFETIME

   cout << "   Construction of ", Print(), cout << endl;

#endif

}

//-------------------------------------------------------

RATIONAL::~RATIONAL()

//-------------------------------------------------------

{

#ifdef SHOWLIFETIME

   cout << "   Destruction of ", Print(), cout << endl;

#endif

}

//-------------------------------------------------------

RATIONAL RATIONAL::Add(const RATIONAL RHS) const

//-------------------------------------------------------

{

   RATIONAL r;

   r.N = this->S*this->N*RHS.D + RHS.S*RHS.N*this->D;

   r.D = D*RHS.D;

   r.Normalize();

   return( r );

}

//-------------------------------------------------------

void RATIONAL::Sub(const RATIONAL RHS,RATIONAL &result) const

//-------------------------------------------------------

{

   RATIONAL r = RHS;

   r.S *= -1;

   result = this->Add(r);

}

//-------------------------------------------------------

//static

RATIONAL RATIONAL::Mul(const RATIONAL LHS,const RATIONAL RHS)

//-------------------------------------------------------

{

   RATIONAL r;

   r.N = LHS.S*LHS.N*RHS.S*RHS.N;

   r.D = LHS.D*RHS.D;

   r.Normalize();

   return( r );

}

//-------------------------------------------------------

void RATIONAL::Print() const

//-------------------------------------------------------

{

   if ( N == 0 )

      cout << '0';

   else

   {

      if ( S == -1 ) cout << '-';

      cout << N;

      if ( D != 1 ) cout << '/' << D;

   }

}

//-------------------------------------------------------

void RATIONAL::Input()

//-------------------------------------------------------

{

   cin >> N;

   cin.ignore(11,'/');

   cin >> D;

   Normalize();

}

//-------------------------------------------------------

// Chapter #9, Problem #6

// Problem6.cpp

//-------------------------------------------------------

#include <iostream>

#include <iomanip>

#include <cstdlib>

using namespace std;

#include ".Rational.h"

RATIONAL GLOBAL1(100,1);

//-------------------------------------------------------

int main()

//-------------------------------------------------------

{

   void MyMain();

   static RATIONAL STATIC1(200,1);

   RATIONAL *HEAP1 = new RATIONAL (300,1);

   MyMain();

   RATIONAL AUTO1(400,1);

   delete HEAP1;

   system("PAUSE");

   return( 0 );

}

RATIONAL GLOBAL2(500,1);

//-------------------------------------------------------

void MyMain()

//-------------------------------------------------------

{

   cout << "Beginning MyMain()" << endl;

  

   static RATIONAL STATIC2(600,1);

  

   RATIONAL AUTO2(700,1);

  

   RATIONAL *HEAP2 = new RATIONAL (800,1);

                                                         

   cout << "1. sizeof(RATIONAL) = " << sizeof(RATIONAL) << endl;

{

   cout << "2. Test Input() and Print()" << endl;

   RATIONAL r;

   cout << "r? "; r.Input();

   cout << "r = "; r.Print(); cout << endl;

}

{

   cout << "3. Test Add()" << endl;

   RATIONAL r,LHS(-23,75),RHS(-77,-150);

   r = LHS.Add(RHS);

   cout << "LHS+RHS = "; r.Print(); cout << endl;

   cout << "LHS+RHS = "; LHS.Add(RHS).Print(); cout << endl;

}

{

   cout << "4. Test Sub()" << endl;

   RATIONAL r,LHS(-23,75),RHS(-77,-150);

   LHS.Sub(RHS,r);

   cout << "LHS-RHS = "; r.Print(); cout << endl;

}

{

   cout << "5. Test Mul()" << endl;

  

   RATIONAL rs[] =

   {

      RATIONAL( -1, 2),

// If this RATIONAL constructor reference causes a compile time error, delete

//    the *& operator pair and make the constructor reference "look like" the one above.

      *&RATIONAL( 2, -4),

      *(new RATIONAL( 4, 10))

   };

   RATIONAL *product = new RATIONAL(-1,-1);

   for (int i = 0; i <= sizeof(rs)/sizeof(RATIONAL)-1; i++)

   {

      *product = RATIONAL::Mul(rs[i],*product);

      cout << " i = " << i << ", partial product = "; product->Print(); cout << endl;

   }

   delete product;

}

   delete HEAP2;

   cout << "Ending MyMain()" << endl;

}

//-------------------------------------------------------

// Chapter #9, Problem #6

// Problem6-2.cpp

//-------------------------------------------------------

#include <iostream>

#include <iomanip>

#include <cstdlib>

using namespace std;

#include ".Rational2.h"

//-------------------------------------------------------

int main()

//-------------------------------------------------------

{

{

   cout << "1. Test Inv()" << endl;

   RATIONAL r(-3,2);

   cout << "r       = ", r.Print(), cout << endl;

   cout << "r.Inv() = ", r.Inv().Print(), cout << endl;

}

{

   cout << "2. Test Neg()" << endl;

   RATIONAL r(-3,2);

   cout << "r       = ", r.Print(), cout << endl;

   cout << "r.Neg() = ", r.Neg().Print(), cout << endl;

}

{

//-------------------------------------------------------

// RATIONAL RATIONAL::Div(const RATIONAL RHS) const;

//-------------------------------------------------------

   cout << "3a. Test Div()" << endl;

   RATIONAL LHS(-3,-2),RHS(-4,3);

   cout << "LHS/RHS = "; LHS.Div(RHS).Print(); cout << endl;

}

{

//-------------------------------------------------------

// void RATIONAL::Div(const RATIONAL RHS,RATIONAL &result) const;

//-------------------------------------------------------

   cout << "3b. Test Div()" << endl;

   RATIONAL r,LHS(-3,-2),RHS(-4,3);

   LHS.Div(RHS,r);

   cout << "LHS/RHS = "; r.Print(); cout << endl;

}

{

//-------------------------------------------------------

// static RATIONAL RATIONAL::Div2(const RATIONAL LHS,const RATIONAL RHS);

//-------------------------------------------------------

   cout << "3c. Test Div2()" << endl;

   RATIONAL LHS(-3,2),RHS(4,3);

  

   cout << "LHS/RHS = "; RATIONAL::Div2(LHS,RHS).Print(); cout << endl;

}

{

//-------------------------------------------------------

// global RATIONAL Div(const RATIONAL LHS,const RATIONAL RHS);

//-------------------------------------------------------

   cout << "3d. Test Div()" << endl;

   RATIONAL LHS(-3,2),RHS(4,3);

  

   cout << "LHS/RHS = "; ::Div(LHS,RHS).Print(); cout << endl;

}

   system("PAUSE");

   return( 0 );

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote