Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C++, implement a Polygon class and write a main driver program to allow for t

ID: 3806338 • Letter: I

Question

In C++, implement a Polygon class and write a main driver program to allow for the instantiation of mutliple polygon objects each of varying length and width.

The definition of the Polygon class should be in an individual header (.h) file and the implementation of the class's methods should be in an individual source (.cpp) file.

The class should be designed around the following characteristics:

The name of the class should be Polygon.


The class should be composed of at least the following (data) members:

MAX_POLYGONS, (static constant) data member to indicate the maximum possible objects that can be instantiated for this class. The assigned value should be 25.

numPolygons, (static) data member to indicate the number of polygons physically instantiated.

length, data member to represent the length of the polygon.

width, data member to represent the width of the polygon.

perimeter, this value will be calculated based on the values of length and width.

area, this value will be calculated based on the values of length and width.

MIN, (constant) data member that represents the minimum possible value for the length and width of a specific polygon object. Unless otherwise specified, this value should be set to DEFAULT_MIN.

MAX, (constant) data member that represents the maximum possible value for the length and width of a specific polygon object. Unless otherwise specified, this value should be set to DEFAULT_MAX.

DEFAULT_MIN, (static constant) represents the default value to initialize the MIN data member of each object when a specific minimum value is not passed. The value assigned should be 10.

DEFAULT_MAX, (static constant) represents the default value to initialize the MAX data member of each object when a specific maximum value is not passed. The value assigned should be 100..

The class should include at least the following methods:

Default Constructor, initializes each data member with appropriate default values.

Constructor, initializes the object with specific values for the MIN and MAX data members. Remaining data members should be initialized appropriately.

Constructor, initializes the object with specific values for length, width, MIN, and MAX. Remaining data members should be initialized appropriately.

Destructor, clears out all the data members and adjusts any class level data as needed.

A method to populate the data members {length, width} from external (user) input.

Mutator methods for each data member or logical groupings, as necessary. Note that these methods should ensure the integrity of the object and should not allow data to be saved that is not appropriate for that data member. Example, length and width must be between MIN and MAX inclusive.

A method to calculate the area of the polygon.

A method to calculate the perimeter of the polygon.

Note that these methods can be invoked implicitly when an object is constructed with specific values for length and width or explicitly by the application after the values for length and width have been set.

A method to display the pertinent data members {length, width, area, perimeter}.

A method to draw a polygon of the specified length and width. You can use any character to draw the shape (i.e. border) of the polygon.

The class should allow for the instantiation of constant objects. In other words applications which use this class should be able to create polygon objects of type const.

The program should instantiate an array of pointers to objects of the Polygon class. The class data member MAX_POLYGONS should be used to declare the size of the array. The program should provide the user with the following options:

1. Build a new polygon object and assign it as the next entry in the array.

2. Draw the shape corresponding to any one of the polygons which have been built. The user can specify the desired polygon by using the objects numerical position in the array (i.e. 1 for the first polygon object, 3 for the third etc.)

3. Show a summary of information of all the polygons that have been built. The summary should include detail information about each polygon. This informaion should include the length, width, area and perimeter. The summary should also display class level statistics, inclusing how many polygons were built as well as the average area and average perimeter of all the polygons. Note that these statistics should be maintained by the class and not by the driver program.

Explanation / Answer

main.cpp


#include "Polygon.h"
#include "Date.h"
#include "Name.h"

int main()
{
//function definitions to be called in the menus
    void displayStat(Polygon**, Date, Name);//displays summary statistics of all the polygons along with the name and date
    void deletePoly(Polygon**, int, int);//deletes a polygon with an index and number of polygons as integers being passed to the function
    bool valid(int);//checks if user choice for a polygon exists

    Polygon *rect[Polygon::getMAX_POLYGONS()];//array of pointers of the polygon class


    bool cont=true;//keeps asking for user input until they choose to exit
    int choice;//keeps track of what the user inputs as their choice
    int polyChoice;//keeps track of which polygon user wants drawn of data shown

    Date createdOn;//initializing object of type date
    Name createdBy;//initializing object of type name

    createdOn.input("What is the day you are creating these polygons?");

    cout << "Who is the creator of these polygons?" << endl;
    createdBy.input();

    while(cont)
    {
        cout << "What would you like to do?" << endl;
        cout << "1) Enter a polygon" << endl;
        cout << "2) Draw a polygon" << endl;
        cout << "3) Show data of a polygon" << endl;
        cout << "4) Show summary statistics" << endl;
        cout << "5) Delete a polygon" << endl;
        cout << "6) Exit" << endl;
        cin >> choice;

        switch(choice)//switch statement determines course of action
        {
        case 1:
            rect[Polygon::getNumPolygons()]=new Polygon;
            rect[Polygon::getNumPolygons()]->input();//calls input function on the index of the polygon array that user is on
            break;

        case 2:

            do {//choice checks if a polygon is in range
            cout << "Which one?" << endl;
            cin >> polyChoice;
            }while(!valid(polyChoice));

            rect[polyChoice-1]->draw();//finds polygon requested to draw
            break;

        case 3:

            do{
            cout << "Which one?" << endl;
            cin >> polyChoice;
            }while(!valid(polyChoice));

            (*rect[polyChoice-1]).displayInfo();//displays info of polygon requested
            break;

        case 4:
            displayStat(rect, createdOn, createdBy);//displays overall statistics and name and date
            break;

        case 5:

            do{
            cout << "Which one?" << endl;
            cin >> polyChoice;
            }while(!valid(polyChoice));

            deletePoly(rect, polyChoice, Polygon::getNumPolygons());
            break;

        case 6:
            cont=false;//stops the next iteration of the while loop
            break;

        default:
            cout << "Invalid choice enter another one" << endl;
            break;
        }
    }

}

void displayStat(Polygon *a[], Date createdOn, Name createdBy)
{
    cout << "Polygons created on: ";
    createdOn.display();//calls display on date object from user input passed to the function
    cout << " by: ";
    createdBy.display();//calls display on name object from user input passed to the function

    a[0]->displayData();//calls summary statistics which outputs static variables so any instance of the class is allowed
}

void deletePoly(Polygon *a[], int index, int numPoly)
{
    delete a[index-1];//deletes pointer from user input, summary statistics are updated by the destructor

    for(int x=index-1; x<numPoly-1; x++)//loops until second to last object is reached
    {
        a[x]=a[x+1];//shifting the pointers over by one
    }
}

bool valid(int c)
{
    bool inRange=false;

    int range=Polygon::getNumPolygons();//the range is the total number of polygons

    if(c>0&&c<=range)//must lie in the range
        inRange=true;

    return inRange;//if if statement isnt executed then the false value is returned
}


Date.cpp


#include "Date.h"

Date::Date(){
   day=1;
   month=1;
   year=1900;
}

Date::Date(int m, int d, int y)
{
    day=d;
    month=m;
    year=y;
}

void Date::display()
{
    cout << month << "/" << day << "/" << year << endl;
}

bool Date::setDate(int m, int d, int y)
{
    day=d;
    year=y;
    if(m<1||m>12)//wont set m to value passed to mutator if it isn't valid
    {
        return true;
    }
    month=m;
    if(m==3)//february
        return (!(d>=1||d<=29));//if lies within range of month returns false if it doesnt returns true
    else if(m%2==1)
        return (!(d>=1||d<=31));
    else
        return (!(d>=1||d<=30));

}

void Date::input(string a)
{
    int m,d,y;
    cout<< a << endl;
    cin>> m >> d >> y;
    while(setDate(m,d,y))
    {
        cout<<"Invalid input re-enter date" << endl;
        cin>> m >> d >> y;
    }
}

int Date::getMonth()
{
   return( month );
}

int Date::getDay()
{
   return( day );
}

int Date::getYear()
{
   return( year );
}

Name.cpp


#include "Name.h"

Name::Name()//default constructor
{
    Fname="Scott";
    Lname="Fitzgerald";
    Minit='F';
}

Name::Name(string f, char m, string l)//overloaded constructor with name parameters
{

    Fname = f;
    Minit = m;
    Lname = l;
}

Name::Name(string f, string l)//overloaded constructor with name parameters if no middle init
{
    Fname= f;
    Minit=' ';
    Lname=l;
}

void Name::display()
{
    cout << Fname << " " << Minit << " " << Lname << endl;
}

void Name::setName(string f, char m, string l)
{
    Fname = f;
    Minit = m;
    Lname = l;
}

void Name::input()
{
    char check;
    cout << "What is your first name?" << endl;
    cin >> Fname;
    cout << "What is your middle initial? (if N/A enter #)" << endl;
    cin >> check;

    if(check == '#')
        Minit = ' ';
    else
        Minit = check;

    cout<< "What is your last name?" << endl;
    cin>>Lname;
}

string Name::getFname()
{
    return Fname;
}

char Name::getMinit()
{
    return Minit;
}

string Name::getLname()
{
    return Lname;
}

Name.h


#ifndef NAME_H
#define NAME_H

#include <iostream>
#include <cstdlib>

using namespace std;

class Name
{
public:
    void input();
    void display();
    Name();
    Name(string, char , string);
    Name(string, string);
    void setName(string, char , string);
    string getFname();
    char getMinit();
    string getLname();

private:
    string Fname, Lname;
    char Minit;
};

#endif

Polygon.cpp

/*
Conway Wang 10/28/15
lab midterm cisc 2000
Function definitions for the Polygon class
*/

#include "Polygon.h"

int Polygon::numPolygons;
double Polygon::pTrack;
double Polygon::aTrack;

Polygon::Polygon():MIN(DEFAULT_MIN), MAX(DEFAULT_MAX)//default constructor
{
    length=0, width=0, area=0, perimeter=0;
}

Polygon::Polygon(int a, int b):MIN(a),MAX(b)//constructor for when min and max values are passed
{
    length=0, width=0, area=0, perimeter=0;
   numPolygons++;
}

Polygon::Polygon(int a, int b, int c,int d):MIN(c),MAX(d)//constructor for when length and width and min and max values are passed
{
    length=a, width=b;
    calculateA(a,b);//variable area is set in the calculate method
    calculateP(a,b);//variable perimeter is set in the calculate method
}

//destructor
Polygon::~Polygon()
{
    numPolygons--;//1 less polygon
    pTrack-=perimeter;//perimeter is subtracted from total perimeter
    aTrack-=area;//area is subtracted from total area
}

void Polygon::input()//takes in user input to fill data members
{
    int l,w;

    do//asks for length of polygon until input is in range
    {
        cout << "What is the length of the polygon? ";
        cin >> l;
    }while(!setLength(l));

    do//asks for width of polygon until input is in range
    {
        cout << "What is the width of the polygon? ";
        cin >> w;
    }while(!setWidth(w));
    numPolygons++;
    area = calculateA(l,w);//calculates area
    perimeter = calculateP(l,w);//calculates perimeter
}

bool Polygon::setLength(int l)//checks if the length is within min and max and stops loop and sets the length if it is
{
    bool a=false;
    if(l>=MIN && l<=MAX)
    {
        length=l;
        a=true;
    }
    return (a);
}

bool Polygon::setWidth(int w)//checks if the width is within min and max and stops loop and sets the width if it is
{
    bool a=false;
    if(w>=MIN && w<=MAX)
    {
        width=w;
        a=true;
    }
    return a;
}

void Polygon::draw() const
{
    for(int x=0; x<width; x++)//what to print for each row
    {
        for(int y=0; y<length; y++)
        {
                if(x==0||x==(width-1))//if first row all x's
                {
                    cout<< 'x';
                }

                else if(y==0||y==(length-1))//if column first or last, x
                {
                        cout << 'x';
                }

                else
                {
                        cout << ' ';//otherwise blank
                }
    }
    cout<< endl;
}
}

void Polygon::displayInfo() const//displays private data members of the function
{
    cout << "length: " << length << endl;
    cout << "width: " << width << endl;
    cout << "area: " << area << endl;
    cout << "perimeter: " << perimeter << endl;
}

void Polygon::displayData() const
{
    cout << "number of polygons: " << numPolygons << endl;
    cout << "average area: " << aTrack/numPolygons << endl;//total area divided by number of polygons gives average area
    cout << "average perimeter " << pTrack/numPolygons << endl;//total perimeter divided by number of polygons gives average perimeter
}

int Polygon::calculateA(int length,int width)
{
   area = length*width;
   aTrack += area;//adds on area to static variable keeping track of the sum of all areas
    return area;
}

int Polygon::calculateP(int length,int width)
{
   perimeter=2*length*2*width;
   pTrack+=perimeter;//adds on perimeter to static variable keeping track of the sum of all perimeters
    return perimeter;
}

int Polygon::getNumPolygons()
{
    return numPolygons;
}

int Polygon::getMAX_POLYGONS()
{
    return MAX_POLYGONS;
}

int Polygon::getPerimeter() const
{
    return perimeter;
}

int Polygon::getArea() const
{
    return area;
}


Polygon.h


#ifndef POLYGON_H
#define POLYGON_H

#include <iostream>
#include <cstdlib>

using namespace std;

class Polygon{
public:
    Polygon();//default constructor
    Polygon(int,int);//constructor with width and legth
    Polygon(int,int,int,int);//constructor with width length max and min
   ~Polygon();//destructor adjusts class level statistics
    bool setLength(int), setWidth(int);
    void input();
    int getArea() const;
    int getPerimeter() const;
    void displayInfo() const;
    void draw() const;
    void displayData() const;
    int calculateA(int,int), calculateP(int,int);
    static int getNumPolygons();
    static int getMAX_POLYGONS();

private:

    static const int DEFAULT_MIN=10, DEFAULT_MAX=100, MAX_POLYGONS=25;//static const declared in the class
    static int numPolygons;
   static double pTrack,aTrack;
    const int MIN, MAX;
    int length, width, area, perimeter;
};

#endif

sample output

What is the day you are creating these polygons?
10 28 2015
Who is the creator of these polygons?
What is your first name?
Conway
What is your middle initial? (if N/A enter #)
#
What is your last name?
Wang
What would you like to do?
1) Enter a polygon
2) Draw a polygon
3) Show data of a polygon
4) Show summary statistics
5) Delete a polygon
6) Exit
1
What is the length of the polygon?
10
What is the width of the polygon?
9
What is the width of the polygon?
10
What would you like to do?
1) Enter a polygon
2) Draw a polygon
3) Show data of a polygon
4) Show summary statistics
5) Delete a polygon
6) Exit
1
What is the length of the polygon?
15
What is the width of the polygon?
20
What would you like to do?
1) Enter a polygon
2) Draw a polygon
3) Show data of a polygon
4) Show summary statistics
5) Delete a polygon
6) Exit
1
What is the length of the polygon?
20
What is the width of the polygon?
15
What would you like to do?
1) Enter a polygon
2) Draw a polygon
3) Show data of a polygon
4) Show summary statistics
5) Delete a polygon
6) Exit
2
Which one?
1
xxxxxxxxxx
x        x
x        x
x        x
x        x
x        x
x        x
x        x
x        x
xxxxxxxxxx
What would you like to do?
1) Enter a polygon
2) Draw a polygon
3) Show data of a polygon
4) Show summary statistics
5) Delete a polygon
6) Exit
2
Which one?
3
xxxxxxxxxxxxxxxxxxxx
x                  x
x                  x
x                  x
x                  x
x                  x
x                  x
x                  x
x                  x
x                  x
x                  x
x                  x
x                  x
x                  x
xxxxxxxxxxxxxxxxxxxx
What would you like to do?
1) Enter a polygon
2) Draw a polygon
3) Show data of a polygon
4) Show summary statistics
5) Delete a polygon
6) Exit
2
Which one?
4
Which one?
3
xxxxxxxxxxxxxxxxxxxx
x                  x
x                  x
x                  x
x                  x
x                  x
x                  x
x                  x
x                  x
x                  x
x                  x
x                  x
x                  x
x                  x
xxxxxxxxxxxxxxxxxxxx
What would you like to do?
1) Enter a polygon
2) Draw a polygon
3) Show data of a polygon
4) Show summary statistics
5) Delete a polygon
6) Exit
3
Which one?
2
length: 15
width: 20
area: 300
perimeter: 1200
What would you like to do?
1) Enter a polygon
2) Draw a polygon
3) Show data of a polygon
4) Show summary statistics
5) Delete a polygon
6) Exit
3
Which one?
2
length: 15
width: 20
area: 300
perimeter: 1200
What would you like to do?
1) Enter a polygon
2) Draw a polygon
3) Show data of a polygon
4) Show summary statistics
5) Delete a polygon
6) Exit
4
Polygons created on: 10/28/2015
by: Conway   Wang
number of polygons: 3
average area: 233.333
average perimeter 933.333
What would you like to do?
1) Enter a polygon
2) Draw a polygon
3) Show data of a polygon
4) Show summary statistics
5) Delete a polygon
6) Exit
5
Which one?
2
What would you like to do?
1) Enter a polygon
2) Draw a polygon
3) Show data of a polygon
4) Show summary statistics
5) Delete a polygon
6) Exit
4
Polygons created on: 10/28/2015
by: Conway   Wang
number of polygons: 2
average area: 200
average perimeter 800
What would you like to do?
1) Enter a polygon
2) Draw a polygon
3) Show data of a polygon
4) Show summary statistics
5) Delete a polygon
6) Exit
6