Arkanium77

Orb

Jun 26th, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.97 KB | None | 0 0
  1. #include <cmath>
  2. #include <cstdlib>
  3. #include <string>
  4. #include <iostream>
  5. #include <cmath>
  6.  
  7. using namespace std;
  8.  
  9. /*
  10.  *
  11.  * “В C++ всего 2 вещи получились не так: начальный замысел и реализация.”
  12.  *                                                              — Бертран Мейер
  13.  *
  14.  */
  15.  
  16. /** Класс исключений
  17.  */
  18. class NegativeException {
  19.     string exc;
  20.  
  21.     friend ostream& operator<<(ostream& os, const NegativeException& vi) {
  22.         os << "исключение " << vi.exc;
  23.         return os;
  24.     }
  25. public:
  26.  
  27.     NegativeException() {
  28.         this->exc = "NegativeException";
  29.     }
  30. };
  31.  
  32. class Orb {
  33. private:
  34.    
  35.     double r;
  36.  
  37. public:
  38.  
  39.     Orb(double r = 1) {
  40.         if (r <= 0)throw NegativeException();
  41.         this->r = r;
  42.     };
  43.  
  44.     Orb(const Orb &orig) {
  45.         this->r = orig.r;
  46.     };
  47.    
  48.     /**
  49.      * Переопределённый оператор вывода iostream
  50.      */
  51.     friend ostream& operator<<(ostream& os, const Orb& at) {
  52.         os << "r=" << at.r;
  53.         return os;
  54.     };
  55.    
  56.     /**
  57.      * Деструктор. Не нужен пока нет указателей
  58.      */
  59.     ~Orb() {
  60.     };
  61.  
  62.     /**
  63.      * Площадь
  64.      * @return площадь
  65.      */
  66.     double s() {
  67.         return (4 * M_PI * r * r);
  68.     };
  69.     /**
  70.      * Объём
  71.      * @return объём
  72.      */
  73.     double v() {
  74.         return 4 / 3 * (M_PI * r * r * r);
  75.     };
  76.    
  77.     double getR() {
  78.         return this->r;
  79.     };
  80.     //переопределённые операторы
  81.     bool operator==(Orb a) {
  82.         return this->equals(a);
  83.     };
  84.  
  85.     bool operator!=(Orb a) {
  86.         return !(this->equals(a));
  87.     };
  88.    
  89.     void operator=(Orb b) {
  90.         this->r = b.r;
  91.     };
  92.    
  93. protected:
  94.    
  95.     bool equals(const Orb &a) {
  96.         if (a.r == this->r) return true;
  97.         return false;
  98.     };
  99. };
Advertisement
Add Comment
Please, Sign In to add comment