Object Oriented Programming Help

Hey there all! So i've been working with this code my professor provided us, and
it seems I can't get this to implement correctly.

What i'm trying to do is add the polyhedra to the collection and merge it with the bounding box.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
  #include "Polyhedron.h"
#include "Composite.h"

//------------------------------------------------------------------------------
Composite::Composite()
    :Polyhedron("Composite")
{
    const double d = this->size();
    boundingBox.setUpperRightVertex(d, d, d);
}

//------------------------------------------------------------------------------
/**
 * @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()
{

    for (Polyhedron* s : this->allPolyhedra) {
        delete s;
    // 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
if(Polyhedron)
toAdd->Polyhedron(this);

}

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

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

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


This utilizes composite.h and polyhedron.h header's.
This is the composite header

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#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


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.

1
2
3
4
5
6
7
8
9
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* &).
Topic archived. No new replies allowed.