const member functions in C++
Level 4242 keystrokes
Marks the getters of a Point class as const, which promises they do not modify the object, while move changes it; prints the coordinates 3, 4.
C++ code
1class Point { // Private coordinates2 int x, y;3public: // Constructor that initializes x and y4 Point(int x, int y) : x(x), y(y) {} // const after the parameter list: the function cannot modify members and can be called on const objects5 int getX() const { return x; }6 int getY() const { return y; } // Not const, because it changes x and y7 void move(int dx, int dy) { x += dx; y += dy; }8};9 // Create the point (3, 4)10Point p(3, 4);// Read the coordinates through the const getters: prints 3, 411cout << p.getX() << ", " << p.getY() << endl;Output
3, 4
Practice typing this code. Results are not recorded in the rankings.
Symbols in this code
The symbols you type in this C++ code and how often they appear.
(× 9)× 9;× 8<× 8,× 6{× 5}× 5:× 2.× 2"× 2