Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please answer 30 through 41 for positve rate.All the files are located below that may be needed for the questions. 30. (Continuing 27) Which one

Please answer 30 through 41 for positve rate.All the files are located below that may be needed for the questions.

30. (Continuing 27) Which one of the following RATIONAL objects defined in Problem6.cpp is destructed latest?

A: GLOBAL1 B: STATIC1 C: AUTO1 D: HEAP1 E: GLOBAL2

31. (Continuing 27) T or F? The scope of the RATIONAL object GLOBAL2 includes the body of the function main().

32. (Continuing 27) Fact The scope of the RATIONAL object GLOBAL1 includes the body of the function main(), but T or F? not the function MyMain().

33. The statement shown below (taken from the Problem6.cpp function MyMain()) produces the output line 1. sizeof(RATIONAL) = 12. 12 what?

A: words B: RATIONAL class members C: bytes D: characters E: none of these

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

34. The code segment, LHS.Add(RHS).Print(), in the statement shown below was taken from the Problem6.cpp function MyMain().T or F? The code segment is an example of cascaded member-function calls. Hint Read Section 9.14 Using the this Pointer.

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

35. T or F? rs[], as defined in the MyMain() function in Problem6.cpp, is an array of exactly 3 RATIONAL objects.

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))

};

36. (Continuing 35) T or F? Each one of the RATIONAL objects listed in the array initializer used in the definition of the array rs[] is itself initialized by the RATIONAL default constructor.

37. (Continuing 35) What is the index of the RATIONAL object listed in the array initializer that is constructed first?

A: 0 B: 1 C: 2 D: 3 E: none of these

38. (Continuing 35) What is the index of the RATIONAL object listed in the array initializer that is destructed first?

A: 0 B: 1 C: 2 D: 3 E: none of these

39. (Continuing 35) How many dereferencing operations are used in the definition of the array rs[]?

A: 0 B: 1 C: 2 D: 3 E: more than 3

40. (Continuing 35) How many of the RATIONAL objects stored in the array rs[] live in the MyMain() activation record?

A: 0 B: 1 C: 2 D: 3 E: more than 3

41. (Continuing 35) How many of the RATIONAL objects stored in the array rs[] live in the heap?

A: 0 B: 1 C: 2 D: 3 E: more than 3

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

// Dr. Art Hanna

// 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();

// Added to answer some Quiz Questions

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

// {

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

// }

};

#endif

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

// Dr. Art Hanna

// Chapter #9, Problem #6

// Rational.cpp

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

#include

#include

#include

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();

}

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

// Dr. Art Hanna

// Chapter #9, Problem #6

// Problem6.cpp

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

#include

#include

#include

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;

}

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

// Dr. Art Hanna

// Chapter #9, Problem #6

// Problem6-2.cpp

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

#include

#include

#include

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 );

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Building The Data Warehouse

Authors: W. H. Inmon

4th Edition

0764599445, 978-0764599446

More Books

Students also viewed these Databases questions