/*

10.13 (Geometry: n-sided regular polygon) An n-sided regular polygon has n sides of the same length, and all its angles have the same degree (i.e., the polygon is both equilateral and equiangular). 

Design a class named RegularPolygon that contains the following:

A private int data field named n that defines the number of sides in the polygon.
A private double data field named side that stores the length of the side.
A private double data field named x that defines the x-coordinate of the center of the polygon.
A private double data field named y that defines the y-coordinate of the center of the polygon.
A no-arg constructor that creates a regular polygon with n 3, side 1, x 0, and y 0.
A constructor that creates a regular polygon with the specified number of sides and length of side, and centered at (0, 0).
A constructor that creates a regular polygon with the specified number of sides, length of side, and x- and y-coordinates.
The constant accessor functions and mutator functions for all data fields.
The constant function getPerimeter() that returns the perimeter of the polygon.

The constant function getArea() that returns the area of the polygon. The formula for computing the area of a regular polygon is.

Area=(n*(side*side))/(4*tan(π/n)).

Draw the UML diagram for the class. 
Implement the class. 
Write a test program that creates three RegularPolygon objects, using the no-arg constructor, using RegularPolygon(6, 4), and using RegularPolygon(10, 4, 5.6, 7.8). 
For each object, display its perimeter and area.

*/

#include <bits/stdc++.h>
#include <iostream>
using namespace std;


class RegularPolygon 
{
  private:
  int n;
  double side, x, y;
  public:
  RegularPolygon() 
  {
    n = 3;
    side = 1;
    x = 0;
    y = 0;
  }
  RegularPolygon(int a, double b) 
  {
    n = a;
    side = b;
    x = 0;
    y = 0;
  }
  RegularPolygon(int a, double b, double c, double d) 
  {
    n = a;
    side = b;
    x = c;
    y = d;
  }
  
  void setN(int i)
  {
    n = i;
  }
  void setSide(double i)
  {
    side = i;
  }
  void setX(double i)
  {
    x = i;
  }
  void setY(double i)
  {
    y = i;
  }
  
  int getN()
  {
    return n;
  }
  double getSide()
  {
    return side;
  }
  double getX()
  {
    return x;
  }
  double getY()
  {
    return y;
  }

  double getPerimeter() 
  {
    return (n * side);
  }  
  double getArea() 
  {
    return (n*(side*side))/(4*tan(M_PI/n));
  }


}; // Must place a semicolon here

int main() {
  RegularPolygon poly1;
  RegularPolygon poly2(6,4);
  RegularPolygon poly3(10, 4, 5.6, 7.8);

  cout << "poly1 perimeter: " << poly1.getPerimeter() << endl;
  cout << "poly1 area: " << poly1.getArea()  <<  endl;
  cout << "poly2 perimeter: " << poly2.getPerimeter() << endl;
  cout << "poly2 area: " << poly2.getArea()  <<  endl;
  cout << "poly3 perimeter: " << poly3.getPerimeter() << endl;
  cout << "poly3 area: " << poly3.getArea()  <<  endl;
  return 0;
} 
by

C++ Online Compiler

Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++ and start coding!

Read inputs from stdin

OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.

#include <iostream>
#include <string>
using namespace std;

int main() 
{
    string name;
    cout << "Enter name:";
    getline (cin, name);
    cout << "Hello " << name;
    return 0;
}

About C++

C++ is a widely used middle-level programming language.

  • Supports different platforms like Windows, various Linux flavours, MacOS etc
  • C++ supports OOPS concepts like Inheritance, Polymorphism, Encapsulation and Abstraction.
  • Case-sensitive
  • C++ is a compiler based language
  • C++ supports structured programming language
  • C++ provides alot of inbuilt functions and also supports dynamic memory allocation.
  • Like C, C++ also allows you to play with memory using Pointers.

Syntax help

Loops

1. If-Else:

When ever you want to perform a set of operations based on a condition If-Else is used.

if(conditional-expression) {
   //code
}
else {
   //code
}

You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.

2. Switch:

Switch is an alternative to If-Else-If ladder.

switch(conditional-expression){    
case value1:    
 // code    
 break;  // optional  
case value2:    
 // code    
 break;  // optional  
......    
    
default:     
 code to be executed when all the above cases are not matched;    
} 

3. For:

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement){  
  //code  
} 

4. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while (condition) {  
// code 
}  

5. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {  
 // code 
} while (condition); 

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.

How to declare a Function:

return_type function_name(parameters);

How to call a Function:

function_name (parameters)

How to define a Function:

return_type function_name(parameters) {  
 // code
}