35657

Untitled

Jul 15th, 2023
897
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. #include <set>
  2. #include <string>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. class Shape {
  9. public:
  10.  
  11.     string GetColor() const {
  12.         return color_;
  13.     }
  14.     void SetColor(string color) {
  15.         color_ = color;
  16.     }
  17.  
  18. private:
  19.     string color_;
  20. };
  21.  
  22. class Rectangle : public Shape {
  23. public:
  24.     Rectangle(double a, double b) : a_(a), b_(b) {}
  25.  
  26.     double GetArea() {
  27.         return a_ * b_;
  28.     }
  29.  
  30. private:
  31.     double a_;
  32.     double b_;
  33. };
  34.  
  35. class Triangle : public Shape {
  36. public:
  37.     Triangle(double a, double b, double c) : a_(a), b_(b), c_(c) {}
  38.  
  39.     double GetArea() {
  40.         double p = (a_ + b_ + c_) / 2;
  41.         return sqrt(p * (p - a_) * (p - b_) * (p - c_));
  42.     }
  43.  
  44. private:
  45.     double a_;
  46.     double b_;
  47.     double c_;
  48. };
  49.  
  50.  
  51. int main() {
  52.     setlocale(LC_ALL, "ru");
  53.     Rectangle rec(4, 5);
  54.     Triangle tr(3, 4, 3);
  55.     rec.SetColor("white");
  56.     tr.SetColor("black");
  57.     cout << rec.GetColor() << " " << rec.GetArea() << endl;
  58.     cout << tr.GetColor() << " " << tr.GetArea() << endl;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment