Arkanium77

Rectangle

Jun 26th, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.63 KB | None | 0 0
  1. #include <cmath>
  2. #include <cstdlib>
  3. #include <string>
  4. #include <iostream>
  5. #include <cmath>
  6.  
  7.  
  8. using namespace std;
  9.  
  10. /*
  11.  *
  12.  * — Существует ли объектно-ориентированный способ стать богатым?
  13.  * — Да, наследование.
  14.  *
  15.  */
  16.  
  17. class NegativeException {
  18.     string exc;
  19.  
  20.     friend ostream& operator<<(ostream& os, const NegativeException& vi) {
  21.         os << "исключение " << vi.exc;
  22.         return os;
  23.     }
  24. public:
  25.  
  26.     NegativeException() {
  27.         this->exc = "NegativeException";
  28.     }
  29. };
  30.  
  31. class Rectangle {
  32.     double a, b;
  33. public:
  34.    
  35.     Rectangle(double a, double b) {
  36.         if (a <= 0 || b <= 0) throw NegativeException();
  37.         this->a = a;
  38.         this->b = b;
  39.     }
  40.  
  41.     Rectangle(double a) {
  42.         if (a <= 0)throw NegativeException();
  43.         this->a = a;
  44.         this->b = a;
  45.     }
  46.  
  47.     Rectangle() {
  48.         this->a = 1;
  49.         this->b = 1;
  50.     }
  51.  
  52.     double s() {
  53.         return a*b;
  54.     }
  55.  
  56.     double p() {
  57.         return 2 * a + 2 * b;
  58.     }
  59.  
  60.     bool equals(Rectangle a) {
  61.         if ((this->a == a.a && this->b == a.b) || (this->a == a.b && this->b == a.a))return true;
  62.         return false;
  63.     }
  64.  
  65.     bool operator==(Rectangle a) {
  66.         return this->equals(a);
  67.     };
  68.  
  69.     bool operator!=(Rectangle a) {
  70.         return !(this->equals(a));
  71.     };
  72.  
  73.     void operator=(Rectangle b) {
  74.         this->a = b.a;
  75.         this->b = b.b;
  76.     };
  77.  
  78.     friend ostream& operator<<(ostream& os, const Rectangle& at) {
  79.         os << "a=" << at.a << ", b=" << at.b;
  80.         return os;
  81.     };
  82. };
Advertisement
Add Comment
Please, Sign In to add comment