Did I Write the Functions Correctly?

We are given the header file with the RMEs and we have to write the functions for the class in the cpp file. I feel like I should be declaring x and y somewhere and I also feel like my read and write functions are wrong. Did I write those functions and did I write the constructors correctly?
GIVEN HEADER FILE WITH RMEs:

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;
}
Last edited on
closed account (E0p9LyTq)
Don't create duplicate posts:
http://www.cplusplus.com/forum/general/201569/

And PLEASE learn to use code tags. You've been asked nicely already, not using code tags makes it hard to read your code.
http://www.cplusplus.com/articles/jEywvCM9/
Sorry I didn't know how to delete the old post-I got it to compile and just wanted to ask strictly about the functions.

I thought I was using code tags-I clicked the button. Is it not working. I'm so so sorry, I'm trying to do this right
We are given the header file with the RMEs

What's an RME (Rolling My Eyes)?

I feel like I should be declaring x and y somewhere

You are. Lines 71,72.

I also feel like my read and write functions are wrong.

They look reasonable.

Line 82: Missing }; to terminate the class declaration

Line 117,125: Your getters and checkRange() should be const correct. I don't know if you're allowed to change the class declaration.

did I write the constructors correctly?

Yes.
Last edited on
Topic archived. No new replies allowed.