Vladislav_Bezruk

OOP 8.2 lab

Oct 1st, 2021 (edited)
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.55 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Coord {
  6.     float x, y;
  7.    
  8.     public:
  9.         Coord() {}
  10.        
  11.         Coord(float a, float b) : x(a), y(b) {}
  12.        
  13.         friend Coord operator + (const Coord &a, const Coord &b);
  14.        
  15.         friend Coord operator + (const Coord &a, const float floatB);
  16.        
  17.         friend Coord operator - (const Coord &a, const Coord &b);
  18.        
  19.         friend Coord operator - (const Coord &a, const float floatB);
  20.        
  21.         bool operator == (const Coord &a) { return (this -> x == a.x && this -> y == a.y); }
  22.        
  23.         bool operator == (const float floatA) { return (this -> x == floatA && this -> y == floatA); }
  24.        
  25.         Coord& operator = (const Coord &a) {
  26.             x = a.x; y = a.y;
  27.             return *this;
  28.         }
  29.        
  30.         Coord& operator = (const float a) {
  31.             x = y = a;
  32.             return *this;
  33.         }
  34.        
  35.         void set(char c) {
  36.             cout << endl << "Enter x & y of coordinate " << c << ": ";
  37.             cin >> x >> y;
  38.             return;
  39.         }
  40.        
  41.         void get(char c) {
  42.             cout << endl << "x & y of coordinate " << c << ": " << x << " " << y << endl;
  43.             return;
  44.         }
  45. };
  46.  
  47. int main() {
  48.    
  49.     Coord a, b, c;
  50.     float x;
  51.    
  52.     a.set('a');
  53.     b.set('b');
  54.    
  55.     cout << "Enter number x: ";
  56.     cin >> x;
  57.    
  58.     cout << endl << "==== operator + ====" << endl;
  59.     a.get('a');
  60.     b.get('b');
  61.     cout << endl << "x = " << x << endl;
  62.     c = a + b;
  63.     cout << endl << "c = a + b" << endl;
  64.     c.get('c');
  65.     c = a + x;
  66.     cout << endl << "c = a + x" << endl;
  67.     c.get('c');
  68.    
  69.     //
  70.    
  71.     cout << endl << "==== operator - ====" << endl;
  72.     a.get('a');
  73.     b.get('b');
  74.     cout << endl << "x = " << x << endl;
  75.     c = a - b;
  76.     cout << endl << "c = a - b" << endl;
  77.     c.get('c');
  78.     c = a - x;
  79.     cout << endl << "c = a - x" << endl;
  80.     c.get('c');
  81.    
  82.     //
  83.    
  84.     cout << endl << "==== operator == ====" << endl;
  85.     a.get('a');
  86.     b.get('b');
  87.     cout << endl << "x = " << x << endl;
  88.     c = a - b;
  89.     cout << endl << "a == b = " << (a == b) << endl;
  90.     cout << endl << "a == x = " << (a == x) << endl;
  91.    
  92.     //
  93.    
  94.     cout << endl << "==== operator = ====" << endl;
  95.     a.get('a');
  96.     cout << endl << "x = " << x << endl;
  97.     c = a;
  98.     cout << endl << "c = a" << endl;
  99.     c.get('c');
  100.     c = x;
  101.     cout << endl << "c = x" << endl;
  102.     c.get('c');
  103.    
  104.     return 0;
  105. }
  106.  
  107. Coord operator + (const Coord &a, const Coord &b) {
  108.     Coord c(a.x + b.x, a.y + b.y);
  109.     return c;
  110. }
  111.  
  112. Coord operator + (const Coord &a, const float floatB) {
  113.     Coord c(a.x + floatB, a.y + floatB);
  114.     return c;
  115. }
  116.  
  117. Coord operator - (const Coord &a, const Coord &b) {
  118.     Coord c(a.x - b.x, a.y - b.y);
  119.     return c;
  120. }
  121.  
  122. Coord operator - (const Coord &a, const float floatB) {
  123.     Coord c(a.x - floatB, a.y - floatB);
  124.     return c;
  125. }
Add Comment
Please, Sign In to add comment