Programming a Point Class

I need to program a class Point that does a few things. Anyone mind helping with where I need to go next?

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.

Here are my code files so far:
http://pastebin.com/sRsR3P8s
http://pastebin.com/U8SJFBaR
http://pastebin.com/CzuKBj9U

File list is as follows for those links:

main.cpp
Point.cpp
Point.h
I really like the way you have used using to specify only the items you actually use rather than bringing the whole namespace into scope with the using namespace statement.

One thing, though. In your header files you should always qualify your symbols with the namespace rather than the using directive. That's good in the .cpp files and the main file, but should not appear in header files.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef POINT_H
#define POINT_H

#include <iostream>

class Point
{
	friend std::istream &operator>>( std::istream &, Point & );
	friend std::ostream &operator<<( std::ostream &, const Point & );
private:
	int xCoordinate;
	int yCoordinate;
};

#endif 


I'm not sure why you put setw(1) in your extraction operator? It seems to work fine without it..

The only requirement left to implement is not outputting if your object was not correctly read from the input stream.

I can only think you are going to need to record the state of the Point in the object itself so as to know if the input was valid or not.

To that effect you should probably be using something other than ignore() to skip the inputting of the '(', ')' and ',' characters. At the moment I can enter X4=7& as input and it works the same as (4,7).

Topic archived. No new replies allowed.