Hey Guys! I am trying to create a polygon class, but I've come across a few errors. The first is that I am supposed to provide summary statistics for all the objects that have been created, which include the average area and perimeter. The catch is that I have to include them in the methods, not in the driver. The average area and perimeter are not being calculated correctly. Also, I am really struggling to create a decent method to draw the polygon, so any suggestions will be incredibly helpful. This is my first time using pointers, so please feel free to let me know anyway to improve! Lastly, thanks for your help!
Did you get compiler errors? I think you should have - post them here.
Line 163: Array size needs to be a constant known at compile time. Read up on how to do dynamic allocation.
Line 165: the normal form for a for loop is this:
for (int i = 0; i < N; i++){
Your use of it with the array means you miss the first element pol[0] , and it will go past the last element pol[N]
Line 170: you are calling delete inside the for loop - not good. it is delete[] when deleting an entire array.
Can you choose better variable names? N , i , pol are not good ones. For some reason, people think it is required to over-abbreviate variable names. I would have had ArraySize, Counter and APloygon. Single char variable names in loops is OK, but I don't like ones that look similar - as in ij mn etc.
The big misleading thing is that the class is called polygon, but it is really a rectangle - are you sure the assignment wasn't meant to be for general polygons such as hexagon etc?
I did not receive any compiler errors when running this. Yes, you are correct in stating that I am drawing rectangles, but I am unsure as to how to go about drawing other polygons, which is why I asked for any suggestions. I was asked to at LEAST include length and width as data members. I have a static constant data member called Max_polygons, not included above, which sets the max number of polygons a person can create to 25. However, I am unsure how I can apply it to my driver. I think this is what is needed for the array size to be known at compiler time, but I am just unsure how to do it. I appreciate your thoughts on variable names very much, and I will be sure to change them. Below is a run of the program.
Welcome to the Polygon Creator! How many polygons would you like to create today ?
2
LENGTH: 15
WIDTH: 15
AREA: 225
PERIMETER: 60
MIN: 10
MAX: 100
AVERAGE AREA: 0
AVERAGE PERIMETER: 0
Please input the length of the polygon.
13
Please input the width of the polygon.
14
LENGTH: 13
WIDTH: 14
AREA: 182
PERIMETER: 54
MIN: 10
MAX: 100
AVERAGE AREA: 54
AVERAGE PERIMETER: 54
LENGTH: 15
WIDTH: 15
AREA: 225
PERIMETER: 60
MIN: 10
MAX: 100
AVERAGE AREA: 0
AVERAGE PERIMETER: 0
Please input the length of the polygon.
15
Please input the width of the polygon.
16
LENGTH: 15
WIDTH: 16
AREA: 240
PERIMETER: 62
MIN: 10
MAX: 100
AVERAGE AREA: 58
AVERAGE PERIMETER: 58
LENGTH: 15
WIDTH: 15
AREA: 225
PERIMETER: 60
MIN: 10
MAX: 100
AVERAGE AREA: 0
AVERAGE PERIMETER: 0
Please input the length of the polygon.
11
Please input the width of the polygon.
13
LENGTH: 11
WIDTH: 13
AREA: 143
PERIMETER: 48
MIN: 10
MAX: 100
AVERAGE AREA: 54
AVERAGE PERIMETER: 54
Destructor runs.
As you can see, the average area and perimeter are not correct. I did not include the draw polygon method at this time since it does not print out anything when I run it. Oh and i put the delete pol[N] outside the for loop and adjusted the for loop appropriately when I did this. Thanks for your time
It looks like you have a collection of polygons and individual polygons. The individuals are doing stuff to update the collection, such as computing numPolygons, average perimeter and total perimeter. That's a bad design. An individual polygon should be responsible for itself only. If you want to do things with a collection of polygons, then create a separate class to deal with the collection.
Is there a way to fix it? I have posted my header file down below
#ifndef POLYGON_H
#define POLYGON_H
#include <iostream>
using namespace std;
class Polygon{
public:
Polygon();//default constructor
Polygon(int, int); //constructor that initializes specific values for max and min
Polygon(int, int, int, int, int, int); //constructor that initializes specific values for length, width, max, and min
~Polygon();
//accessor methods
static int getNumPolygons();
int getLength();
int getWidth();
int getArea();
int getPerimeter();
int getAvgArea();
int getAvgPerimeter();
int getMIN();
int getMAX();
//methods to calculate area and perimeter
void calculateArea();
void calculatePerimeter();
void averageArea(); //method to calculate average area of all the polygons
void averagePerimeter(); //method to calculate the average perimeter of all the polygons
//method to draw polygon
void DrawPolygon();
private:
static const int MAX_POLYGONS = 25;
static int numPolygons, totalArea, totalPerimeter;
const int MAX, MIN;
static const int DEFAULT_MIN = 10;
static const int DEFAULT_MAX = 100;
int length, width, perimeter, area, avgArea, avgPerimeter;
My instructor required that the summary statistics such as the perimeter, area, average perimeter and average area, be maintained by the polygon class and not by the driver itself, is there any way to implement these instructions into my code?
Maybe the interpretation of the assignment is a little off because you only have 1 class. As dhayden was saying, you need a class to hold the stats. When the assignment says " ...... and not by the driver itself ....", driver means the main() function. I imagine there wasn't anything in the assignment that forbade you from creating another class.
Your avgArea, avgPerimeter aren't static hence your problems in that area. But you should really have them in a different class. I think you might risk loosing marks if you don't.
Some other things I noticed:
It's great you are using ctor initialiser lists, but you should use them to set all the member data, in the same order as what they are listed in the class definition. Validation of the values should either be done before the ctor is called, or in the ctor body, but then one should throw an exception or set a global error object if there are problems. I understand this is a fairly beginners assignment, so you should at least print an error message. And I think you will earn some brownie points for actually doing validation of some kind.
On that, int is not a particularly good type for distances or areas, unsigned or std::size_t is better. Or double, provided you have a way ensuring values are positive.
Prefer to declare your variables 1 per line, this help avoid problems such as qualifiers sometimes not being applied to all variables.
Make the arguments to your functions const , and mark your accessors const as well - they don't change the state of the class.
I am not sure that you need all those set functions, they are only required if an object needs to change after it has been created, use of ctor init list obviates that.
In the header file provide names for the functions arguments, make them the same as those in the function definitions.