35657

Untitled

Dec 9th, 2023
708
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 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.     Shape(string color) : color_(color) {}
  13.  
  14.     void SetColor(string color) {
  15.         color_ = color;
  16.     }
  17.  
  18.     string GetColor() const {
  19.         return color_;
  20.     }
  21.  
  22. private:
  23.     string color_;
  24. };
  25.  
  26. class Rectangle : public Shape {
  27. public:
  28.     Rectangle(string color, double a, double b) : Shape(color), a_(a), b_(b) {}
  29.  
  30.     double GetArea() const {
  31.         return a_ * b_;
  32.     }
  33.  
  34. private:
  35.     double a_;
  36.     double b_;
  37. };
  38.  
  39. class Triangle : public Shape {
  40.  
  41. public:
  42.     Triangle(string color, double a, double b, double c) : Shape(color), a_(a), b_(b), c_(c) {}
  43.  
  44.     double GetArea() const {
  45.         double p = (a_ + b_ + c_) / 2;
  46.         return sqrt(p * (p - a_) * (p - b_) * (p - c_));
  47.     }
  48.  
  49. private:
  50.     double a_;
  51.     double b_;
  52.     double c_;
  53. };
  54.  
  55. int main() {
  56.     Rectangle rec("black", 4, 5);
  57.     Triangle tr("white", 3, 4, 3);
  58.  
  59.     cout << rec.GetArea() << endl;
  60.     cout << tr.GetArea() << endl;
  61.  
  62.     cout << rec.GetColor() << endl;
  63.     rec.SetColor("white");
  64.     cout << rec.GetColor() << endl;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment