Class values

I'm learning about classes at the moment, and I'm working on one of the exercises in my book. Here is the exercise description:

Create a class called Triangle that stores the length of the base and height of a right triangle in
two private instance variables. Include a constructor that sets these values. Define two
functions. The first is hypot( ), which returns the length of the hypotenuse. The second is area( ),
which returns the area of the triangle.

My code so far looks like this:

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
#include <iostream>
#include <cmath>

using namespace std;

class triangle {
	double length;
	double height;
public:
	triangle(double a, double b) {
		a = length;
		b = height;
	}
	double hypotenuse();
	double area();
};
double triangle::hypotenuse() {
	return sqrt((length*length) + (height*height));
}
double triangle::area() {
	return (length*height)/2;
}
int main()
{
	triangle ob1(10.0, 10.0);
	cout << "Hypotenuse: " << ob1.hypotenuse() << endl;
	cout << "Area: " << ob1.area() << endl;
	return 0;
}


But it outputs some odd numbers, and they are the same all the time, even if I change the values of ob1 to something else than 10. Any help would be greatly apreciated!
What are these "odd numbers"?
Check lines 11 and 12. You're doing the assignment in the wrong direction. :)

-Albatross
The assignments in lines 11 and 12 are reversed.

EDIT: too slow!
Last edited on
Aah, of course! Thank you very much!
Topic archived. No new replies allowed.