How does C++ calculate the area of circles, rectangles and triangles
This article mainly introduces "how C++ calculates the area of circles, rectangles and triangles". In daily operation, I believe many people have doubts about how C++ calculates the areas of circles, rectangles and triangles. The editor consulted all kinds of data and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "how C++ calculates the area of circles, rectangles and triangles". Next, please follow the editor to study!
Topic description
Using polymorphism to write a program, declare the abstract base class Shape, from which three derived classes are derived: Circle (circle), Rectangle (rectangle) and Triangle (triangle). A function printArea () is used to output the area of the above three respectively (the result retains two decimal places). The data of the three graphics are given when defining the object.
Input
The radius of a circle, the side of a rectangle, the base and height of a triangle.
Output
The area of a circle
The area of a rectangle
The area of a triangle
Note: there is a carriage return after each line
Sample input
12.6 4.5 8.4 4.5 8.4
Sample output
Area of circle=498.76
Area of rectangle=37.80
Area of triangle=18.90
Code implementation # include#include # define PI 3.1415926using namespace std;class Shape {public: virtual double printArea () = 0;}; class Circle:public Shape {private: double r; public: Circle (double x) {rystx;} virtual double printArea () {return PI*r*r;}}; class Rectangle:public Shape {private: double wm m h Public: Rectangle (double Xrem double y) {wimpy;} virtual double printArea () {return wigh;}}; class Triangle:public Shape {private: double wrech; public: Triangle (double xrem double y) {wimpy } virtual double printArea () {return wreckhpact 2;}}; double printArea (Shape & x) {return x.printArea ();} int main () {double a > > b > > c > > d > > e; Circle cir (a); Rectangle rec (bMagee c); Triangle tri (dMagne); cout