Polygon Class, help much appreciated!

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!

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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//METHODS FOR CLASS
#include "Polygon.h"
//static variables
int Polygon::numPolygons = 0;
int Polygon::totalArea = 0;
int Polygon::totalPerimeter = 0;

//constructors
Polygon::Polygon(): MIN(DEFAULT_MIN), MAX(DEFAULT_MAX){

        length = 15;
        width = 15;
        perimeter = 60;
        area = 225;
        numPolygons++;
}
Polygon::Polygon(int minval, int maxval):MIN(minval), MAX(maxval){
        length = 15;
        width = 15;
        perimeter = 60;
        area = 225;
        numPolygons++;
}
Polygon::Polygon(int minval, int maxval, int l, int w, int p, int a):MIN(minval), MAX(maxval){
        length = l;
        width = w;
        perimeter = p;
        area = a;
        numPolygons++;
}
//destructor
Polygon::~Polygon(){
        cout << "Destructor runs." <<endl;
}
//input and display methods
void Polygon::input(){
        inputLength();
        inputWidth();
        calculateArea();
        calculatePerimeter();
        averageArea();
        averagePerimeter();
}

void Polygon::display(){
        cout << "LENGTH: " << getLength() << endl;
        cout << "WIDTH: " << getWidth() << endl;
        cout << "AREA: " << getArea() << endl;
        cout << "PERIMETER: " << getPerimeter() << endl;
        cout << "MIN: " << getMIN() << endl;
        cout << "MAX: " << getMAX() << endl;
        cout << "\n\nAVERAGE AREA: " << getAvgArea() << endl;
        cout << "AVERAGE PERIMETER: " << getAvgPerimeter() << endl;

        //DrawPolygon();
}
//mutator methods with validations
bool Polygon::setLength(int l){
        bool validate = false;
        if(l >= MIN && l <= MAX){
                length = l;
                validate = true;
        }

        return (validate);
}

bool Polygon::setWidth(int w){
        bool validate = false;
        if (w >= MIN && w <= MAX){
                width = w;
                validate = true;
        }

        return (validate);
}

//individual input methods for length and width
void Polygon::inputLength(){
        int l;
        do{
                cout << "Please input the length of the polygon." <<endl;
                cin >> l;
        }while(!setLength(l));
}

void Polygon::inputWidth(){
        int w;
        do{
                cout << "Please input the width of the polygon." <<endl;
                cin >> w;
        }while(!setWidth(w));
}

//methods to calculate area, perimeter, average area, and average perimeter
void Polygon::calculateArea(){
        area = length*width;
        totalArea = totalArea + area;
}
void Polygon::calculatePerimeter(){
        perimeter = (2*width) + (2*length);
        totalPerimeter = totalPerimeter + perimeter;
}
void Polygon::averageArea(){
        avgArea = totalArea/numPolygons;

}
void Polygon::averagePerimeter(){
        avgPerimeter = totalPerimeter/numPolygons;

}
//accessor methods
int Polygon::getNumPolygons(){
        return (numPolygons);
}
int Polygon::getLength(){
        return (length);
}
int Polygon::getWidth(){
        return (width);
}
int Polygon::getArea(){
        return(area);
}
int Polygon::getPerimeter(){
        return (perimeter);
}
int Polygon::getAvgPerimeter(){
        return (avgPerimeter);
}
int Polygon::getAvgArea(){
        return (avgPerimeter);
}
int Polygon::getMIN(){
        return (MIN);
}
int Polygon::getMAX(){
        return (MAX);
}

void Polygon::DrawPolygon(){
        for(int i = 0; i <= length; i++){
                for(int j = 0; j <= width; j++){
                        if (i > 0 || i < length || j > 0 || j < width){
                                cout << " " << endl;
                        }
                        else{
                                cout << "*" << endl;
                          }
                }
                cout << endl;
        }
}

//DRIVER
#include "Polygon.h"

main(){
        int N;

        cout << "Welcome to the Polygon Creator! How many polygons would you like to create today?" << endl;
        cin >> N;
        Polygon *pol[N]; //create an array of pointer objects

                for (int i = 1; i <= N; i++){
                        pol[i] = new Polygon;
                        pol[i] -> display();
                        pol[i] -> input();
                        pol[i] -> display();
                        delete pol[i];
                }
}
~


Last edited on
You did not allocate the array
Hi,

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 i j m n 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
Last edited on
Please post Polygon.h.

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();

//input methods
void input();
void inputLength();
void inputWidth();
//display method
void display();

//mutator methods
bool setLength(int);
bool setWidth(int);

//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;

};
#endif
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?
Hi,

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.
Topic archived. No new replies allowed.