Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- =======
- Point.h
- =======
- #pragma once
- #include <iostream>
- class Point {
- private:
- int xCoordinate;
- int yCoordinate;
- public:
- Point();
- ~Point();
- friend bool operator >>(std::istream &, Point &);
- friend void operator <<(std::ostream &, Point &);
- };
- ~~~~~~~~~~
- =========
- Point.cpp
- =========
- #include "Point.h"
- Point::Point() {
- }
- Point::~Point() {
- }
- ~~~~~~~~~~~~~~~~
- ========
- main.cpp
- ========
- #include <iostream>
- #include "Point.h"
- bool operator >>(std::istream &input, Point &p);
- void operator <<(std::ostream &output, Point &p);
- int main() {
- Point point;
- bool result = std::cin >> point; //This will prompt the user to input the coordinates into the class
- if (result = true) //The flag will be switched to true if input for both xCoordinate and yCoordinate are fulfilled
- std::cout << point;
- return 0;
- }
- bool operator >>(std::istream &input, Point &p) {
- std::cout << "Please enter the x-coordinate: ";
- input >> p.xCoordinate;
- if (input.fail()) { //Condition to check if input is of type int
- std::cout << "You have entered the coordinate incorrectly" << std::endl;
- input.setstate(std::ios::failbit); //This will set the failbit if the number is inputed incorrectly
- return false;
- }
- else {
- std::cout << "Please enter the y-coordinate: ";
- input >> p.yCoordinate;
- if (input.fail()) { //Condition to check if input is of type int
- std::cout << "You have entered the coordinate incorrectly" << std::endl;
- input.setstate(std::ios::failbit); //This will set the failbit if the number is inputed incorrectly
- return false;
- }
- }
- return true;
- }
- void operator <<(std::ostream &output, Point &p) {
- output << '(' << p.xCoordinate << ',' << p.yCoordinate << ')' << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement