Advertisement
Guest User

Untitled

a guest
Apr 25th, 2015
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.37 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Rectangle
  5. {
  6. public:
  7.     Rectangle(): xa(0), ya(0), xb(0), yb(0){}
  8.     Rectangle(int _xa, int _ya, int _xb, int _yb): xa(_xa), ya(_ya), xb(_xb), yb(_yb) {}
  9.  
  10.     friend istream& operator >> (istream& in, Rectangle& r)
  11.     {
  12.         in >> r.xa >> r.ya >> r.xb >> r.yb;
  13.         return in;
  14.     }
  15.  
  16.     friend ostream& operator << (ostream& out, Rectangle const& r)
  17.     {
  18.         out << r.xa << " " << r.ya << " " << r.xb << " " << r.yb;
  19.         return out;
  20.     }
  21.  
  22.     Rectangle operator +  (const Rectangle &r) const;
  23.     Rectangle operator -  (const Rectangle &r) const;
  24.     Rectangle operator *  (const Rectangle &r) const;
  25.  
  26.     friend Rectangle operator + (const Rectangle &r, int a);
  27.     friend Rectangle operator - (const Rectangle &r, int a);
  28.     friend Rectangle operator * (const Rectangle &r, int a);
  29.  
  30.     friend Rectangle operator * (int a, const Rectangle &r);
  31.     friend Rectangle operator / (const Rectangle& r, int a);
  32.  
  33. private:
  34.     int xa, ya, xb, yb;
  35. };
  36.  
  37. Rectangle Rectangle::operator +  (const Rectangle &r) const
  38. {
  39.     Rectangle r1(*this);
  40.     r1.xa += r.xa;
  41.     r1.ya += r.ya;
  42.     r1.xb += r.xb;
  43.     r1.yb += r.yb;
  44.     return r1;
  45. }
  46.  
  47. Rectangle Rectangle::operator -  (const Rectangle &r) const
  48. {
  49.     Rectangle r1(*this);
  50.     r1.xa -= r.xa;
  51.     r1.ya -= r.ya;
  52.     r1.xb -= r.xb;
  53.     r1.yb -= r.yb;
  54.     return r1;
  55. }
  56.  
  57. Rectangle Rectangle::operator *  (const Rectangle &r) const
  58. {
  59.     Rectangle r1(*this);
  60.     r1.xa *= r.xa;
  61.     r1.ya *= r.ya;
  62.     r1.xb *= r.xb;
  63.     r1.yb *= r.yb;
  64.     return r1;
  65. }
  66.  
  67. Rectangle operator + (const Rectangle &r, int a)
  68. {
  69.     return Rectangle(r.xa + a, r.ya + a, r.xb + a, r.yb + a);
  70. }
  71.  
  72. Rectangle operator - (const Rectangle &r, int a)
  73. {
  74.     return Rectangle(r.xa - a, r.ya - a, r.xb - a, r.yb - a);
  75. }
  76.  
  77. Rectangle operator * (const Rectangle &r, int a)
  78. {
  79.     return Rectangle(a*r.xa, a*r.ya, a*r.xb, a*r.yb);
  80. }
  81.  
  82. Rectangle operator * (int a, const Rectangle &r)
  83. {
  84.     return Rectangle(a*r.xa, a*r.ya, a*r.xb, a*r.yb);
  85. }
  86.  
  87. Rectangle operator / (const Rectangle& r, int a)
  88. {
  89.     return Rectangle(r.xa/a, r.ya/a, r.xb/a, r.yb/a);
  90. }
  91.  
  92. int main()
  93. {
  94.     Rectangle a, b, c;
  95.     cout << "Enter coordinates: ";
  96.     cin >>  a;
  97.     cin >> b;
  98.     c = a + b;
  99.     cout << c << endl;
  100.     c = a - b;
  101.     cout << c << endl;
  102.     c = a * b;
  103.     cout << c << endl;
  104.     c = a + 3;
  105.     cout << c << endl;
  106.     c = a - 3;
  107.     cout << c << endl;
  108.     c = a * 3;
  109.     cout << c << endl;
  110.     c = a / 2;
  111.     cout << c << endl;
  112.  
  113.     system ("pause");
  114.     return 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement