Work on Static methods OBJECTIVES -continue to practice to write the .docx
1. Work on Static methods OBJECTIVES -continue to practice to
write the code for the data type class -Learn what is static
members in data type class and how to write static methods -
Learn how to access static members of data type class from
main() Provide the application that helps users to calculate the
area of the following list of shapes. Square Rectangle Triangle
Circle Trapezoid Rhombus Kite Parallelogram For each shape,
you can ask for all necessary information of the selected shape
to calculate its area from the keyboard. The result should be
displayed in the following format, for example with selected
shape is Rectangle -Manage the menu with the do., while loop
to redisplayed the menu after finishing one shape -How to write
the switch statement -How to format the output in columns and
with 2 decimal digits -How to create the static method in data
type class -How to access static method of data type class from
main() -The formula to calculate the area of all above shapes -
You have to provide the pseudo code of main() at the top of
driver class or in the word document with party -Create a
project named SU2016LAB6_PART2_YourLastName then add
two classes in -Class SU2016LAB6_ShapeArea_yourLastName
that includes all the static methods that receive the information
input from main(), calculate the area of required shapes and
return its area. -Driver class
SU2016LAB6_AccessStaticMethod_yourLastName: this class
include main() where you can provide the menu to allow users
to select the shapes to calculate the area. For each shape, ask
for and read the necessary information to calculate the area
from the keyboard, then call the static method of class
SU2016LAB6_ShapeArea_yourLastName to pass the
information in to get the area. After that, display the result as
requested format
2. Solution
#include <iostream>
#include <conio.h>
#include <math.h>
using namespace std;
class Shape{ //shape class
public:
virtual double area();
};
class Rectangle : public Shape {//rectangle class
private:
double width, length; //sides
public:
Rectangle(double width, double length) {
this->width = width;
this->length = length;
}
double area() {
// A = w * l
return width * length;
}
4. class Parallelogram:public Shape {//parallelogram class
private:double width, length; //sides
public:
Parallelogram(double width, double length) {
this->width = width;
this->length = length;
}
double area() {
return width * length;
}
};
class Circle:public Shape {//circle class
private: double radius;
public:
Circle(double radius) {
this->radius = radius;
}
double area() {
// A = p r^2
return 22/7 * radius*radius;
}
};
class Square:public Shape{//square class
private: double side;
public:
5. Square(double side) {
this->side = side;
}
double area() {
return side*side;
}
};
class Triangle:public Shape{//triangle class
private: double a, b, c; // sides
public:
Triangle(double a, double b, double c) {
this->a = a;
this->b = b;
this->c = c;
}
double area() {
// Heron's formula:
// A = SquareRoot(s * (s - a) * (s - b) * (s - c))
// where s = (a + b + c) / 2, or 1/2 of the perimeter of the
triangle
double s = (a + b + c) / 2;
return sqrt(s * (s - a) * (s - b) * (s - c));
}
};
class Trapezoid:public Shape{//trapezoid class
6. private: double a, b, c; // sides
public:
Trapezoid(double a, double b, double c) {
this->a = a;
this->b = b;
this->c = c;
}
double area() {
return 1/2*(a+b)*c;
}
};
int main() {
// Square test
double side;
cout<<"Enter the side"<<endl;
cin>>side;
Shape square = Square(side);
cout<<"Shape : Square "<<"side = "<<side<<" Area = "
<< square.area()<< " ";
// Rectangle test
double width,length;
cout<<"Enter the width and length"<<endl;
cin>>width>>length;
7. Shape rectangle = Rectangle(width, length);
cout<<"Shape : Rectangle "<< "Width = "<<width<<"
Length = "<< length
<< " Area = " << rectangle.area()<< " ";
// Triangle test
double a, b, c;
cout<<"enter the sides of th triangle"<<endl;
cin>>a>>b>>c;
Shape triangle = Triangle(a,b,c);
cout<<"Shape : Triangle " << "Side1 = "<<a <<" "<<
"Side2 = " << b<<" Side3 = "<< c
<< " Area = " << triangle.area()<< " ";
// Circle test
double radius;
cout<<"Enter the radius"<<endl;
cin>>radius;
Shape circle = Circle(radius);
cout<<"Shape : Circle " << "Radius = "<<radius << "
Area = " << circle.area()<< " ";
// Trapezoid test
double a1, b1, c1;
cout<<"enter the sides of th trapezoid"<<endl;
cin>>a1>>b1>>c1;