+1443 776-2705 panelessays@gmail.com

  Sample Output

Original Polyhedra



------------------------------------------------------



[Sphere] (2, 2, 2)->Radius: 1 Diameter: 2



[Cylinder] (2, 2, 2)->Radius: 1 Height: 2



[Sphere] (8, 8, 8)->Radius: 4 Diameter: 8



[Cylinder] (6, 6, 2)->Radius: 3 Height: 2



[Composite] (14, 14, 14)->3 polyhedra



 [Sphere] (6, 6, 6)->Radius: 3 Diameter: 6



 [Sphere] (10, 10, 10)->Radius: 5 Diameter: 10



 [Sphere] (14, 14, 14)->Radius: 7 Diameter: 14







[Composite] (10, 10, 10)->2 polyhedra



 [Cylinder] (4, 4, 1)->Radius: 2 Height: 1



 [Sphere] (10, 10, 10)->Radius: 5 Diameter: 10







[Sphere] (6, 6, 6)->Radius: 3 Diameter: 6











Scaled Polyhedra (Clones)



------------------------------------------------------



[Sphere] (4, 4, 4)->Radius: 2 Diameter: 4



[Cylinder] (4, 4, 4)->Radius: 2 Height: 4



[Sphere] (16, 16, 16)->Radius: 8 Diameter: 16



[Cylinder] (12, 12, 4)->Radius: 6 Height: 4



[Composite] (28, 28, 28)->3 polyhedra



 [Sphere] (12, 12, 12)->Radius: 6 Diameter: 12



 [Sphere] (20, 20, 20)->Radius: 10 Diameter: 20



 [Sphere] (28, 28, 28)->Radius: 14 Diameter: 28







[Composite] (20, 20, 20)->2 polyhedra



 [Cylinder] (8, 8, 2)->Radius: 4 Height: 2



 [Sphere] (20, 20, 20)->Radius: 10 Diameter: 20







[Sphere] (12, 12, 12)->Radius: 6 Diameter: 12

 your task is 

1. Acquire complete versions of Cylinder.h and Cylinder.cpp from Part 1. You may use my Part 1 solution, or your own solution.

  1. Complete add, display, and scale functions for Composite.
  2. Complete the Big-3 for Composite
    • Note that the Composite::allPolyhedra attribute, is a vector of pointers (std::vector<Polyhedron*>).
    • I was feeling generous and provided you the Composite::operator= method (I used the copy-and-swap idiom). This leaves you with the Copy Constructor and Destructor (2 of the Big-3).

 

If you implemented everything in Composite correctly you will see:

PASSED->testDefaultConstructor

PASSED->testAdd

PASSED->testClone

PASSED->testRead

PASSED->testScale

PASSED->testDisplay

 If you see FAILED you must revisit revisit the corresponding function(s).  

Many students forget to scale the bounding boxes. This can be done using the BoundingBox::scale or Polyhedron::scale functions.

asst5 OOP/bodgeUnitTest.h

#ifndef BODGE_UNIT_TEST_H_INCLUDED
#define BODGE_UNIT_TEST_H_INCLUDED

#include <cstdlib>
#include <functional>
#include <iostream>
#include <string>

#include “bodgeUnitTest.h”

/**
* This is the Bodge-Unit-Testing… PseUdO-Framework
*
* Bodge – A clumsy or inelegant job, usually a temporary repair;
* a patch, a repair. (From Wiktionary)
*/

#define bodgeAssert(expression)
if (!(expression)) {
std::cout << “FAILURE: ”
<< __func__ << “:” << __LINE__
<< ” -> (” << #expression << “)n”;
return false;
}
// End Macro

// Unit Aliases
using UnitTestFunction = std::function<bool()>;
using UnitTestPair = std::pair<UnitTestFunction, std::string>;

/**
* Run a single unit test function and output PASSED of FAILED based on the
* result.
*
* @TODO I could (and should) probably turn this into a macro.
*/
inline
void runTest(const UnitTestFunction& testFunction, std::string description)
{
std::cout << ” ” << (testFunction() ? “PASSED” : “FAILED”)
<< ” -> ” << description
<< std::endl;
}

#endif

asst5 OOP/BoundingBox.cpp

#include “BoundingBox.h”

//——————————————————————————
BoundingBox::BoundingBox()
:lowerLeftVertex (0, 0, 0),
upperRightVertex(0, 0, 0)
{
}

//——————————————————————————
BoundingBox::BoundingBox(Point lowerLeft, Point upperRight)
:lowerLeftVertex (lowerLeft),
upperRightVertex(upperRight)
{
}

//——————————————————————————
Point BoundingBox::getLowerLeftVertex() const
{
return lowerLeftVertex;
}

//——————————————————————————
Point BoundingBox::getUpperRightVertex() const
{
return upperRightVertex;
}

//——————————————————————————
void BoundingBox::setUpperRightVertex(Point u)
{
upperRightVertex = u;
}

//——————————————————————————
void BoundingBox::setUpperRightVertex(double x, double y, double z)
{
upperRightVertex.x = x;
upperRightVertex.y = y;
upperRightVertex.z = z;
}

//——————————————————————————
void BoundingBox::merge(const BoundingBox& other)
{
upperRightVertex.x = std::max(this->upperRightVertex.x,
other.upperRightVertex.x);

upperRightVertex.y = std::max(this->upperRightVertex.y,
other.upperRightVertex.y);

upperRightVertex.z = std::max(this->upperRightVertex.z,
other.upperRightVertex.z);
}

asst5 OOP/BoundingBox.h

#ifndef BOUNDINGBOX_H_INCLUDED
#define BOUNDINGBOX_H_INCLUDED

#include “Point.h”

/**
* Rectangular prism representing the boundaries
* x, y, and z of a polyhedron
*/
class BoundingBox {
private:
/**
* Lower boundary. In this exercise, it is fixed at (0,0,0)
*/
Point lowerLeftVertex;

/**
* Upper boundary
*/
Point upperRightVertex;

public:
/**
* Default Constructor
*/
BoundingBox();

/**
* Construct a bounding box from lower and upper points that define it
*/
BoundingBox(Point lowerLeft, Point upperRight);

// Use the compiler generated version
BoundingBox(const BoundingBox& src) = default;

// Use the compiler generated version
~BoundingBox() = default;

// Use the compiler generated version
BoundingBox& operator=(const BoundingBox& rhs) = default;

/**
* Retrieve the lower boundary
*/
Point getLowerLeftVertex() const;

/**
* Retrieve the upper boundary
*/
Point getUpperRightVertex() const;

/**
* Set the upper boundary using a Point
*/
void setUpperRightVertex(Point u);

/**
* Set the upper boundary using the x, y, and z components.
*/
void setUpperRightVertex(double x, double y, double z);

/**
* Merge two bounding boxes, taking the
* largest values for each of x, y, and z
*/
void merge(const BoundingBox& other);

/**
* Apply a scaling factor
*/
void scale(double s);
};

//——————————————————————————
inline
void BoundingBox::scale(double s)
{
upperRightVertex.scale(s);
}

#endif

asst5 OOP/Composite.cpp

#include “Polyhedron.h”
#include “Composite.h”

//——————————————————————————
Composite::Composite()
:Polyhedron(“Composite”)
{
}

//——————————————————————————
/**
* @todo write this function
*/
Composite::Composite(const Composite& src)
:Polyhedron(“Composite”)
{
// Perform a deep copy… maybe the _add_ method can help…
}

//——————————————————————————
/**
* @todo write this function
*/
Composite::~Composite()
{
// Delete each component polyhedra
}

//——————————————————————————
void Composite::read(std::istream& ins){
int numPolyhedra;

ins >> numPolyhedra;

allPolyhedra.resize(numPolyhedra);

for (int i = 0; i < numPolyhedra; i++) {
ins >> allPolyhedra[i];

boundingBox.merge(allPolyhedra[i]->getBoundingBox());
}
}

//——————————————————————————
/**
* @todo write this function
*/
void Composite::display(std::ostream& outs) const
{
Polyhedron::display(outs);

outs << allPolyhedra.size() << ” polyhedra” << “n”;

// Loop through all component polyhedra and
// print (display) them
}

//——————————————————————————
/**
* @todo write this function
*/
void Composite::scale(double scalingFactor)
{
// Loop through all polyhedra and scale them

// Do not forget the bounding box… after the loop
}

//——————————————————————————
Composite& Composite::operator=(Composite rhs)
{
swap(*this, rhs);
return *this;
}

//——————————————————————————
Composite::iterator Composite::begin()
{
return allPolyhedra.begin();
}

//——————————————————————————
Composite::iterator Composite::end()
{
return allPolyhedra.end();
}

//——————————————————————————
Composite::const_iterator Composite::begin() const
{
return allPolyhedra.begin();
}

//——————————————————————————
Composite::const_iterator Composite::end() const
{
return allPolyhedra.end();
}

//——————————————————————————
/**
* @todo write this function
*/
void Composite::add(const Polyhedron* toAdd)
{
// Add one new polyhedra and _merge_ its boundingBox with _this->boundingBox_
}

//——————————————————————————
void swap(Composite& lhs, Composite& rhs)
{
using std::swap;

std::swap(lhs.allPolyhedra, rhs.allPolyhedra);

swap(lhs.boundingBox, rhs.boundingBox);
}

asst5 OOP/Composite.h

#ifndef COMPOSITE_H_INCLUDED
#define COMPOSITE_H_INCLUDED

#include <vector>

#include “Polyhedron.h”

class Composite : public Polyhedron {
public:
using Collection = std::vector<Polyhedron*>;

using iterator = Collection::iterator;
using const_iterator = Collection::const_iterator;

private:
/**
* Collection of polyhedra of which
* this composite polyhedron is composed
*/
Collection allPolyhedra;

public:

/**
* Default Constructor
*/
Composite();

/**
* Copy Constructor
*/
Composite(const Composite& src);

/**
* Destructor
*/
virtual ~Composite();

/**
* Assignment Operator
*/
Composite& operator=(Composite rhs);

/**
* Return the number of polyhedra that are part of this
* Composite object.
*/
int size() const;

// Iterator helpers
iterator begin();
iterator end();

const_iterator begin() const;
const_iterator end() const;

/**
* Add a Polyhedron to the `Composite` collection.
*
* @post toAdd is cloned and the copy is added.
*/
void add(const Polyhedron* toAdd);

// Polyhedron Interface

/**
* Copy Constructor Wrapper
*/
virtual Polyhedron* clone() const;

/**
* Read all component polyhedra
*
* @pre numPolyhedra == 0
*/
virtual void read(std::istream& ins);

/**
* Print all polyhedra
*/
virtual void display(std::ostream& outs) const;

/**
* Scale all polyhedra
*/
virtual void scale(double scalingFactor);

/**
* Swap the contents of two `Composite`s
* <p>
* I am using a friend function here and only here (under protest)
*/
friend
void swap(Composite& lhs, Composite& rhs);
};

//——————————————————————————
inline
int Composite::size() const
{
return this->allPolyhedra.size();
}

//——————————————————————————
inline
Polyhedron* Composite::clone() const
{
return new Composite(*this);
}

#endif

asst5 OOP/Cylinder.cpp

Polyhedra (OOP) Part 1

You have submitted this assignment 1 time(s).

You last submitted it on Wed Nov 3 18:36:14 2021

The files you submitted were

File Name Bytes
Cylinder.h 2524
Cylinder.cpp 1453

You may not submit this, because: you have already requested and been given access to the solution;
A grade report was posted on Wed Nov 3 18:45:04 2021
Look at the solution to this assignment.
(You will not be permitted to submit again after the solution has been revealed.)
 

asst5 OOP/Cylinder.h

Polyhedra (OOP) Part 1

You have submitted this assignment 1 time(s).

You last submitted it on Wed Nov 3 18:36:14 2021

The files you submitted were

File Name Bytes
Cylinder.h 2524
Cylinder.cpp 1453

You may not submit this, because: you have already requested and been given access to the solution;
A grade report was posted on Wed Nov 3 18:45:04 2021
Look at the solution to this assignment.
(You will not be permitted to submit again after the solution has been revealed.)
 

asst5 OOP/make.dep.txt

createPolyhedra.o: createPolyhedra.cpp Polyhedron.h Point.h BoundingBox.h
PolyhedronFactory.h
Polyhedron.o: Polyhedron.cpp Polyhedron.h Point.h BoundingBox.h
PolyhedronFactory.h
Sphere.o: Sphere.cpp Sphere.h Polyhedron.h Point.h BoundingBox.h
Point.o: Point.cpp Point.h utilities.h
PolyhedronFactory.o: PolyhedronFactory.cpp PolyhedronFactory.h Sphere.h
Polyhedron.h Point.h BoundingBox.h Cylinder.h Composite.h
BoundingBox.o: BoundingBox.cpp BoundingBox.h Point.h
Cylinder.o: Cylinder.cpp Cylinder.h Polyhedron.h Point.h BoundingBox.h
Composite.o: Composite.cpp Polyhedron.h Point.h BoundingBox.h Composite.h
utilities.o: utilities.cpp utilities.h

asst5 OOP/makefile.txt

MAINPROG=createPolyhedra
CPPS= createPolyhedra.cpp Polyhedron.cpp Sphere.cpp Point.cpp
PolyhedronFactory.cpp BoundingBox.cpp Cylinder.cpp Composite.cpp
utilities.cpp
TEST_CPPS= Polyhedron.cpp Sphere.cpp Point.cpp
PolyhedronFactory.cpp BoundingBox.cpp Cylinder.cpp Composite.cpp
utilities.cpp
TEST_DRIVERS=TestCylinder.cpp TestComposite.cpp

DIR=${PWD}
ASST=$(notdir ${DIR})
ifneq (,$(findstring MinGW,$(PATH)))
DISTR=MinGW
EXE=.exe
LFLAGS=
else
DISTR=Unix
EXE=
LFLAGS=-fsanitize=leak,address -fuse-ld=gold
endif
#
########################################################################
# Macro definitions for “standard” C and C++ compilations
#
CPPFLAGS=-g -std=c++11 -D$(DISTR) -Wall -Wextra -Wpedantic -Weffc++
CFLAGS=-g
TARGET=$(MAINPROG)$(EXE)
LINK=g++ $(CPPFLAGS)
#
CC=gcc
CPP=g++
#
#
# In most cases, you should not change anything below this line.
#
# The following is “boilerplate” to set up the standard compilation
# commands:
#

OBJS=$(CPPS:%.cpp=%.o)
DEPENDENCIES = $(CPPS:%.cpp=%.d)

TEST_OBJS=$(TEST_CPPS:%.cpp=%.o)
TEST_DRIVER_OBJS=$(TEST_DRIVERS:%.cpp=%.o)

%.d: %.cpp
touch [email protected]

%.o: %.cpp
$(CPP) $(CPPFLAGS) -MMD -o [email protected] -c $*.cpp

#
# Targets:
#
all: $(TARGET) tests

win: $(OBJS)
$(LINK) $(FLAGS) -o $(TARGET) $(OBJS)

$(TARGET): $(OBJS)
$(LINK) $(FLAGS) -o $(TARGET) $(OBJS) $(LFLAGS)

tests: $(TEST_OBJS) $(TEST_DRIVER_OBJS)
$(LINK) $(FLAGS) -o testCylinder $(TEST_OBJS) TestCylinder.o $(LFLAGS)
$(LINK) $(FLAGS) -o testComposite $(TEST_OBJS) TestComposite.o $(LFLAGS)

clean:
-/bin/rm -f *.d *.o $(TARGET) $(TEST_DRIVER_OBJS) testCylinder.o TestComposite.o

make.dep: $(DEPENDENCIES)
-cat $(DEPENDENCIES) > [email protected]

include make.dep

asst5 OOP/Point.cpp

#include “Point.h”

#include “utilities.h”

//——————————————————————————
Point::Point()
:x(0), y(0), z(0)
{
}

//——————————————————————————
Point::Point(double x_, double y_, double z_)
:x(x_), y(y_), z(z_)
{
}

//——————————————————————————
void Point::scale(double scalingFactor)
{
x *= scalingFactor;
y *= scalingFactor;
z *= scalingFactor;
}

//——————————————————————————
void Point::display(std::ostream& outs) const
{
outs << “(” << x
<< “, ” << y
<< “, ” << z
<< “)”;
}

//——————————————————————————
bool operator==(const Point& lhs, const Point& rhs)
{
if (!fpNumsAreEqual(lhs.x, rhs.x)) {
return false;
}

if (!fpNumsAreEqual(lhs.y, rhs.y)) {
return false;
}

if (!fpNumsAreEqual(lhs.z, rhs.z)) {
return false;
}

return true;
}

asst5 OOP/Point.h

#ifndef POINT_H_INCLUDED
#define POINT_H_INCLUDED

#include <iostream>

/**
* Coordinate in 3 dimensional Cartesian space
*/
struct Point {
double x, y, z;

/**
* Default Constructor
*/
Point();

/**
* Construct a Point from specified
* x, y, and z values
*/
Point(double x, double y, double z);

// Use the compiler generated version
Point(const Point& src) = default;

// Use the compiler generated version
~Point() = default;

// Use the compiler generated version
Point& operator=(const Point& rhs) = default;

/**
* Apply geometric scaling function
*/
void scale(double scalingFactor);

/**
* Print a point
*/
void display(std::ostream& outs) const;

/**
* Swap the contents of two `Point`s
* <p>
* I am using a friend function here and only here (under protest)
*/
friend
void swap(Point& lhs, Point& rhs);
};

/**
* Logical Equivalence Operator – Weak Equality
*/
bool operator==(const Point& lhs, const Point& rhs);

/**
* Stream insertion (output) operator
*/
inline
std::ostream& operator<<(std::ostream& outs, const Point prt)
{
prt.display(outs);
return outs;
}

//——————————————————————————
inline
void swap(Point& lhs, Point& rhs)
{
std::swap(lhs.x, rhs.x);
std::swap(lhs.y, rhs.y);
std::swap(lhs.z, rhs.z);
}

#endif

asst5 OOP/polyhedra1.txt

sphere 1
cylinder 2 1
sphere 4
cylinder 2 3
sphere 3

asst5 OOP/polyhedra2.txt

sphere 1
cylinder 2 1
sphere 4
cylinder 2 3
composite 3
sphere 3
sphere 5
sphere 7

composite 2
cylinder 1 2
sphere 5
sphere 3

asst5 OOP/Polyhedron.cpp

#include “Polyhedron.h”
#include “PolyhedronFactory.h”

//——————————————————————————
Polyhedron::Polyhedron()
:type(“Polyhedron”),
boundingBox()
{
}

//——————————————————————————
Polyhedron::Polyhedron(const char* t)
:type(t),
boundingBox()
{
}

//——————————————————————————
Polyhedron::~Polyhedron()
{
// No dynamic memory
}

//——————————————————————————
void Polyhedron::display(std::ostream& outs) const
{
outs << “[” << type << “] ”
<< boundingBox.getUpperRightVertex()
<< “->”;
}

//——————————————————————————
void Polyhedron::scale(double scalingFactor)
{
boundingBox.scale(scalingFactor);
}

//——————————————————————————
std::istream& operator>>(std::istream& ins, Polyhedron*& ply)
{
std::string polyhedronType;

if (ins >> polyhedronType) {
ply = PolyhedronFactory::createPolyhedron(polyhedronType);

if (ply != nullptr) {
ply->read(ins);
}
else {
getline(ins, polyhedronType);
}
}

return ins;
}

asst5 OOP/Polyhedron.h

#ifndef POLYHEDRON_H_INCLUDED
#define POLYHEDRON_H_INCLUDED

#include <cmath>
#include <cstdlib>
#include <iostream>
#include <string>

#include “Point.h”
#include “BoundingBox.h”

/**
* Abstract Polyhedron Base Class
*/
class Polyhedron {
private:
/**
* A string representing the name of this polyhedron
*/
std::string type;

protected:
/**
* Box (rectangular prism) that contains this element
*/
BoundingBox boundingBox;

public:

/**
* Default Constructor
*/
Polyhedron();

/**
* Constructor which allows
* a name to be set
*
* @param t c-string representing the polyhedron name
*/
Polyhedron(const char* t);

/**
* Destructor
*/
virtual ~Polyhedron();

/**
* Get the polyhedron name
*/
std::string getType() const;

/**
* set the polyhedron name
*/
void setType(std::string t);

/**
* Retrieve the bounding box
*/
BoundingBox getBoundingBox() const;

/**
* Duplicate the polyhedron
*/
virtual Polyhedron* clone() const = 0;

/**
* Retrieve and reconstruct the polyhedron
* from an input stream
*/
virtual void read(std::istream& ins) = 0;

/**
* Print the polyhedron
*/
virtual void display(std::ostream& outs) const;

/**
* Check if two polyhedra have matching types
*/
bool isTypeMatch(const Polyhedron* rhs) const;

/**
* Apply a geometric scaling operation
*/
virtual void scale(double scalingFactor);
};

//——————————————————————————
inline
std::string Polyhedron::getType() const
{
return type;
}

//——————————————————————————
inline
void Polyhedron::setType(std::string t)
{
type = t;
}

//——————————————————————————
inline
BoundingBox Polyhedron::getBoundingBox() const
{
return boundingBox;
}

//——————————————————————————
inline
bool Polyhedron::isTypeMatch(const Polyhedron* rhs) const
{
return this->getType() == rhs->getType();
}

/**
* Stream insertion operator (inline wrapper for display)
*/
inline
std::ostream& operator<<(std::ostream& outs, const Polyhedron& ply) {
ply.display(outs);

return outs;
}

/**
* Stream extraction (input) operator
*/
std::istream& operator>>(std::istream& ins, Polyhedron*& ply);

#endif

asst5 OOP/PolyhedronFactory.cpp

#include “PolyhedronFactory.h”

#include “Sphere.h”
#include “Cylinder.h”
#include “Composite.h”

#include <iterator>
#include <algorithm>

PolyhedronFactory::PolyhedronPair PolyhedronFactory::_known_polyhedra[] = {
{“sphere” , new Sphere()},
{“composite”, new Composite()},
{“cylinder” , new Cylinder()}
};

//——————————————————————————
Polyhedron* PolyhedronFactory::createPolyhedron(std::string name)
{
for(const PolyhedronPair& pair : _known_polyhedra) {
if(pair.first == name) {
return pair.second->clone();
}
}

// A polygon with the given name could not be found
return nullptr;
}

//——————————————————————————
bool PolyhedronFactory::isKnown(std::string name)
{
const auto it = std::find_if(std::begin(_known_polyhedra),
std::end(_known_polyhedra),
[&name](const PolyhedronPair& pairToCheck) {
return pairToCheck.first == name;
});

return it != std::end(_known_polyhedra);
}

//——————————————————————————
void PolyhedronFactory::listKnown(std::ostream& outs)
{
for(const PolyhedronPair& pair : _known_polyhedra) {
outs << ” ” << pair.first << “n”;
}
}

//——————————————————————————
int PolyhedronFactory::numberKnown()
{
return std::end(_known_polyhedra) – std::begin(_known_polyhedra);
}

asst5 OOP/PolyhedronFactory.h

#ifndef POLYHEDRONFACTORY_H_INCLUDED
#define POLYHEDRONFACTORY_H_INCLUDED

#include <iostream>
#include <array>

class Polyhedron;

/**
* The Polyhedron Creating Wizard
*/
class PolyhedronFactory {
private:
/**
* Name Polyhedron Pair 2-tuple( name, model )
* <p>
* Note how this is now a std::pair
*/
using PolyhedronPair = std::pair<std::string, Polyhedron*>;

static PolyhedronPair _known_polyhedra[]; ///< Listing of known polyhedra

public:
/**
* Create a Polyhedron
*
* @param name the polyhedron to be created
*
* @return A polyhedron with the specified name
* or nullptr if no matching polyhedron is found
*/
static Polyhedron* createPolyhedron(std::string name);

/**
* Determine whether a given polyhedron is known
*
* @param name the polyhedron for which to query
*/
static bool isKnown(std::string name);

/**
* Print a list of known Polyhedrons
*
* @param outs the output stream
*/
static void listKnown(std::ostream& outs);

/**
* Determine the number of known Polyhedrons
*
* @return the number of known polyhedrons
*
*/
static int numberKnown();
};

#endif

asst5 OOP/Sphere.cpp

#include “Sphere.h”

//——————————————————————————
Sphere::Sphere()
:Sphere(1)
{
}

//——————————————————————————
Sphere::Sphere(double r)
:Polyhedron(“Sphere”),
radius(r)
{
double d = this->getDiameter();
boundingBox.setUpperRightVertex(d, d, d);
}

//——————————————————————————
void Sphere::read(std::istream& ins)
{
ins >> radius;

double d = this->getDiameter();
boundingBox.setUpperRightVertex(d, d, d);
}

//——————————————————————————
void Sphere::display(std::ostream& outs) const
{
Polyhedron::display(outs);

outs << “Radius: ” << radius
<< ” ”
<< “Diameter: ” << getDiameter();
}

//——————————————————————————
void Sphere::scale(double scalingFactor)
{
radius *= scalingFactor;

boundingBox.scale(scalingFactor);
}

asst5 OOP/Sphere.h

#ifndef SPHERE_H_INCLUDED
#define SPHERE_H_INCLUDED

#include “Polyhedron.h”

/**
* Sphere
*/
class Sphere : public Polyhedron {
private:
double radius;

public:
/**
* Default Constructor
*/
Sphere();

/**
* Construct a sphere from a provided radius
*/
Sphere(double r);

// Use the compiler generated version
Sphere(const Sphere& src) = default;

// Use the compiler generated version
virtual ~Sphere() = default;

// Use the compiler generated version
Sphere& operator=(const Sphere& rhs) = default;

/**
* Retrieve the radius
*/
double getRadius() const;

/**
* Update the radius
*/
void setRadius(double r);

/**
* Compute and return the diameter
*/
double getDiameter() const;

virtual Polyhedron* clone() const;
virtual void read(std::istream& ins);
virtual void display(std::ostream& outs) const;
virtual void scale(double scalingFactor);
};

//——————————————————————————
inline
double Sphere::getRadius() const
{
return this->radius;
}

//——————————————————————————
inline
void Sphere::setRadius(double r)
{
radius = r;

double d = getDiameter();
boundingBox.setUpperRightVertex(d, d, d);
}

//——————————————————————————
inline
double Sphere::getDiameter() const
{
return (2 * radius);
}

//——————————————————————————
inline
Polyhedron* Sphere::clone() const
{
return new Sphere(*this);
}

#endif

asst5 OOP/TestComposite.cpp

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <vector>
#include <sstream>
#include <algorithm>
#include <functional>
#include <cmath>
#include <memory>

#include “Polyhedron.h”
#include “Sphere.h”
#include “Cylinder.h”
#include “Composite.h”

#include “utilities.h”

#include “bodgeUnitTest.h”

//—————————————————————————–
// Unit Tests
//—————————————————————————–
const Point ORIGIN(0, 0, 0);
const Composite EMPTY_POLY;

const std::string INPUT_STR = R”(2
cylinder 1 2
sphere 5)”;

bool testDefaultConstructor()
{
Point lowerPoint = (EMPTY_POLY.getBoundingBox()).getLowerLeftVertex();
Point upperPoint = (EMPTY_POLY.getBoundingBox()).getUpperRightVertex();

bodgeAssert(lowerPoint == ORIGIN);
bodgeAssert(upperPoint == ORIGIN);

bodgeAssert(EMPTY_POLY.size() == 0);

bodgeAssert(EMPTY_POLY.begin() == EMPTY_POLY.end());

// I am skipping display in this test

return true;
}

//—————————————————————————–
bool testAdd()
{
Polyhedron* sphere = new Sphere(2);
Polyhedron* cylinder = new Cylinder(3, 5);

Composite comp1;

comp1.add(sphere);
bodgeAssert(comp1.size() == 1);
bodgeAssert(comp1.begin() != comp1.end());

Point lowerPoint = (comp1.getBoundingBox()).getLowerLeftVertex();
Point upperPoint = (comp1.getBoundingBox()).getUpperRightVertex();

bodgeAssert(lowerPoint == ORIGIN);
bodgeAssert(upperPoint == Point(4, 4, 4));

comp1.add(cylinder);

bodgeAssert(comp1.size() == 2);
bodgeAssert(comp1.begin() != comp1.end());

lowerPoint = (comp1.getBoundingBox()).getLowerLeftVertex();
upperPoint = (comp1.getBoundingBox()).getUpperRightVertex();

bodgeAssert(lowerPoint == ORIGIN);
bodgeAssert(upperPoint == Point(6, 6, 5));

// I am skipping display in this test

delete sphere;
delete …

#include “Polyhedron.h”
#include “Composite.h”
#include <assert.h>

//——————————————————————————
Composite::Composite()
:Polyhedron(“Composite”)
{
}

//——————————————————————————
/**
* @todo write this function
*/
Composite::Composite(const Composite& src)
:Polyhedron(“Composite”)
{
// Perform a deep copy… maybe the _add_ method can help…
for (auto *itr: src.allPolyhedra)
{
allPolyhedra.puch_back(itr->clone());
}

/*for (const Polyhedron* p: src.allPolyhedra){
allPolyhedra.push_back(p->clone());
}*/
}

//——————————————————————————
/**
* @todo write this function
*/
Composite::~Composite()
{

// Delete each component polyhedra
/*for (Polyhedron* p: this->allPolyhedra)
{
delete p;
}*/
for ( auto *itr: this->allPolyhedra)
{
delete itr;
}
}

//——————————————————————————
void Composite::read(std::istream& ins){
int numPolyhedra;

ins >> numPolyhedra;

allPolyhedra.resize(numPolyhedra);

for (int i = 0; i < numPolyhedra; i++) {
ins >> allPolyhedra[i];

boundingBox.merge(allPolyhedra[i]->getBoundingBox());
}//——————————————————————————
/**
* @todo write this function
*/
void Composite::display(std::ostream& outs) const
{
Polyhedron::display(outs);

outs << allPolyhedra.size() << ” polyhedra” << “n”;
for(auto &itr: this->allPolyhedra)
{
outs << *itr << “n”;
/* for ( const Polyhedron* p : this->allPolyhedra)
{
outs << *p <<” n “;
}*/

// Loop through all component polyhedra and
// print (display) them
}

//——————————————————————————
/**
* @todo write this function
*/
void Composite::scale(double scalingFactor)
{
// Loop through all polyhedra and scale them

// Do not forget the bounding box… after the loop
}

//——————————————————————————
Composite& Composite::operator=(Composite rhs)
{
swap(*this, rhs);
return *this;
}
Composite::iterator Composite::begin()
{
return allPolyhedra.begin();
}

//——————————————————————————
Composite::iterator Composite::end()
{
return allPolyhedra.end();
}

//——————————————————————————
Composite::const_iterator Composite::begin() const
{
return allPolyhedra.begin();
}

//——————————————————————————
Composite::const_iterator Composite::end() const
{
return allPolyhedra.end();
}
//——————————————————————————
/**
* @todo write this function
*/
void Composite::add(const Polyhedron* toAdd)
{
// Add one new polyhedra and _merge_ its boundingBox with _this->boundingBox
/* assert(toAdd !=nullptr);
allPolyhedra.push_back(toAdd);
boundingBox.merge(allPolyhedra, getBoundingBox());
Polyhedron.merge(allPolyhedra, getBoundingBox());*/
}

//——————————————————————————
void swap(Composite& lhs, Composite& rhs)
{
using std::swap;

std::swap(lhs.allPolyhedra, rhs.allPolyhedra);

swap(lhs.boundingBox, rhs.boundingBox);
}