Question
21 Quick True of False Questions. Explanations are not necessary just type in T or F. 1. Classes encapsulate (i.e., wrap) attributes and member functions
21 Quick True of False Questions. Explanations are not necessary just type in T or F.
1. Classes encapsulate (i.e., wrap) attributes and member functions into objectsan objects attributes and member functions are intimately related. Objects may communicate with one another, but theyre normally not allowed to know how other objects are implementedimplementation details are hidden within the objects themselves. This information hiding, as well see, is crucial to good software engineering. T or F? The use of the private access specifier shown below (code taken from Rational.h) enforces information hiding.
private:
int S; // { +1,-1 }
int N; // (N)umerator N >= 0
int D; // (D)enominator D > 0
2. (Continuing 1) T or F? The RATIONAL class data members S, N, and D have class scope. Hint Read Section 9.4 Class Scope and Accessing Class Members.
3. T or F? Both functions shown below, Normalize() and GCD(), are examples of utility functions defined inside the RATIONAL class that are for the sole use of other RATIONAL class member functions. Hint Read Section 9.5 Access Functions and Utility Functions.
void Normalize();
static int GCD(int x,int y)
4. (Continuing 3) T or F? The functions Normalize() and GCD() are part of the public interface of the RATIONAL class.
5. (Continuing 3) T or F? The function Normalize() is a private non-static member function of the RATIONAL class.
6. (Continuing 3) T or F? The function GCD() is a private static member function of the RATIONAL class. Hint Read Section 9.15 static Class Members.
7. (Continuing 3) T or F? The function GCD() is defined as a recursive function.
8. T or F? The function Mul() is a public static member function of the RATIONAL class.
static RATIONAL Mul(const RATIONAL LHS,const RATIONAL RHS);
9. The constructor member function of the RATIONAL class prototyped below T or F? is a default constructor. Hint Quoting from Section 9.5 Time Class Case Study: Constructors with Default Arguments, A constructor that defaults all its arguments is also a default constructorthat is, a constructor that can be invoked with no arguments. There can be at most one default constructor per class.
RATIONAL(int N = 0,int D = 1);
10. T or F? The RATIONAL class member function Add() prototyped below is a const public member function. Hint Read Section 9.11 const Objects and const Member Functions.
RATIONAL Add(const RATIONAL RHS) const;
11. Of the 4 RATIONAL class member functions prototyped below, T or F? only the function Add() is not called for the side-effect it provides.
RATIONAL Add(const RATIONAL RHS) const;
void Sub(const RATIONAL RHS,RATIONAL &result) const;
void Print() const;
void Input();
12. T or F? The preprocessor #include statement shown below (taken from Rational.cpp) is absolutely necessary for the correct compilation of Rational.cpp.
#include ".\Rational.h"
13. The code shown below (taken from the RATIONAL class member function Normalize()) T or F? always ensures that S { -1,+1 }, N 0, and D > 0.
S = +1;
if ( N < 0 )
{
S *= -1;
N = -N;
}
if ( D < 0 )
{
S *= -1;
D = -D;
}
14. The code shown below (taken from the RATIONAL class member function Normalize()) T or F? always ensures that GCD(N,D) = 1.
int gcd = GCD(N,D);
N /= gcd;
D /= gcd;
15. T or F? The use of the this pointer shown below is absolutely necessary. Hint Read Section 9.14 Using the this Pointer.
//-------------------------------------------------------
RATIONAL::RATIONAL(int N,int D)
//-------------------------------------------------------
{
this->N = N;
16. T or F? The reference to the RATIONAL class member function Normalize() shown below (taken from the RATIONAL class constructor) can be written legally as this->Normalize().
Normalize();
17. The function header for the RATIONAL class Add() member function is shown below. T or F? The formal parameter RHS is passed-by-value.
//-------------------------------------------------------
RATIONAL RATIONAL::Add(const RATIONAL RHS) const
//-------------------------------------------------------
18. T or F? The initial value of the pass-by-value formal parameter RHS is copied from the corresponding actual parameter using the compiler-supplied default copy constructor. Hint Quoting from Section 9.10 Default Memberwise Assignment, Objects may be passed as function arguments and may be returned from functions. Such passing and returning is performed using pass-by-value by defaulta copy of the object is passed or returned. In such cases, C++ creates a new object and uses a copy constructor to copy the original objects values into the new object. For each class, the compiler provides a default copy constructor that copies each member of the original object into the corresponding member of the new object [memberwise copy].
19. (Continuing 15) In the statement shown below (taken from the RATIONAL class Add() member function), T or F? none of the this pointer references are absolutely necessary.
r.N = this->S*this->N*RHS.D + RHS.S*RHS.N*this->D;
20. The function header for the RATIONAL class Sub() member function is shown below. T or F? The formal parameter result is passed-by-reference.
//-------------------------------------------------------
void RATIONAL::Sub(const RATIONAL RHS,RATIONAL &result) const
//-------------------------------------------------------
21. (Continuing 18 and 20) T or F? The initial value of the pass-by-reference formal parameter result is copied from the corresponding actual parameter using the compiler-supplied default copy constructor.
//-------------------------------------------------------
// 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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started