Something wrong with my constructor/function?

Mar 13, 2013 at 12:59am
This program is supposed to accurately calculate the perimeter of two rectangles. It always prints out the number 858993466 for each however. I'm sure it's a simple syntax error.

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

#include<iostream>
using namespace std;

class Rect
{
	int a, b, c, d;
public:
	Rect (int, int, int, int);
	int perimeter()
	{
		return (a + b + c + d);
	}
};

Rect::Rect(int one, int two, int three, int four)
{
	one = a;
	two = b;
	three = c;
	four = d;
}

int main()
{
	Rect uno (1,1,1,1);
	Rect dos (4,11,4,6);
	cout << "Rect 1 P: " << uno.perimeter() << endl;
	cout << "Rect 2 P: " << dos.perimeter() << endl;
	return 0;
}

Last edited on Mar 13, 2013 at 1:00am
Mar 13, 2013 at 1:04am
You wrote the assignments in the constructor backwards. This way the arguments get the same value as the uninitialized members, and the members themselves remain uninitialized.
Mar 13, 2013 at 3:36am
You should have heard the derp slap I made lol. Thank you kind sir for showing me my simple error.
Mar 13, 2013 at 3:53am
Rect dos (4,11,4,6);

That isnt a rectangle.

If a Rectangle has four arguments, then I expect the first two to be an x-y coordinate, and the others to be width and height,
Last edited on Mar 13, 2013 at 3:54am
Topic archived. No new replies allowed.