Write a program that accomplishes each of the following:
a) Create a user-defined class Point that contains the private integer data members xCoordinate and yCoordinate and declares stream insertion and stream extraction overloaded operator functions as friends of the class.
b) Define the stream insertion and stream extraction operator functions. The stream extraction operator function should determine whether the data entered is valid, and, if not, it should set the failbit to indicate improper input. The stream insertion operator should not be able to display the point after an input error occurred.
c) Write a main function that tests input and output of user-defined class Point, using the overloaded stream extraction and stream insertion operators.
```
#ifndef POINT_H
#define POINT_H
#include
using namespace std;
class Point
{
// overloaded input and output operators
friend ostream &operator<<( ostream&, const Point& );
friend istream &operator>>( istream&, Point& );
private:
int xCoordinate; // x-coordinate of point pair
int yCoordinate; // y-coordinate of point pair
}; // end class Point
#endif
```
```
// Member function definition of class Point.
#include
#include "Point.h"
using namespace std;
// overloaded output (<<) operator
ostream& operator<<( ostream& out, const Point& p )
{
out << "(" << p.xCoordinate << ", " << p.yCoordinate << ")\n";
return out; // return ostream reference
} // end overloaded output (<<) operator
// overloaded input (>>) operator
istream& operator>>( istream& in, Point& p )
{
// validate first character entered and ignore if valid
if ( in.peek() != '(' )
in.clear( ios::failbit ); // set failbit if invalid
else
{
in.ignore(); // skip (
in >> p.xCoordinate; // next character is x-coordinate
// validate third character and skip if valid
if ( in.peek() != ',' )
in.clear( ios::failbit ); // set failbit if invalid
else
{
in.ignore(); // skip ,
// validate fourth character and skip if valid
if ( in.peek() != ' ' )
in.clear( ios::failbit ); // set failbit if invalid
else
{
in.ignore(); // skip space
in >> p.yCoordinate; // next character is y-coordinate
// validate last character and skip if valid
if ( in.peek() != ')' )
in.clear( ios::failbit ); // set failbit if invalid
else
in.ignore(); // skip )
} // end else
} // end else
} // end else
return in; // return istream reference
} // end overloaded input (>>) operator
```
```
// Point test program.
#include
#include "Point.h"
using namespace std;
int main()
{
Point pt; // create point object
// ask user to enter point
cout << "Enter a point in the form (x, y):\n";
cin >> pt; // store user entered point
if ( !cin.fail() ) // validate input
cout << "Point entered was: " << pt << endl; // display point
else
cout << "\nInvalid data\n"; // tell user invalid data was entered
} // end main
```
Enter a point in the form (x, y):
(7, 8)
Point entered was: (7, 8)
You might also like to view...
The ____ keyword tells the computer to pass the variable's address rather than a copy of its contents.
A. ByRef B. ByVal C. ByValue D. ByLoc
The SelectedItem property of a ComboBox returns a null string if the user entered a value into the Text property of the ComboBox.
Answer the following statement true (T) or false (F)
List the categories of choices for Active Directory installation when a domain already exists.
What will be an ideal response?
You can move to the previous or next sheet by pressing the ____ keys.
A. Alt + Page Up or Alt + Page Down B. Ctrl + Page Up or Ctrl + Page Down C. Tab + Page Up or Tab + Page Down D. F4 + Page Up or F4 + Page Down