1

You need to decide an appropriate internal representation of the Polygon before you can start, e.g. do you want to store the lines of the polygon, or just the points, or both? Should you use array, vector, linked list or something else? etc. Declare the appropriate information in the private section of the class.

I'm not sure should I use a vector or linked list?

Tasked to implement the following functions.

Polygon(const Point & p1, const Point & p2, const Point & p3)

virtual void print( ostream & out )

virtual double perimeter()

virtual bool add(Point & p)

virtual bool add(Line & exist, Point & p)

Line and Point classes have already been implemented.

  • 2
    Does this answer your question? [When to use a linked list over an array/array list?](https://stackoverflow.com/questions/393556/when-to-use-a-linked-list-over-an-array-array-list) – Brian61354270 Apr 05 '21 at 04:01
  • What operations does `Polygon` require, and what are the asymptotic runtimes of the respective list types on those operations? – Silvio Mayolo Apr 05 '21 at 04:02
  • ```Line```, ```Point``` and ```Polygon``` are classes. The implementation of the vector / linked list needs to be in the private section. The above mentioned functions require ```Polygon``` – strangledstranger Apr 05 '21 at 04:56
  • Try thinking about expected usage patterns of the `Polygon` class, and performance requirements associated with those usage patterns. Do you expect to be adding `Point`s to a polygon (or removing points from) or calculating its perimeter repeatedly? Can you (or code using your `Polygon` class) estimate an upper bound on the number of points? As a rough rule, you will find that picking different containers comes with trade-offs - some operations will be constant-time with some containers but more complex with others (and no container can do EVERYTHING in constant time). – Peter Apr 05 '21 at 05:03
  • Interesting reading on the topic: [Are lists evil?](https://isocpp.org/blog/2014/06/stroustrup-lists) – user4581301 Apr 05 '21 at 06:18
  • If there are no requirements on runtimes, would ```vector newVector``` be sufficient? – strangledstranger Apr 05 '21 at 06:27
  • Both linked list and vector provide the functionality you need. One of them may be more convenient to use than another and one may be more efficient. You can implement all of those functions regardless of your choice of container though. – fabian Apr 05 '21 at 06:41
  • Ahh I see, thanks :) – strangledstranger Apr 05 '21 at 07:00

0 Answers0