Something wrong with my constructor/function?

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
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.
You should have heard the derp slap I made lol. Thank you kind sir for showing me my simple error.
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
Topic archived. No new replies allowed.