Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.61 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. class Example {
  4.     int x;
  5.     int y;
  6. public:
  7.     Example(int x, int y): x(x), y(y) {
  8.  
  9.     }
  10.     int getX() {
  11.         return x;
  12.     }
  13.     int getY() {
  14.         return y;
  15.     }
  16.     Example operator+(const Example &a) const{
  17.         return Example(this->x + a.x, this->y + a.y);
  18.     }
  19.     Example operator+(int i) {
  20.         return Example(this->x + i, this->y + i);
  21.     }
  22.     Example operator*(const Example& a) const {
  23.         return Example(this->x * a.x, this->y * a.y);
  24.     }
  25.     Example operator*(int i) {
  26.         return Example(this->x * i, this->y * i);
  27.     }
  28.     Example& operator=(const Example &a) {
  29.         if (this == &a) {
  30.             return *this;
  31.         }
  32.         this->x = a.x;
  33.         this->y = a.y;
  34.         std::cout << this << "\n";
  35.         return *this;
  36.     }
  37.     Example& operator++() {
  38.         this->x++;
  39.         this->y++;
  40.         return *this;
  41.     }
  42.     Example operator++(int) {
  43.         Example temp(this->x, this->y);
  44.         this->x++;
  45.         this->y++;
  46.         return temp;
  47.     }
  48.     bool operator==(Example &p) {
  49.         return (x == p.x && y == p.y);
  50.     }
  51.     bool operator==(int i) {
  52.         return (x == i || y == i);
  53.     }
  54.     friend Example operator+(int i, Example& a);
  55. };
  56.  
  57. Example operator+(int i, Example& a) {
  58.     return Example(a.x + i, a.y + i);
  59. }
  60.  
  61. void findThese(Example objects[], int elements, int goal) {
  62.     for (int i = 0; i < elements; i++) {
  63.         if (objects[i] == goal) {
  64.             std::cout << "Object[" << i << "] = Correct!\n";
  65.         }
  66.         else {
  67.             std::cout << "Object[" << i << "] = Wrong!\n";
  68.         }
  69.     }
  70. }
  71.  
  72. int main()
  73. {
  74.     Example a(10, 40);
  75.     Example b(20, 10);
  76.     Example sum(0, 0);
  77.     sum = (a + b);
  78.     std::cout << sum.getX() << " " << sum.getY() << "\n";
  79.     Example objects[5] = { {1,2},{3,4},{4,3},{2,1},{5,2} };
  80.     findThese(objects, 5, 2);
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement