点类定义和使用.cpp 482 B

123456789101112131415161718192021222324252627282930
  1. #include <iostream>
  2. class Point {
  3. private:
  4. int x;
  5. int y;
  6. public:
  7. void setxy(int x_val, int y_val) {
  8. x = x_val;
  9. y = y_val;
  10. }
  11. void displayxy() {
  12. std::cout << "(" << x << "," << y << ")\n";
  13. }
  14. };
  15. int main() {
  16. Point point1, point2;
  17. point1.setxy(3, 4);
  18. point2.setxy(5, 6);
  19. std::cout << "The first point is:";
  20. point1.displayxy();
  21. std::cout << "The second point is:";
  22. point2.displayxy();
  23. return 0;
  24. }