Advertisement
Josif_tepe

Untitled

Sep 12th, 2021
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. class Point {
  5. private:
  6.     mutable int x;// we can change the value inside constant functions
  7.     int y;
  8. public:
  9.     Point(int _x = 0, int _y = 0) {
  10.         x = _x;
  11.         y = _y;
  12.     }
  13.     Point(const Point &tmp) {
  14.         x = tmp.x;
  15.         y = tmp.y;
  16.     }
  17.      int get_x() const { // this means we cannot change any variable in the function
  18.          x++; // since it's mutable, the value can be chagned even though it is a constant function
  19.         return x;
  20.     }
  21.     int get_y() const {
  22.         return y;
  23.     }
  24. };
  25. int main()
  26. {
  27.     Point p(10, 20);
  28.     cout << p.get_x() << endl;
  29.    
  30.     return 0;
  31. }
  32.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement