Question
Eight Quick Questions. NO EXPLAINATION NECESSARY just the answer only. Will rate if fully complete. All the source files are located below. 33. The LINE
Eight Quick Questions. NO EXPLAINATION NECESSARY just the answer only. Will rate if fully complete. All the source files are located below.
33. The LINE class member function Print() is shown below. The scope resolution operator used in the code segment SHAPE::Print() is absolutely necessary. Why? T or F? Without the scope resolution (*this) would implicitly reference the LINE class Print() member function resulting in a self-referencing infinite recursion.
virtual void Print()
{
SHAPE::Print(), cout << ", p1 = ", p1.POINT2D::Print(),
cout << ", p2 = ", p2.POINT2D::Print();
}
34. (Continuing 33) T or F? Both code segments p1.POINT2D::Print() and p2.POINT2D::Print() can be rewritten safely without the scope resolution as in p1.Print() and p2.Print().
35. Which one of the following classes is not an abstract class?
A: OBJECT B: SHAPE C: TWODSHAPE D: THREEDSHAPE E: none of these
36. Fact An abstract class can never be instantiated, but T or F? an abstract class will always have a constructor and a destructor.
37. Which one of the following classes is not a concrete class?
A: POINT2D B: TWODSHAPE C: CIRCLE D: CUBE E: none of these
38. Which one of the following functions is not a member function of the CIRCLE class?
A: Print() B: Type() C: GetID() D: TWODSHAPE::Print() E: none of these
39. T or F? The pointer objects points-to a dynamically-allocated array of pointers.
OBJECT **objects;
objects = new OBJECT * [ n+1 ];
40. T or F? The static types of each pointer stored in the array that objects points-to are exactly the same.
//--------------------------------------------------------
// 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 = 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