#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
*/
virtualvoid read(std::istream& ins);
/**
* Print all polyhedra
*/
virtualvoid display(std::ostream& outs) const;
/**
* Scale all polyhedra
*/
virtualvoid scale(double scalingFactor);
/**
* Swap the contents of two `Composite`s
* <p>
* I am using a friend function here and only here (under protest)
*/
friendvoid swap(Composite& lhs, Composite& rhs);
};
//------------------------------------------------------------------------------
inlineint Composite::size() const
{
returnthis->allPolyhedra.size();
}
//------------------------------------------------------------------------------
inline
Polyhedron* Composite::clone() const
{
returnnew Composite(*this);
}
#endif
And the Polyhedron.h I can provide if needed.
Main thing I am stumped on is void Composite::add(const Polyhedron* toAdd)
once I get that figured out i'm sure it'll help me clear the rest of these functions.
class Composite : public Polyhedron {
public:
using Collection = std::vector<Polyhedron*>;
private:
/**
* Collection of polyhedra of which
* this composite polyhedron is composed
*/
Collection allPolyhedra;
Composite HAS pointers to Polyhedra.
1 2 3 4 5 6
/**
* Add a Polyhedron to the `Composite` collection.
*
* @post toAdd is cloned and the copy is added.
*/
void add(const Polyhedron* toAdd);
The toAdd is a pointer that points to a Polyhedron.
That Polyhedron must be cloned. Pointer to copy is what you store.
1 2 3 4 5 6
void Composite::read(std::istream& ins){
int numPolyhedra;
ins >> numPolyhedra;
allPolyhedra.resize(numPolyhedra);
for (int i = 0; i < numPolyhedra; i++) {
ins >> allPolyhedra[i];
Pigs fly here, unless there is std::istream& operator>> (std::istream&, Polyhedron* &).