Advertisement
Guest User

Point Class

a guest
Nov 19th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.74 KB | None | 0 0
  1. =======
  2. Point.h
  3. =======
  4. #pragma once
  5. #include <iostream>
  6.  
  7. class Point {
  8. private:
  9.     int xCoordinate;
  10.     int yCoordinate;
  11. public:
  12.     Point();
  13.     ~Point();
  14.  
  15.     friend bool operator >>(std::istream &, Point &);
  16.     friend void operator <<(std::ostream &, Point &);
  17. };
  18.  
  19. ~~~~~~~~~~
  20. =========
  21. Point.cpp
  22. =========
  23. #include "Point.h"
  24.  
  25. Point::Point() {
  26.  
  27. }
  28.  
  29. Point::~Point() {
  30.  
  31. }
  32.  
  33. ~~~~~~~~~~~~~~~~
  34. ========
  35. main.cpp
  36. ========
  37. #include <iostream>
  38. #include "Point.h"
  39.  
  40. bool operator >>(std::istream &input, Point &p);
  41. void operator <<(std::ostream &output, Point &p);
  42.  
  43.  
  44. int main() {
  45.     Point point;
  46.     bool result = std::cin >> point; //This will prompt the user to input the coordinates into the class
  47.     if (result = true) //The flag will be switched to true if input for both xCoordinate and yCoordinate are fulfilled
  48.         std::cout << point;
  49.     return 0;
  50. }
  51.  
  52. bool operator >>(std::istream &input, Point &p) {
  53.     std::cout << "Please enter the x-coordinate: ";
  54.     input >> p.xCoordinate;
  55.  
  56.     if (input.fail()) { //Condition to check if input is of type int
  57.         std::cout << "You have entered the coordinate incorrectly" << std::endl;
  58.         input.setstate(std::ios::failbit); //This will set the failbit if the number is inputed incorrectly
  59.         return false;
  60.     }
  61.     else {
  62.         std::cout << "Please enter the y-coordinate: ";
  63.         input >> p.yCoordinate;
  64.         if (input.fail()) { //Condition to check if input is of type int
  65.             std::cout << "You have entered the coordinate incorrectly" << std::endl;
  66.             input.setstate(std::ios::failbit); //This will set the failbit if the number is inputed incorrectly
  67.             return false;
  68.         }
  69.     }
  70.     return true;
  71. }
  72.  
  73. void operator <<(std::ostream &output, Point &p) {
  74.     output << '(' << p.xCoordinate << ',' << p.yCoordinate << ')' << std::endl;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement