Question: Twelve Quick Questions. NO EXPLAINATION NECESSARY just the answer only. Will rate if fully complete. All the source files are located below. 21. T or

Twelve Quick Questions. NO EXPLAINATION NECESSARY just the answer only. Will rate if fully complete. All the source files are located below.

21. T or F? It is legal to add the statement shown below to the Problem.cpp function main().

SHAPE::COLOR color = SHAPE::R;

22. T or F? It is legal to add the statement shown below to the Problem.cpp function main().

SHAPE::COLOR color = SHAPE::COLOR::R;

23. (Continuing 21 and 22) T or F? The 2 definitions of the variable color are functionally-equivalent to each another.

24. The constructor and the destructor of the OBJECT class are not inherited by classes derived from OBJECT, but T or F? the constructor and the destructor of the OBJECT class are accessible to classes in the OBJECT-rooted inheritance hierarchy.

25. (Continuing 15 and 16) T or F? Both SHAPE constructors implicitly reference the OBJECT default constructor.

26. The following statement is found in the body of the SHAPE member function Print(). T or F? Type() is defined as a non-static member function.

cout << setw(10) << Type() << ": ";

27. (Continuing 26) T or F? The naked reference to Type() is functionally-equivalent to

this->Type().

28. (Continuing 26) The function Type() returns a

A: char B: const char * C: const char ** D: STL string E: none of these

29. T or F? The data members defined in the LINE class (shown below) are an example of composition (also known as aggregation or has-a).

protected:

POINT2D p1;

POINT2D p2;

30. T or F? The member initializer syntax shown below, p1(p1), references the POINT2D class constructor.

LINE(COLOR color,POINT2D p1,POINT2D p2): SHAPE(color),p1(p1),p2(p2) {}

31. (Continuing 30) T or F? The by-value parameter passing used for the formal parameter p1 causes the compiler to reference the default POINT2D copy constructor.

32. (Continuing 31) T or F? The default POINT2D copy constructor implements memberwise copying.

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

// Dr. Art Hanna

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

}

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

// Dr. Art Hanna

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

pointer = dynamic_cast(object);

if ( pointer != NULL )

cout << ", area = "

<< setw(7) << setprecision(1) << fixed

<< (static_cast(pointer))->Area();

pointer = dynamic_cast(object);

if ( pointer != NULL )

cout << ", volume = "

<< setw(7) << setprecision(1) << fixed

<< (static_cast(pointer))->Volume();

cout << endl;

delete object;

}

system("PAUSE");

return( 0 );

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!