Question

In: Computer Science

Study the file polygon.h. It contains the header file for a class of regular polygons. Implement...

Study the file polygon.h. It contains the header file for a class of regular polygons. Implement the methods, and provide a driver to test it. It should be in C++

Write a polygon.h file with given instructions for the polygon.cpp file

#include <iostream>
#include <math.h>
using namespace std;
#ifndef M_PI
# define M_PI 3.14159265358979323846
#endif
int main()
{
   float areaP, length, sides;
   cout << "\n\n Polygon area program.\n";
   cout << "---------------------------------\n";
   cout << " Enter the number of sides: ";
   cin >> sides;
   cout << " Enter the length of one side: ";
   cin >> length;
   areaP = (sides * (length * length)) / (4.0 * tan((M_PI / sides)));
   cout << " The area of the ploygon is: " << areaP << "\n";
   cout << " Done..." << endl;
       system("pause");
   return 0;

}

Also include suitable methods and write a code to provide a driver to test it.

Solutions

Expert Solution

Polygon.h

#include <iostream>

#include <sstream>

#include <cmath>

# define M_PI 3.14159265358979323846

using namespace std;

class Polygon

{

private:

int numSides;

float sideLength;

public:

Polygon();

Polygon(int, float);

int getNumberOfSides();

float getSideLength();

float getArea();

string toString();

};

Polygon.cpp

#include "Polygon.h"

Polygon::Polygon()

{

this->numSides = 0;

this->sideLength = 0.0f;

}

Polygon::Polygon(int sides, float len)

{

this->numSides = sides;

this->sideLength = len;

}

int Polygon::getNumberOfSides(){ return this->numSides; }

float Polygon::getSideLength(){ return this->sideLength; }

float Polygon::getArea()

{

return (numSides * (sideLength * sideLength)) / (4.0 * tan((M_PI / numSides)));;

}

string Polygon::toString()

{

stringstream ss;

ss << "Number of sides: " << this->numSides << endl << "Length of each side: " << this->sideLength << " units" << endl << "Area: " << getArea() << " sq. units" << endl;

return ss.str();

}

main.cpp

#include "Polygon.h"

int main()

{

int numOfSides;

float sideLength;

cout << "\n\n Polygon area program.\n";

cout << "---------------------------------\n";

cout << " Enter the number of sides: ";

cin >> numOfSides;

cout << " Enter the length of one side: ";

cin >> sideLength;

Polygon polygon(numOfSides, sideLength);

cout << endl << polygon.toString() << endl;

}

**************************************************************** SCREENSHOT ***********************************************************


Related Solutions

Study the file polygon.h. It contains the header file for a class of regular polygons. Implement...
Study the file polygon.h. It contains the header file for a class of regular polygons. Implement the methods, and provide a driver to test it. It should be in C++ polygon.h file- #ifndef POLY_RVC_H #define POLY_RVC_H #include <iostream> using namespace std; class Polygon { public:    Polygon();    Polygon(int n, double l);    //accessors - all accessors should be declared "const"    // usually, accessors are also inline functions    int getSides() const { return sides; }    double getLength()...
A header file contains a class template, and in that class there is a C++ string...
A header file contains a class template, and in that class there is a C++ string object. Group of answer choices(Pick one) 1)There should be a #include for the string library AND a using namespace std; in the header file. 2)There should be a #include for the string library. 3)There should be a #include for the string library AND a using namespace std; in the main program's CPP file, written before the H file's include.
please complete the header file that contains a class template for ADT Queue and complete all...
please complete the header file that contains a class template for ADT Queue and complete all the member functions in the class template. Submit the header file only, but please write a source file that tests all the member functions to make sure they are working correctly. queue.h #ifndef _QUEUE #define _QUEUE #include"Node.h" template<class ItemType> class Queue { private:    Node<ItemType> *backPtr;    Node<ItemType> *frontPtr; public:    Queue(); //Default constructor    Queue(const Queue<ItemType> &aQueue);    bool isEmpty() const;    bool...
Problem Statement: Implement the MyString class using a header and implementation file named MyString.h and MyString.cpp...
Problem Statement: Implement the MyString class using a header and implementation file named MyString.h and MyString.cpp respectively. Make sure to properly test your code on your own by creating a test driver that tests every function created in the MyString class. Deliverables: proj3-MyString.h proj3-MyString.cpp proj3-testMain.cpp Memory Requirements: Your MyString should start with 10 bytes of allocated memory and should grow in size by doubling. So, we should be able to predict the capacity of your MyString as acquiring a patten...
Write a C++ program that design a class definition to be put in a header file...
Write a C++ program that design a class definition to be put in a header file called fizzjazz.h A store sells online FizzJazz which are sound tones made by famous bands. For each FizzJazz, the store wants to keep track of the following attributes: * title - the name of the sound tone * band - Famous band name that created the tone * duration - this is in seconds and may be fractional: examples: 20.0, 34.5 Each attribute will...
Write a C++ program that design a class definition to be put in a header file...
Write a C++ program that design a class definition to be put in a header file called fizzjazz.h A store sells online FizzJazz which are sound tones made by famous bands. For each FizzJazz, the store wants to keep track of the following attributes: * title - the name of the sound tone * band - Famous band name that created the tone * duration - this is in seconds and may be fractional: examples: 20.0, 34.5 Each attribute will...
Write a C++ program that design a class definition to be put in a header file...
Write a C++ program that design a class definition to be put in a header file called fizzjazz.h A store sells online FizzJazz which are sound tones made by famous bands. For each FizzJazz, the store wants to keep track of the following attributes: * title - the name of the sound tone * band - Famous band name that created the tone * duration - this is in seconds and may be fractional: examples: 20.0, 34.5 Each attribute will...
In a header file Record.h, create a Record structure that contains the following information: recordID, firstName,...
In a header file Record.h, create a Record structure that contains the following information: recordID, firstName, lastName, startYear. recordID is an integer. In testRecord, first create a record (record1) using “normal” pointers. Set the values to 1001, Fred, Flintstone, 1995 In testRecord, create a new record (record2) using a unique smart pointer. Set the values to 1002, Barney, Rubble, 2000 Create a function (or functions) that will print the records to any output stream. Since the print function does not...
Objective You are given a partial implementation of one header file, GildedRose.h. Item is a class...
Objective You are given a partial implementation of one header file, GildedRose.h. Item is a class that holds the information for each item for the inn. GildedRose is a class that holds an internal listing of many Item objects. This inventory should hold at least 10 items. For this you can use arrays, the std::array class, or even the vector class. Complete the implementation of these classes, adding public/private member variables and functions as needed. You should choose an appropriate...
using the header: #include <pthread.h> // This is a header file for a Read/Right Lock Library....
using the header: #include <pthread.h> // This is a header file for a Read/Right Lock Library. Your C code //SHOULD access your routines using these exact function // prototypes typedef struct RW_lock_s { } RW_lock_t; void RW_lock_init(RW_lock_t *lock); /* This routine should be called on a pointer to a struct variable of RW_lock_t to initialize it and ready it for use. */ void RW_read_lock(RW_lock_t *lock); /* This routine should be called at the beginning of a READER critical section */...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT