How can I use construct object (Shape) efficiently?

I am a c++ novice and have a problem with my code (I wish for answer with code).
The conditions I have to grant:

- **Only** a pointer to an object must be saved in a **standard** class vector (e.g vector<ShapePtr>)
- Base class must be a polymorphic class (I call this class ShapePtr)
- I **must** have a deep copy process
- Constructor is **not** suppose to do a deep copy
- **clone()** is suppose to do the deep copy

Here is the code **provided** to me:

main.cpp

1
2
3
4
5
6
7
8
9
    int main() {
      typedef shared_ptr<Shape> ShapePtr;
      vector<ShapePtr> shapevec;
      Vertex varr[] = { Vertex(0,0), Vertex(10,0), Vertex(5,2), Vertex(5,5) };
      shapevec.push_back( ShapePtr(new Polygon(1, 4, varr, 4)) );
      // shapevec.push_back( ShapePtr(new Circle(5, 5, 4)) );
      // shapevec.push_back( ShapePtr(new Rectangle(4, 10, 2, 4)) );
      // shapevec.push_back( ShapePtr(new Point(6,7,1)) );
     }


I have written a code for a deep copy constructor (that's the code that lies in the polygon constructor right now) but because of my assignment says that deep copying should **NOT** be in the polygon's constructor and that the clone() must do the deep copy, but the clone() **MUST** be included in my assignment: this is where my problem arises. Thus, I have not been able to call or activated the clone().

This is code I've written by **myself**:

Shape.h

1
2
3
4
5
6
7
8
9
10
11
    
     class Shape {
      protected:
          Shape *ShPek;
    
      public:
        Shape();
        Shape(const Shape &ptr);
        virtual ~Shape();
        virtual Shape* clone() const = 0;
    }

Shape.cpp

1
2
3
4
5
    Shape::Shape(): ShPek(0){}
    
    Shape::Shape(const Shape &ptr){
        ShPek = ptr.clone();
    }

Polygon.h

1
2
3
4
5
6
7
8
9
10
11
12
    class Polygon : public Shape {
      private:
        Vertex* fp;
        int num;
    
      public:
        Polygon();
        Polygon( double x, double y, Vertex* varr, int num  );
        Polygon( const Polygon &p );
        ~Polygon();
        Shape* clone() const;
    };


Polygon.cpp

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
    Polygon::Polygon(): pX(0), pY(0), Pol(0), storlek(0){
        this->ShPek = 0;
    }
    
    Polygon::Polygon(int x, int y, Vertex* varr, int num) : pX(x), pY(y), storlek(num) {
             Pol = new Vertex[storlek];
             for (int ix=0; ix<storlek; ix++){
                 Pol[ix] = varr[ix];
             }
    }
    
    Polygon::Polygon(const Polygon &Poly): pX(Poly.pX), pY(Poly.pY), storlek(Poly.storlek){
        if (Poly.storlek > 0) {
             Pol = new Vertex[Poly.storlek];
             for (int ix=0; ix<Poly.storlek; ix++){
                 Pol[ix] = Poly.Pol[ix];
             }
        } else Pol = 0;
    }
    
    Polygon::~Polygon() {
            delete[] Pol;
        }
    
    Shape* Polygon::clone() const {
          return new Polygon(*this);
    }


Vertex.h

1
2
3
4
5
6
7
8
9
10
11
12
    class Vertex {
    
      protected:
        int x, y;
    
      public:
        Vertex() : x(0), y(0) {}
        Vertex(int _X, int _Y) : x(_X), y(_Y) {}
        ~Vertex() {}
        int Xv() { return x; }
        int Yv() { return y; }
    };

Thank you in advance.
I don't see any problems.
Topic archived. No new replies allowed.