1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
#ifndef POINT_H
#define POINT_H
#include <iostream>
using namespace std;
class Point
{
public:
/**
* Requires: Nothing.
* Modifies: x, y.
* Effects: Default contructor. Sets point to origin (0,0).
*/
Point();
/**
* Requires: Nothing.
* Modifies: x, y.
* Effects: Constructs a point and sets x and y coordinates.
* Note: you will want to implement the private
* member function checkRange() before
* implementing this one
*/
Point(int xVal, int yVal);
/**
* Requires: Nothing.
* Modifies: x.
* Effects: Sets x coordinate.
*/
void setX(int xVal);
/**
* Requires: Nothing.
* Modifies: Nothing.
* Effects: Returns x coordinate.
*/
int getX();
/**
* Requires: Nothing.
* Modifies: y.
* Effects: Sets y coordinate.
*/
void setY(int yVal);
/**
* Requires: Nothing.
* Modifies: Nothing.
* Effects: Returns y coordinate.
*/
int getY();
/**
* Requires: ins is in good state.
* Modifies: ins, x, y.
* Effects: Reads point in form (x,y).
*/
void read(istream& ins);
/**
* Requires: outs is in good state.
* Modifies: outs.
* Effects: Writes point in form (x,y).
*/
void write(ostream& outs);
private:
int x;
int y;
/**
* Requires: nothing
* Modifies: nothing
* Effects:
* Effects: Returns val if val is in range [0,DIMENSION),
* otherwise returns the closest of 0 and DIMENSION - 1.
*/
int checkRange(int val);
/**
* Overloading >> and << for reading a Point from streams.
* Example on how to use these:
* Point pt;
* cout << "Please enter a point using format (x,y) : ";
* cin >> pt;
* cout << "\nthe point you just entered is: ";
* cout << pt << endl;
*/
istream& operator >> (istream& ins, Point& pt);
ostream& operator << (ostream& outs, Point pt);
#endif
CPP FILE WITH FUNCTIONS I WROTE:
#include "Point.h"
// for the declaration of DIMENSION
#include "utility.h"
// TODO: implement two constructors, setX, getX, setY, getY, read, write, checkRange.
Point::Point() { // Default constructor
x = 0;
y = 0;
}
void Point::setX(int xVal) {
x = xVal;
}
int Point::getX() {
return x;
}
void Point::setY(int yVal) {
y = yVal;
}
int Point::getY() {
return y;
}
void Point::read(istream& ins) {
char junk;
char junk2;
char junk3;
ins >> junk >> x >> junk2 >> y >> junk3;
}
void Point::write(ostream& outs) {
outs << "(" << x << "," << y << ")";
}
int Point::checkRange(int val){
if ((val >= 0) && (val < DIMENSION)) {
return val;
}
else {
if ((val - (DIMENSION - 1)) > val) {
return 0;
}
else {
return (DIMENSION - 1);
}
}
}
Point::Point(int xVal, int yVal) { // Second constructor
x = xVal;
y = yVal;
}
// Your code goes above this line.
// Don't change the implementations below!
istream& operator >> (istream& ins, Point& pt)
{
pt.read(ins);
return ins;
}
ostream& operator<< (ostream& outs, Point pt)
{
pt.write(outs);
return outs;
}
|