Question
Ten Quick Questions. NO EXPLAINATION NECESSARY just the answer only. Will rate if fully complete. All the source files are located below. 1. Which Windows
Ten Quick Questions. NO EXPLAINATION NECESSARY just the answer only. Will rate if fully complete. All the source files are located below.
1. Which Windows folder contains the Random.h header file?
A: the folder than contains the Chapter #12 problems source file Problem.cpp
B: the parent folder of the folder than contains Problem.cpp
C: the parent folder of the parent folder of the folder than contains Problem.cpp
D: the parent folder of the parent folder of the parent folder of the folder than contains Problem.cpp
E: none of these
#include "..\..\Random.h"
2. The code shown below is contained in the definition of the OBJECT class. T or F? sequence and ID are inherited by all classes derived directly or indirectly from the base class OBJECT.
private:
static int sequence;
int ID;
3. (Continuing 2) Direct access to sequence and ID is impossible for all classes derived directly or indirectly from the base class OBJECT, but T or F? indirect access to ID is possible using public and protected member functions of the base class OBJECT.
4. (Continuing 3) T or F? If the answer to Question 3 is (T)rue, then the GetID() function shown below is the only way to access to the OBJECT data member ID.
int GetID() { return( ID ); }
5. (Continuing 4) T or F? The GetID() function is inherited by every class derived directly or indirectly from the base class OBJECT.
6. (Continuing 2) Consider the inheritance hierarchy that can be created with the class OBJECT as the root of the hierarchy: T or F? No matter how many objects are instantiated with types taken from the OBJECT-rooted inheritance hierarchy, there will always be only one instance of the data member sequence.
7. T or F? The constructor for the OBJECT class (shown below) is a default constructor.
OBJECT() { ID = sequence++; }
8. (Continuing 7) T or F? The construction of every object in the OBJECT-rooted inheritance hierarchy causes the OBJECT constructor to execute.
9. T or F? The destructor for every class in the OBJECT-rooted inheritance hierarchy is a virtual destructor.
virtual ~OBJECT() {}
10. T or F? The scope resolution operator used in the statement shown below is absolutely necessary.
int OBJECT::sequence = 1;
//----------------------------------------------------- // Header file for random-related functions // Random.h //----------------------------------------------------- #ifndef RANDOM_H #define RANDOM_H #ifndef __cplusplus #include#endif void SetRandomSeed(void); double RandomReal(void); int RandomInteger(int LB,int UB); bool RandomBoolean(double bias); char RandomCharacter(char characters[],int size); #endif
//----------------------------------------------------- // Function definition file for random-related functions // Random.cpp //----------------------------------------------------- #include#include //----------------------------------------------------- void SetRandomSeed(void) //----------------------------------------------------- { srand( (unsigned int) time(NULL) ); } //-------------------------------------------------- double RandomReal() //-------------------------------------------------- { int R; do R = rand(); while ( R == RAND_MAX ); return( (double) R/RAND_MAX ); } //----------------------------------------------------- int RandomInteger(int LB,int UB) //----------------------------------------------------- { double RandomReal(void); return( (int) ((UB-LB+1)*RandomReal()) + LB ); } //----------------------------------------------------- bool RandomBoolean(double bias) //----------------------------------------------------- { int RandomInteger(int LB,int UB); // Return true (0.0 <= bias <= 1.0)*100 percent of the time return( ((double) RandomInteger(1,1000000)/1000000) <= bias ); } //----------------------------------------------------- char RandomCharacter(char characters[],int size) //----------------------------------------------------- { int RandomInteger(int LB,int UB); return( characters[RandomInteger(0,(size-1))] ); }
//--------------------------------------------------------
// Chapter #12 Problem
// Problem.cpp
//--------------------------------------------------------
#include
#include
using namespace std;
#include "..\..\Random.h"
#define POINT2DTYPE 1
#define LINETYPE 2
#define CIRCLETYPE 3
#define SQUARETYPE 4
#define RECTANGLETYPE 5
#define CUBETYPE 6
#define BOXTYPE 7
//--------------------------------------------------------
class OBJECT
//--------------------------------------------------------
{
private:
static int sequence;
int ID;
public:
OBJECT() { ID = sequence++; }
virtual ~OBJECT() {}
int GetID() { return( ID ); }
virtual void Print() = 0;
};
int OBJECT::sequence = 1;
//--------------------------------------------------------
class POINT2D: public OBJECT
//--------------------------------------------------------
{
protected:
double x;
double y;
public:
POINT2D(double x,double y): x(x),y(y) {}
const char *Type() { return( "POINT2D" ); }
POINT2D &operator=(POINT2D &RHS)
{
x=RHS.x, y=RHS.y, cout << "= ";
return(*this);
}
POINT2D(const POINT2D &RHS): x(RHS.x),y(RHS.y) {}
virtual void Print()
{
cout << setw(10) << Type() << ": ";
cout << "ID = " << setw(3) << GetID();
cout << ", (x,y) = (" << setw(5) << setprecision(1) << fixed << x;
cout << "," << setw(5) << setprecision(1) << fixed << y << ")";
}
};
//--------------------------------------------------------
class SHAPE: public OBJECT
//--------------------------------------------------------
{
public:
enum COLOR { Q=0,R=1,B=2,G=3 };
protected:
COLOR color;
public:
SHAPE(): color(Q) {}
SHAPE(COLOR color): color(color) {}
virtual const char *Type() = 0;
virtual void Print()
{
cout << setw(10) << Type() << ": ";
cout << "ID = " << setw(3) << GetID();
cout << ", color = " << ((color == R) ? 'R' : ((color == B) ? 'B' : ((color == G) ? 'G' : 'Q')));
}
};
//--------------------------------------------------------
class LINE: public SHAPE
//--------------------------------------------------------
{
protected:
POINT2D p1;
POINT2D p2;
public:
LINE(COLOR color,POINT2D p1,POINT2D p2): SHAPE(color),p1(p1),p2(p2) {}
virtual const char *Type() { return( "LINE" ); }
virtual void Print()
{
SHAPE::Print(), cout << ", p1 = ", p1.POINT2D::Print(),
cout << ", p2 = ", p2.POINT2D::Print();
}
};
//--------------------------------------------------------
class TWODSHAPE: public SHAPE
//--------------------------------------------------------
{
protected:
const int dimension;
public:
TWODSHAPE(COLOR color): SHAPE(color),dimension(2) {}
virtual void Print()
{
SHAPE::Print(), cout << ", dimension = " << dimension;
}
};
//--------------------------------------------------------
class CIRCLE: public TWODSHAPE
//--------------------------------------------------------
{
protected:
int radius;
public:
CIRCLE(COLOR color,int radius): TWODSHAPE(color),radius(radius) {}
virtual const char *Type() { return( " CIRCLE"); }
virtual void Print()
{
TWODSHAPE::Print(), cout << ", radius = " << radius;
}
};
//--------------------------------------------------------
class SQUARE: public TWODSHAPE
//--------------------------------------------------------
{
protected:
int side;
public:
SQUARE(COLOR color,int side): TWODSHAPE(color),side(side) {}
virtual const char *Type() { return( "SQUARE" ); }
virtual void Print()
{
TWODSHAPE::Print(), cout << ", side = " << side;
}
};
//--------------------------------------------------------
class RECTANGLE: public TWODSHAPE
//--------------------------------------------------------
{
protected:
int height;
int width;
public:
RECTANGLE(COLOR color,int height,int width): TWODSHAPE(color),height(height),width(width) {}
virtual const char *Type() { return( "RECTANGLE" ); }
virtual void Print()
{
TWODSHAPE::Print(), cout << ", height = " << height << ", width = " << width;
}
};
//--------------------------------------------------------
class THREEDSHAPE: public SHAPE
//--------------------------------------------------------
{
protected:
const int dimension;
public:
THREEDSHAPE(COLOR color): SHAPE(color),dimension(3) {}
virtual void Print()
{
SHAPE::Print(), cout << ", dimension = " << dimension;
}
};
//--------------------------------------------------------
class CUBE: public THREEDSHAPE
//--------------------------------------------------------
{
protected:
int side;
public:
CUBE(COLOR color,int side): THREEDSHAPE(color),side(side) {}
virtual const char *Type() { return( "CUBE" ); }
virtual void Print()
{
THREEDSHAPE::Print(), cout << ", side = " << side;
}
};
//--------------------------------------------------------
class BOX: public THREEDSHAPE
//--------------------------------------------------------
{
protected:
int height;
int width;
int depth;
public:
BOX(COLOR color,int height,int width,int depth):
THREEDSHAPE(color),height(height),width(width),depth(depth) {}
virtual const char *Type() { return( "BOX" ); }
virtual void Print()
{
THREEDSHAPE::Print(), cout << ", height = " << height
<< ", width = " << width
<< ", depth = " << depth;
}
};
//--------------------------------------------------------
int main()
//--------------------------------------------------------
{
OBJECT **objects;
int n;
// COLOR color = R; // Added for Question 19
// COLOR color = COLOR::R; // Added for Question 20
// SHAPE::COLOR color = SHAPE::R; // Added for Question 21
// SHAPE::COLOR color = SHAPE::COLOR::R; // Added for Question 22
SetRandomSeed();
cout << "n? "; cin >> n;
objects = new OBJECT * [ n+1 ];
for (int i = 1; i <= n; i++)
{
switch ( RandomInteger(POINT2DTYPE,BOXTYPE) )
{
case POINT2DTYPE:
objects[i] = new POINT2D(RandomReal()*100.0,
RandomReal()*100.0);
break;
case LINETYPE:
objects[i] = new LINE((SHAPE::COLOR) RandomInteger((int) SHAPE::R,SHAPE::G),
POINT2D(RandomReal()*100.0,
RandomReal()*100.0),
POINT2D(RandomReal()*100.0,
RandomReal()*100.0));
break;
case CIRCLETYPE:
objects[i] = new CIRCLE((SHAPE::COLOR) RandomInteger((int) SHAPE::R,SHAPE::G),
RandomInteger(1,10));
break;
case SQUARETYPE:
objects[i] = new SQUARE((SHAPE::COLOR) RandomInteger((int) SHAPE::R,SHAPE::G),
RandomInteger(1,10));
break;
case RECTANGLETYPE:
objects[i] = new RECTANGLE((SHAPE::COLOR) RandomInteger((int) SHAPE::R,SHAPE::G),
RandomInteger(1,10),
RandomInteger(1,10));
break;
case CUBETYPE:
objects[i] = new CUBE((SHAPE::COLOR) RandomInteger((int) SHAPE::R,SHAPE::G),
RandomInteger(1,10));
break;
case BOXTYPE:
objects[i] = new BOX((SHAPE::COLOR) RandomInteger((int) SHAPE::R,SHAPE::G),
RandomInteger(1,10),
RandomInteger(1,10),
RandomInteger(1,10));
break;
}
}
for (int i = 1; i <= n; i++)
{
objects[i]->Print();
cout << endl;
}
for (int i = 1; i <= n; i++)
delete objects[i];
delete [] objects;
system("PAUSE");
return( 0 );
}
//--------------------------------------------------------
// Chapter #12 Problem-2
// Problem-2.cpp
//--------------------------------------------------------
#include
#include
#include
using namespace std;
#include "..\..\Random.h"
#define POINT2DTYPE 1
#define LINETYPE 2
#define CIRCLETYPE 3
#define SQUARETYPE 4
#define RECTANGLETYPE 5
#define CUBETYPE 6
#define BOXTYPE 7
#define POINT3DTYPE 8
#define SPHERETYPE 9
...Same code as found in Problem.cpp with added Area() and Volume() functions...
//--------------------------------------------------------
class POINT3D: public OBJECT
//--------------------------------------------------------
{
Student adds missing code
};
//--------------------------------------------------------
class SPHERE: public THREEDSHAPE
//--------------------------------------------------------
{
Student adds missing code
};
//--------------------------------------------------------
int main()
//--------------------------------------------------------
{
int n;
SetRandomSeed();
cout << "n? "; cin >> n;
for (int i = 1; i <= n; i++)
{
OBJECT *object;
switch ( RandomInteger(POINT2DTYPE,SPHERETYPE) )
{
case POINT2DTYPE:
object = new POINT2D(RandomReal()*100.0,
RandomReal()*100.0);
break;
case LINETYPE:
object = new LINE((SHAPE::COLOR) RandomInteger((int) SHAPE::R,SHAPE::G),
POINT2D(RandomReal()*100.0,
RandomReal()*100.0),
POINT2D(RandomReal()*100.0,
RandomReal()*100.0));
break;
case CIRCLETYPE:
object = new CIRCLE((SHAPE::COLOR) RandomInteger((int) SHAPE::R,SHAPE::G),
RandomInteger(1,10));
break;
case SQUARETYPE:
object = new SQUARE((SHAPE::COLOR) RandomInteger((int) SHAPE::R,SHAPE::G),
RandomInteger(1,10));
break;
case RECTANGLETYPE:
object = new RECTANGLE((SHAPE::COLOR) RandomInteger((int) SHAPE::R,SHAPE::G),
RandomInteger(1,10),
RandomInteger(1,10));
break;
case CUBETYPE:
object = new CUBE((SHAPE::COLOR) RandomInteger((int) SHAPE::R,SHAPE::G),
RandomInteger(1,10));
break;
case BOXTYPE:
object = new BOX((SHAPE::COLOR) RandomInteger((int) SHAPE::R,SHAPE::G),
RandomInteger(1,10),
RandomInteger(1,10),
RandomInteger(1,10));
break;
case POINT3DTYPE:
object = new POINT3D(RandomReal()*100.0,
RandomReal()*100.0,
RandomReal()*100.0);
break;
case SPHERETYPE:
object = new SPHERE((SHAPE::COLOR) RandomInteger((int) SHAPE::R,SHAPE::G),
RandomInteger(1,10),
POINT3D(RandomReal()*100.0,
RandomReal()*100.0,
RandomReal()*100.0));
break;
}
void *pointer = object;
(static_cast
pointer = dynamic_cast
if ( pointer != NULL )
cout << ", area = "
<< setw(7) << setprecision(1) << fixed
<< (static_cast
pointer = dynamic_cast
if ( pointer != NULL )
cout << ", volume = "
<< setw(7) << setprecision(1) << fixed
<< (static_cast
cout << endl;
delete object;
}
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