fork download
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4. //4.1.
  5. class Figure{
  6. protected:
  7. double x, y;
  8. public:
  9. //4.2.
  10. static int count;
  11. void setDim(double i, double j){
  12. x=i;
  13. y=j;
  14. }
  15. virtual void showArea() = 0;
  16. virtual void showPerimeter() = 0;
  17. };
  18.  
  19. //4.2.
  20. int Figure::count = 0;
  21.  
  22. class Triangle: public Figure{
  23. public:
  24. void showArea(){
  25. cout << "Triangle area: " << 0.5 *x*y<< endl;
  26. }
  27. void showPerimeter(){
  28. double z = sqrt(x*x + y*y);
  29. cout << "Triangle perimeter: " << x+y+z << endl;
  30. }
  31. };
  32.  
  33. class Rectangle: public Figure{
  34. public:
  35. void showArea(){
  36. cout << "Rectangle area: " << x*y<< endl;
  37. }
  38. void showPerimeter(){
  39. cout << "Rectangle perimeter: " << 2*(x+y) << endl;
  40. }
  41. };
  42.  
  43. class Circle: public Figure{
  44. public:
  45. void showArea(){
  46. cout << "Circle area: " << 3.14 *x*y<< endl;
  47. }
  48. void showPerimeter(){
  49. cout << "Circle perimeter: " << 2* 3.14 * x << endl;
  50. }
  51. };
  52. int main() {
  53. Figure *pFigure;
  54. Triangle triangle;
  55. Rectangle rectangle;
  56. Circle circle;
  57.  
  58. pFigure = &triangle;
  59. pFigure->setDim(10.0, 5.0);
  60. pFigure->showArea();
  61. pFigure->showPerimeter();
  62.  
  63. pFigure = &rectangle;
  64. pFigure->setDim(10.0, 5.0);
  65. pFigure->showArea();
  66. pFigure->showPerimeter();
  67.  
  68. pFigure = &circle;
  69. pFigure->setDim(5.0, 11.0);
  70. pFigure->showArea();
  71. pFigure->showPerimeter();
  72. return 0;
  73. }
Success #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
Triangle area: 25
Triangle perimeter: 26.1803
Rectangle area: 50
Rectangle perimeter: 30
Circle area: 172.7
Circle perimeter: 31.4