Advertisement
35657

Untitled

Aug 30th, 2024
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. #include <set>
  2. #include <string>
  3. #include <iostream>
  4. #include <numeric>
  5.  
  6. using namespace std;
  7.  
  8.  
  9. // фигура
  10. class Shape {
  11. public:
  12.  
  13.     string GetColor() const {
  14.         return color_;
  15.     }
  16.     void SetColor(string color) {
  17.         color_ = color;
  18.     }
  19.  
  20. private:
  21.     string color_ = "black";
  22. };
  23.  
  24.  
  25.  
  26. // прямоугольник
  27. class Rectangle : public Shape {
  28. public:
  29.  
  30.     Rectangle(double a, double b) : a_(a), b_(b) {}
  31.  
  32.     double GetArea() {
  33.         return a_ * b_;
  34.     }
  35.  
  36. private:
  37.     double a_;
  38.     double b_;
  39. };
  40.  
  41.  
  42. // треугольник
  43. class Triangle : public Shape {
  44. public:
  45.  
  46.     Triangle(double a, double b, double c) : a_(a), b_(b), c_(c) {}
  47.  
  48.     double GetArea() {
  49.         double p = (a_ + b_ + c_) / 2;
  50.         return sqrt(p * (p - a_) * (p - b_) * (p - c_));
  51.     }
  52.  
  53. private:
  54.     double a_;
  55.     double b_;
  56.     double c_;
  57. };
  58.  
  59. int main() {
  60.     Rectangle rec(4, 5);
  61.     Triangle tr(3, 4, 3);
  62.     cout << rec.GetArea() << endl;
  63.     cout << tr.GetArea() << endl;
  64.     cout << rec.GetColor() << endl;
  65.     rec.SetColor("white");
  66.     cout << rec.GetColor() << endl;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement