Write a C program using 1 or more classes that will calculat
Write a C++ program using 1 or more classes that will calculate and display the areas and perimeters of the regular polygons (congruent sides and interior angles) listed below. Allow side lengths to be specified as positive floating point numbers. Be sure to write a thorough test program. equilateral triangle square regular pentagon regular hexagon regular heptagon regular octagon
Solution
/*
* Areas.cpp
*
* Created on: 27-Feb-2017
*
*/
#include<iostream>
#include<math.h>
class Polygons
{
double side_length;
int no_of_sides;
double apothem()
{
//a=side_length/ 2tan(180/no_of_sides)
double an=180.0 / no_of_sides;
return side_length / ( 2 * tan(an*3.14159265/180) );
}
public:
Polygons( double side_length, int no_of_sides )
{
this->side_length = side_length;
this->no_of_sides = no_of_sides;
}
double perimeter()
{
return no_of_sides * side_length;
}
double area()
{
//area = apothem*perimeter/2
return apothem() * perimeter() / 2;
}
};
int main(){
Polygons *p=new Polygons(10,4);//side_length ,no_of_sides
std::cout<<\"perimeter: \"<<p->perimeter()<<\"\ \";
std::cout<<\"Area: \"<<p->area()<<\"\ \";
delete p;
return 0;
}

