Using the idea of inheritance and polymorphism write a C++ program that calculates the area of regular polygons.
This is a typical output when the desired program is executed:
>> please enter the sides of your polygon: 3,4,5
>> Your polygon is a triangle and its area is: 6
In your program, you must check that the values entered are correct and can build a polygon. The number of the digits entered initially can help software to determine the polygon type. For example, if 4 numbers are entered then it is a 4 sided polygon.
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
//abstract base class
class base
{
protected :
double a,b,c,ar;
public:
virtual void input()=0;
virtual void area()=0;
};
//triangle derived class
class triangle : public base
{
public:
void input();
void area();
};
//quadrilateral derived class
class quadrilateral : public base
{
public:
void input();
void area();
};
//pentagon derived class
class pentagon : public base
{
public:
void input();
void area();
};
//hexagon derived
class hexagon : public base
{
public:
void input();
void area();
};
//heptagon derived class
class heptagon : public base
{
public:
void input();
void area();
};
//octagon derived class
class octagon : public base
{
public:
void input();
void area();
};
//nonagon derived class
class nonagon : public base
{
public:
void input();
void area();
};
//decagon derived class
class decagon : public base
{
public:
void input();
void area();
};
//input method for triangle
void triangle :: input()
{
cout<<endl<<"Enter the three sides
of triangle";
cin>>a>>b>>c;
}
//input method for quadrilateral
void quadrilateral :: input()
{
cout<<endl<<"Enter the side of
quadrilateral";
cin>>a;
}
//input method for pentagon
void pentagon :: input()
{
cout<<endl<<"Enter the side of
pentagon";
cin>>a;
}
//input method for hexagon
void hexagon :: input()
{
cout<<endl<<"Enter the side of
hexagon";
cin>>a;
}
//input method for heptagon
void heptagon :: input()
{
cout<<endl<<"Enter the side of
heptagon";
cin>>a;
}
//input method for octagon
void octagon :: input()
{
cout<<endl<<"Enter the side of
octagon";
cin>>a;
}
//input method for nonagon
void nonagon :: input()
{
cout<<endl<<"Enter the side of
nonagon";
cin>>a;
}
//input method for decagon
void decagon :: input()
{
cout<<endl<<"Enter the side of
decagon";
cin>>a;
}
//area for triangle
void triangle :: area()
{
double s;
s=(a+b+c)/2;
s= s * (s-a) * (s-b) * (s-c);
s=sqrt(s);
cout<<endl<<"Your Polygon is a triangle
and its area is : "<<s<<" sq. units.";
}
//area for quadrilateral
void quadrilateral :: area()
{
cout<<endl<<"Your Polygon is a
quadrilateral and its area is : "<<a*a<<" sq.
units.";
}
//area for pentagon
void pentagon :: area()
{
double t;
t= sqrt(5);
t= 2*t;
t= 5+t;
t=5*t;
t=sqrt(t);
t= 0.25 * t * (a*a);
cout<<endl<<"Your Polygon is a pentagon
and its area is : "<<t<<" sq.
units.";
}
//area of hexagon
void hexagon :: area()
{
double t;
t=sqrt(3);
t=3*t;
t=t/2;
t=t * (a*a);
cout<<endl<<"Your Polygon is a hexagon and
its area is : "<<t<<" sq. units.";
}
//area of heptagon
void heptagon :: area()
{
double t;
t=1.75 * a*a;
t= t * cos(25.71)/sin(25.71);
cout<<endl<<"Your Polygon is a heptagon
and its area is : "<<t<<" sq.
units.";
}
//area of octagon
void octagon :: area()
{
double t;
t=sqrt(2);
t=2*(1+t)*(a*a);
cout<<endl<<"Your Polygon is a heptagon
and its area is : "<<t<<" sq.
units.";
}
//area of octagon
void nonagon :: area()
{
double t;
t=2.25 * (a*a);
t= t * cos(20)/sin(20);
cout<<endl<<"Your Polygon is a nonagon and
its area is : "<<t<<" sq. units.";
}
//area of octagon
void decagon :: area()
{
double t;
t=sqrt(5);
t=2*t;
t=5+t;
t=sqrt(t);
t= 2.5 *(a*a) *t;
cout<<endl<<"Your Polygon is a decagon and
its area is : "<<t<<" sq. units.";
}
int main()
{
int n,opt=1;
base *obj;
//derived class objects
triangle t;
quadrilateral q;
pentagon p;
hexagon h;
heptagon he;
octagon oc;
nonagon no;
decagon de;
//infinite loop
while(opt==1)
{
cout<<"Enter the number of sides of
polygon";
cin>>n;
//check the validity of sides
if(n<3 || n>10)
{
cout<<endl<<"Invalid Polygon";
break;
}
//block for triangle
if(n==3)
{
obj=&t;
obj->input(
);
obj->area();
}
else
if(n==4) //block for
quadrilateral
{
obj=&q;
obj->input(
);
obj->area();
}
else
if(n==5) //block for pentagon
{
obj=&p;
obj->input(
);
obj->area();
}
else
if(n==6) //block forhexagon
{
obj=&h;
obj->input(
);
obj->area();
}
else
if(n==7) //block for heptagon
{
obj=&he;
obj->input(
);
obj->area();
}
else
if(n==8) //block for octagon
{
obj=&oc;
obj->input(
);
obj->area();
}
else
if(n==9) //block for nonagon
{
obj=&no;
obj->input(
);
obj->area();
}
else
if(n==10) //block for decagon
{
obj=&de;
obj->input(
);
obj->area();
}
cout<<endl<<"Do you
want to compute more, Enter 1 to continue, any value to
stop";
cin>>opt;
}
}
OUTPUT
NOTE: FOR SIDE 4 I HAVE COMPUTED FOR SQUARE, IF YOU NEED FOR ALL QUADRILITERALS THEN INFORM.
Using the idea of inheritance and polymorphism write a C++ program that calculates the area of...
N-Sided Polygon An n-sided polygon is a planed figure whose sides have the same length and whose angles have the same degree. Write a class called NSidedPolygon that contains the following components: Private members to store the name of a polygon, the number of sides, and the length of each side. (You are expected to select the appropriate data types for each member.) A constructor that accepts the number of sides and the length of each side. A private method...
Part A) Write a C++ program that calculates the area under a curve. Here is the equation: f(x) = x^2 +1.5x +4 You must prompt the user for the beginning and the ending x values. You are to assume, but not check, that the user will put in whole positive numbers. The units are inches. The program input/output should look something like this: This program calculates the area under a curve between two points on the x axis. The equation...
In a sensible language ( any of C#/Java/Python) write a program which correctly calculates add, subtract, multiply and divide using our ‘minifloat’ binary format using an algorithm you code yourself. Some details: Your program only needs to work on two ‘numbers’ at a time, read those in from a text file – failure to read those values from a text file will result in a grade of 0. For each ‘number’ store the sign, exponent and mantissa separately. You can hard code your...
Question 4-6 Please. Python 3.6. def main(). entered by a user. Write a program that finds the sum and average of a series of numbers he program should first prompt the user to enter total numbers of numbers are to be summed and averaged. It should then as for input for each of the numbers, add them, and print the total of the numbers and their average 2. Write a progra m that finds the area of a circle. The...
JAVA: Quadrilateral Inheritance Hierarchy Write an inheritance hierarchy for classes Quadrilateral, Parallelogram, Rectangle, and Square. Use Quadrilateral as the superclass of the hierarchy. Create and use a Point class to represent the points in each shape. Make the hierarchy as deep as possible, i.e. more than two levels. Specify the instance variables and methods for each class. The private instance variables of Quadrilateral should be the x-y coordinate pairs for the four endpoints of the Quadrilateral. A program that instantiates...
Write in C++ using emacs. Write a program that calculates the ending balance of several savings accounts. The program reads information about different customers’ accounts from an input file, “accounts.txt”. Each row in the file contains account information for one customer. This includes: the account number, account type (premium, choice, or basic account), starting balance, total amount deposited, and total amount withdrawn. Your program should: - open the file and check for successful open, - then calculate the ending balance...
C++ programming For this assignment, write a program that will act as a geometry calculator. The program will be menu-driven and should continue to execute as long as the user wants to continue. Basic Program Logic The program should start by displaying a menu similar to the following: Geometry Calculator 1. Calculate the area of a circle 2. Calculate the area of a triangle 3. Quit Enter your choice(1-3): and get the user's choice as an integer. After the choice...
python Topics covered: Using variables e Arithmetic operators Printing output Manipulating string objects . Generating random numbers . Getting user input NOTE: Each of your files must include a docstring at the top of the file containing your name, username, ID number and a description of the program. When solving these questions you must only use content covered in lectures 1 to 6 Submit the files containing your exercises using the Assignment Dropbox https:/adb auckland.ac.nz/Homel QUESTION 1 (3 MARKS) A...
C++ Pr ogramming (CSC-115) Functions (pass by Reference) Programming project Using FUNCTIONS, write a C++ program that calculates the Area of a cirele, a square and a rectangle. Your program must prompt the user to select which area is to be calculated. Document your program. Apply the do while loop to repeat the program Apply the while loop for input validation. Apply the switch statements or the if/ else if statement to prompt the user for the area's selection. Based...
Write a C++ program to manage a Point of Sale System for a Supermarket. The main user is an employee at the Supermarket. Build Specifications (36 points) 1. The system should load a catalog of all items that the store sells. 2. A user can search the inventory: The user of the system can search the inventory by using the name of the item or by category. 3. A user can sell items once they are found. Or can sell...