Advertisement
35657

Untitled

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