Class Lab

I was given this assignment by my teacher and am absolutely stumped, I have a basic grasp on what I need to do and am having troubles understanding what I am missing and what the error code is saying.

Lab as follows:
Create a class MyRectangle with attributes length and width, each of which defaults to 1. Provide member functions that calculate the perimeter and the area of the rectangle. Also, provide set and get functions for the length and width attributes. The set functions should verify that length and width are each floating-point numbers larger than 0.0 and less than 20.0.

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
// MyRectangle Lab

#include "stdafx.h"
#include "MyRectangle.h"
#include <iostream>
#include <SFML\Graphics.hpp>

using namespace std;
using namespace sf;

int main(){
	MyRectangle start;
	start.run();
	return 0;
}
[/end main.cpp/]

[MyRectangle.h]
#pragma once
#include "stdafx.h"
#include <iostream>
#include <SFML/Graphics.hpp>

using namespace std;
using namespace sf;

class MyRectangle{
public:
	MyRectangle();
	~MyRectangle();
	void run();
	int getX(float);
	int getY(float);
	void setValues(float, float);
	void tryAgain(float, float);
	double area(double fxy[2], double area);
	double prim(double fxy[2], double prim);
	double area;
	double prim;

private:
	float x, y;
	double fx, fy;	//fx && fy == first x and first y
	void assignments(float, float, double[]); //To set up fx and fy
	void checkRec(float, float);	
	double fxy[2];
	double area_prim[2];

};
[/end MyRectangle.h/]

[MyRectangle.cpp]
/*

****TO DO:*****
1) Make and algerithem that will tell the user if the object is infact a rectangle (wont need to on a 2D structure but will for pt2).

*/


#include "stdafx.h"
#include "MyRectangle.h"
#include <iostream>
#include <SFML/Graphics.hpp>

using namespace std;
using namespace sf;

MyRectangle::MyRectangle()
{
	x = 1;
	y = 1;
}


MyRectangle::~MyRectangle()
{
}


void MyRectangle::run() {
	float h = 0, w = 0;
	char RS;	// RS == restart
	
	do{
		system("cls");
		cout << "Note: input must be bigger than 0 and less than 20." << endl;
		cout << "Please input the width: ";
		cin >> w;
		cout << "Please input the height:";
		cin >> h;
		system("cls");
		setValues(h, w);
		checkRec(h, w);
		assignments(x, y, fxy);

		RectangleShape rectangle(Vector2f(getX(x), getY(y)));
		rectangle.setFillColor(Color::Yellow);
		rectangle.setSize(Vector2f(getX(x), getY(y)));

		RenderWindow window(VideoMode(400, 400), "Rectangle:");
		window.clear();
		window.draw(rectangle);
		window.display();

		cout << fx << fy << endl;

		cout << "\t\tNote: Your rectangle will be set to a 1:20 scale.\n\n";
		cout << "The area of your rectangle is: " << area << "." << endl;
		cout << "The perimiter of your rectangle is: " << prim << "." << endl;
		system("pause");
		system("cls");

		cout << "\t\tWould you like to do it again? (Y/N)";
		cin >> RS;

	} while (RS == 'Y' || RS == 'y');
}


int MyRectangle::getX(float x) {
	x = x * 10;
	return x;
}

int MyRectangle::getY(float y) {
	y = y * 10;
	return y;
}

void MyRectangle::setValues(float width, float height){
	x = width;
	y = height;
}

void MyRectangle::assignments(float x, float y, double fxy[2]){
	fxy[0] = x;
	fxy[1] = y;
}


void MyRectangle::checkRec(float x, float y) {
	if (x < 0 || x > 20)
		tryAgain(x, y);	
}


void MyRectangle::tryAgain(float x, float y) {
	double x_, y_;

	cout << "Please re-enter your width:";
	cin >> x_;
	cout << "Please re-enter your height:";
	cin >> y_;
	system("cls");

	x = x_;
	y = y_;
}


double MyRectangle::area(double fxy[2], double area) {
	double fx = fxy[0], fy = fxy[1];
	area = fx*fy;
	return area;
}


double MyRectangle::prim(double fxy[2], double prim) {
	double fx = fxy[0], fy = fxy[1];
	prim = (fx * 2) + (fy * 2);
	return prim;
}
[/end MyRectangle.cpp/]
//End code 


Thank you for any and all help!
I am a first year student, and am using Visual Studio mixed with the SFML graphics software (can be found at SFML-dev.org)
Last edited on
what the error code is saying.


And what is it saying?
I'm sorry, I did forget that didn't I?

Warning C4244 'argument': conversion from 'int' to 'float', possible loss of data SFML Test c:\users\ely\dropbox\school stuff\c++\c++ projects\rectangle class lab\sfml test\myrectangle.cpp 43

Warning C4244 'argument': conversion from 'int' to 'float', possible loss of data SFML Test c:\users\ely\dropbox\school stuff\c++\c++ projects\rectangle class lab\sfml test\myrectangle.cpp 45

Error C3867 'MyRectangle::area': non-standard syntax; use '&' to create a pointer to member SFML Test c:\users\ely\dropbox\school stuff\c++\c++ projects\rectangle class lab\sfml test\myrectangle.cpp 55

Error C3867 'MyRectangle::prim': non-standard syntax; use '&' to create a pointer to member SFML Test c:\users\ely\dropbox\school stuff\c++\c++ projects\rectangle class lab\sfml test\myrectangle.cpp 56

Warning C4244 'return': conversion from 'float' to 'int', possible loss of data SFML Test c:\users\ely\dropbox\school stuff\c++\c++ projects\rectangle class lab\sfml test\myrectangle.cpp 69

Warning C4244 'return': conversion from 'float' to 'int', possible loss of data SFML Test c:\users\ely\dropbox\school stuff\c++\c++ projects\rectangle class lab\sfml test\myrectangle.cpp 75

Warning C4244 '=': conversion from 'double' to 'float', possible loss of data SFML Test c:\users\ely\dropbox\school stuff\c++\c++ projects\rectangle class lab\sfml test\myrectangle.cpp 106

Warning C4244 '=': conversion from 'double' to 'float', possible loss of data SFML Test c:\users\ely\dropbox\school stuff\c++\c++ projects\rectangle class lab\sfml test\myrectangle.cpp 107

Error (active) declaration is incompatible with "double MyRectangle::area" (declared at line 20 of "c:\Users\Ely\Dropbox\School Stuff\C++\C++ Projects\Rectangle Class Lab\SFML Test\MyRectangle.h") SFML Test c:\Users\Ely\Dropbox\School Stuff\C++\C++ Projects\Rectangle Class Lab\SFML Test\MyRectangle.cpp 112

Error (active) declaration is incompatible with "double MyRectangle::prim" (declared at line 21 of "c:\Users\Ely\Dropbox\School Stuff\C++\C++ Projects\Rectangle Class Lab\SFML Test\MyRectangle.h") SFML Test c:\Users\Ely\Dropbox\School Stuff\C++\C++ Projects\Rectangle Class Lab\SFML Test\MyRectangle.cpp 119

Error C2365 'MyRectangle::area': redefinition; previous definition was 'member function' SFML Test c:\users\ely\dropbox\school stuff\c++\c++ projects\rectangle class lab\sfml test\myrectangle.h 20

Error C2365 'MyRectangle::area': redefinition; previous definition was 'member function' SFML Test c:\users\ely\dropbox\school stuff\c++\c++ projects\rectangle class lab\sfml test\myrectangle.h 20

Error C2365 'MyRectangle::prim': redefinition; previous definition was 'member function' SFML Test c:\users\ely\dropbox\school stuff\c++\c++ projects\rectangle class lab\sfml test\myrectangle.h 21

Error C2365 'MyRectangle::prim': redefinition; previous definition was 'member function' SFML Test c:\users\ely\dropbox\school stuff\c++\c++ projects\rectangle class lab\sfml test\myrectangle.h 21


These are the Error messages and the lines they correspond with.
Last edited on
I think the ones that are confusing me the most are the ones that are saying that one section(s) are incompatible as well as the ones that are saying that MyRectangle..... was redefined and was previously a Member function.
Don't use the same name for input parameters, return parameters, and the name of the function.
Oh ok, thank you, sorry that was kind of a dumb question.
Topic archived. No new replies allowed.