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. void setDim(double i, double j){
  10. x=i;
  11. y=j;
  12. }
  13. virtual void showArea() = 0;
  14. virtual void showPerimeter() = 0;
  15. };
  16.  
  17. class Triangle: public Figure{
  18. public:
  19. void showArea(){
  20. cout << "Triangle area: " << 0.5 *x*y<< endl;
  21. }
  22. void showPerimeter(){
  23. double z = sqrt(x*x + y*y);
  24. cout << "Triangle perimeter: " << x+y+z << endl;
  25. }
  26. };
  27.  
  28. class Rectangle: public Figure{
  29. public:
  30. void showArea(){
  31. cout << "Rectangle area: " << x*y<< endl;
  32. }
  33. void showPerimeter(){
  34. cout << "Rectangle perimeter: " << 2*(x+y) << endl;
  35. }
  36. };
  37.  
  38. class Circle: public Figure{
  39. public:
  40. void showArea(){
  41. cout << "Circle area: " << 3.14 *x*y<< endl;
  42. }
  43. void showPerimeter(){
  44. cout << "Circle perimeter: " << 2* 3.14 * x << endl;
  45. }
  46. };
  47. int main() {
  48. Figure *pFigure;
  49. Triangle triangle;
  50. Rectangle rectangle;
  51. Circle circle;
  52.  
  53. pFigure = &triangle;
  54. pFigure->setDim(10.0, 5.0);
  55. pFigure->showArea();
  56. pFigure->showPerimeter();
  57.  
  58. pFigure = &rectangle;
  59. pFigure->setDim(10.0, 5.0);
  60. pFigure->showArea();
  61. pFigure->showPerimeter();
  62.  
  63. pFigure = &circle;
  64. pFigure->setDim(5.0);
  65. pFigure->showArea();
  66. pFigure->showPerimeter();
  67. return 0;
  68. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Triangle area: 25
Triangle perimeter: 26.1803
 Rectangle area: 50
Rectangle perimeter: 30
Circle area: 157
Circle perimeter: 62.8