(Shape Hierarchy) Implement the Shape hierarchy designed in Exercise 12.7 (which is based on the hierarchy in Fig. 12.3). Each TwoDimensionalShape should contain function getArea to calculate the area of the two-dimensional shape. Each ThreeDimensionalShape should have mem- ber functions getArea and getVolume to calculate the surface area and volume, respectively, of the three-dimensional shape. Create a program that uses a vector of Shape pointers to objects of each concrete class in the hierarchy.

The program should print the object to which each vector element points. Also, in the loop that processes all the shapes in the vector, determine whether each shape is a TwoDimensionalShape or a ThreeDimensionalShape. If a shape is a TwoDimensionalShape, dis- play its area. If a shape is a ThreeDimensionalShape, display its area and volume.


```
// Definition of base-class Shape
#ifndef SHAPE_H
#define SHAPE_H

#include
using namespace std;

class Shape {
friend ostream & operator<<( ostream &, Shape & );
public:
Shape( double = 0.0, double = 0.0 ); // default constructor
double getCenterX() const; // return x from coordinate pair
double getCenterY() const; // return y from coordinate pair
virtual void print() const = 0; // output Shape object
protected:
double xCenter; // x part of coordinate pair
double yCenter; // y part of coordinate pair
}; // end class Shape

#endif
```
```
// Member and friend definitions for class Shape

#include "Shape.h"

// default constructor
Shape::Shape( double x, double y )
{
xCenter = x;
yCenter = y;
} // end Shape constructor

// return x from coordinate pair
double Shape::getCenterX() const
{
return xCenter;
} // end function getCenterX

// return y from coordinate pair
double Shape::getCenterY() const
{
return yCenter;
} // end function getCenterY

// overloaded output operator
ostream & operator<<( ostream &out, Shape &s )
{
s.print();
return out;
} // end overloaded output operator function
```
```
// Definition of class TwoDimensionalShape
#ifndef TWODIM_H
#define TWODIM_H

#include "Shape.h"

class TwoDimensionalShape : public Shape
{
public:
// default constructor
TwoDimensionalShape( double x, double y ) : Shape( x, y ) { }

virtual double getArea() const = 0; // area of TwoDimensionalShape
}; // end class TwoDimensionalShape

#endif
```
```
// Definition of class ThreeDimensionalShape
#ifndef THREEDIM_H
#define THREEDIM_H

#include "Shape.h"

class ThreeDimensionalShape : public Shape
{
public:
// default constructor
ThreeDimensionalShape( double x, double y ) : Shape( x, y ) { }

virtual double getArea() const = 0; // area of 3-dimensional shape
virtual double getVolume() const = 0; // volume of 3-dimensional shape
}; // end class ThreeDimensionalShape

#endif
```
```
// Definition of class Circle
#ifndef CIRCLE_H
#define CIRCLE_H

#include "TwoDimensionalShape.h"

class Circle : public TwoDimensionalShape
{
public:
// default consturctor
Circle( double = 0.0, double = 0.0, double = 0.0 );

virtual double getRadius() const; // return radius
virtual double getArea() const; // return area
void print() const; // output Circle object
private:
double radius; // Circle's radius
}; // end class Circle

#endif
```
```
// Member-function definitions for class Circle
#include
#include "Circle.h"
using namespace std;

// default constructor
Circle::Circle( double r, double x, double y )
: TwoDimensionalShape( x, y )
{
radius = ( ( r > 0.0 ) ? r : 0.0 );
} // end Circle constructor

// return radius of circle object
double Circle::getRadius() const
{
return radius;
} // end function getRadius

// return area of circle object
double Circle::getArea() const
{
return 3.14159 * radius * radius;
} // end function getArea

// output circle object
void Circle::print() const
{
cout << "Circle with radius " << radius << "; center at ("
<< xCenter << ", " << yCenter << ")";
} // end function print
```
```
// Definition of class Square
#ifndef SQUARE_H
#define SQUARE_H

#include "TwoDimensionalShape.h"

class Square : public TwoDimensionalShape
{
public:
// default constructor
Square( double = 0.0, double = 0.0, double = 0.0 );

virtual double getSideLength() const; // return length of sides
virtual double getArea() const; // return area of Square
void print() const; // output Square object
private:
double sideLength; // length of sides
}; // end class Square

#endif
```
```
// Member-function definitions for class Square
#include
#include "Square.h"
using namespace std;

// default constructor
Square::Square( double s, double x, double y )
: TwoDimensionalShape( x, y )
{
sideLength = ( ( s > 0.0 ) ? s : 0.0 );
} // end Square constructor

// return side of Square
double Square::getSideLength() const
{
return sideLength;
} // end function getSideLength

// return area of Square
double Square::getArea() const
{
return sideLength * sideLength;
} // end function getArea

// output Square object
void Square::print() const
{
cout << "Square with side length " << sideLength << "; center at ("
<< xCenter << ", " << yCenter << ")";
} // end function print
```
```
// Definition of class Sphere
#ifndef SPHERE_H
#define SPHERE_H

#include "ThreeDimensionalShape.h"

class Sphere : public ThreeDimensionalShape
{
public:
// default constructor
Sphere( double = 0.0, double = 0.0, double = 0.0 );

virtual double getArea() const; // return area of Sphere
virtual double getVolume() const; // return volume of Sphere
double getRadius() const; // return radius of Sphere
void print() const; // output Sphere object
private:
double radius; // radius of Sphere
}; // end class Sphere

#endif
```
```
// Member-function definitions for class Sphere
#include
#include "Sphere.h"
using namespace std;

// default constructor
Sphere::Sphere( double r, double x, double y )
: ThreeDimensionalShape( x, y )
{
radius = ( ( r > 0.0 ) ? r : 0.0 );
} // end Sphere constructor

// return area of Sphere
double Sphere::getArea() const
{
return 4.0 * 3.14159 * radius * radius;
} // end function getArea

// return volume of Sphere
double Sphere::getVolume() const
{
return ( 4.0 / 3.0 ) * 3.14159 * radius * radius * radius;
} // end function getVolume

// return radius of Sphere
double Sphere::getRadius() const
{
return radius;
} // end function getRadius

// output Sphere object
void Sphere::print() const
{
cout << "Sphere with radius " << radius << "; center at ("
<< xCenter << ", " << yCenter << ")";
} // end function print
```
```
// Definition of class Cube
#ifndef CUBE_H
#define CUBE_H

#include "ThreeDimensionalShape.h"

class Cube : public ThreeDimensionalShape
{
public:
// default constructor
Cube( double = 0.0, double = 0.0, double = 0.0 );

virtual double getArea() const; // return area of Cube object
virtual double getVolume() const; // return volume of Cube object
double getSideLength() const; // return length of sides
void print() const; // output Cube object
private:
double sideLength; // length of sides of Cube
}; // end class Cube

#endif
```
```
// Member-function definitions for class Cube
#include
#include "Cube.h"
using namespace std;

// default constructor
Cube::Cube( double s, double x, double y )
: ThreeDimensionalShape( x, y )
{
sideLength = ( ( s > 0.0 ) ? s : 0.0 );
} // end Cube constructor

// return area of Cube
double Cube::getArea() const
{
return 6 * sideLength * sideLength;
} // end function getArea

// return volume of Cube
double Cube::getVolume() const
{
return sideLength * sideLength * sideLength;
} // end function getVolume

// return length of sides
double Cube::getSideLength() const
{
return sideLength;
} // end function getSideLength

// output Cube object
void Cube::print() const
{
cout << "Cube with side length " << sideLength << "; center at ("
<< xCenter << ", " << yCenter << ")";
} // end function print
```
```
// Driver to test Shape hierarchy
#include
#include
#include "Shape.h"
#include "TwoDimensionalShape.h"
#include "ThreeDimensionalShape.h"
#include "Circle.h"
#include "Square.h"
#include "Sphere.h"
#include "Cube.h"
using namespace std;

int main()
{
// create vector shapes
vector < Shape * > shapes( 4 );

// initialize vector with Shapes
shapes[ 0 ] = new Circle( 3.5, 6, 9 );
shapes[ 1 ] = new Square( 12, 2, 2 );
shapes[ 2 ] = new Sphere( 5, 1.5, 4.5 );
shapes[ 3 ] = new Cube( 2.2 );

// output Shape objects and display area and volume as appropriate
for ( int i = 0; i < 4; i++ )
{
cout << *shapes[ i ] << endl;

// downcast pointer
TwoDimensionalShape *twoDimensionalShapePtr =
dynamic_cast < TwoDimensionalShape * > ( shapes[ i ] );

// if Shape is a TwoDimensionalShape, display its area
if ( twoDimensionalShapePtr != 0 )
cout << "Area: " << twoDimensionalShapePtr->getArea() << endl;

// downcast pointer
ThreeDimensionalShape *threeDimensionalShapePtr =
dynamic_cast < ThreeDimensionalShape * > ( shapes[ i ] );

// if Shape is a ThreeDimensionalShape, display its area and volume
if ( threeDimensionalShapePtr != 0 )
cout << "Area: " << threeDimensionalShapePtr->getArea()
<< "\nVolume: " << threeDimensionalShapePtr->getVolume()
<< endl;

cout << endl;
} // end for
} // end main
```
Circle with radius 3.5; center at (6, 9)
Area: 38.4845
Square with side length 12; center at (2, 2)
Area: 144
Sphere with radius 5; center at (1.5, 4.5)
Area: 314.159
Volume: 523.598
Cube with side length 2.2; center at (0, 0)
Area: 29.04
Volume: 10.648

Computer Science & Information Technology

You might also like to view...

Data communications is a special case of telecommunications, where only ones and zeroes are exchanged.

a. True b. False

Computer Science & Information Technology

What is Subscript?

What will be an ideal response?

Computer Science & Information Technology

________ is an image property and can be read by a screen reader

Fill in the blank(s) with correct word

Computer Science & Information Technology

The third line of a header in multiple page letters and memos contain the  ____________________ .

Fill in the blank(s) with the appropriate word(s).

Computer Science & Information Technology