// Classes have methods and data. The data is nearly always private. // A class really should have some public methods to be useful, // (say, a constructor) but private methods, used internally, are also common. #include using namespace std; class Point { public: Point(int a, int b) { x = a; y = b; } // constructor (alias ctor) int get_x() const { return x; } // accessor int get_y() const { return y; } // accessor void go_right() { x++; } // mutator private: int x, y; // Hands off! }; // Make << work for a Point. ostream& operator<<(ostream &out, const Point &p) { return out << p.get_x() << ',' << p.get_y(); // Hey! How can we return an output statement!? } int main() { Point p(19,57); // Parameters to ctor // p.x = 5; // Sorry, it’s private! cout << p << '\n'; // invoke overloaded <<, as defined above p.go_right(); cout << p << '\n'; return 0; // dtor (where is it?) called automatically }