westron

class

Mar 22nd, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.21 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Drob
  5. {
  6. public:
  7.     int ch, zn;
  8.    
  9.     Drob(int ch, int zn)
  10.     {
  11.         // constructor
  12.         this->ch = ch;
  13.         this->zn = zn;
  14.     }
  15.    
  16.     ~Drob()
  17.     {
  18.         // destructor
  19.     }
  20.    
  21.     const Drob operator+(const Drob &v) const;
  22.     const Drob& operator+=(const Drob &v);
  23.     bool operator==(const Drob &v);
  24.    
  25.     friend istream& operator>>(istream& stream, Drob &v);
  26.     friend ostream& operator<<(ostream& stream, Drob &v);
  27. };
  28.  
  29. const Drob Drob::operator+(const Drob &v) const
  30. {
  31.     Drob Sum;
  32.     Sum.ch = this->ch + v.ch;
  33.     Sum.zn = this->zn + v.zn;
  34.     return Sum;
  35. }
  36.  
  37. const Drob& Drob::operator+=(const Drob &v)
  38. {
  39.     ch += v.ch;
  40.     zn += v.zn;
  41.     return *this;
  42. }
  43.  
  44. bool Drob::operator==(const Drob &v)
  45. {
  46.     if(this->ch == v.ch && this->zn == v.zn)
  47.         return true;
  48.     return false;
  49. }
  50.  
  51. istream& operator>>(istream& stream, Drob &v)
  52. {
  53.     stream >> v.ch >> v.zn;
  54.     return stream;
  55. }
  56.  
  57. ostream& operator<<(ostream& stream, Drob &v)
  58. {
  59.     stream << "ch = " << v.ch << ", zn = " << v.zn;
  60.     return stream;
  61. }
  62.  
  63. void main()
  64. {
  65.     SetConsoleCP(1251);
  66.     SetConsoleOutputCP(1251);
  67.     Drob d1, d2;
  68.     cin >> d1 >> d2;
  69.     cout << d1 << endl << d2;
  70.     d1 += d2;
  71.     cout << d1 << endl << d2;
  72.     cout << "\n\n";
  73.     system("pause");
  74. }
Add Comment
Please, Sign In to add comment