Classes

Hi all,
We're doing classes in CPP at my course right now using OOP and Im a bit lost with them. I understand the whole process - I think, but I just cant seem to get it right.

Basically the whole premise is that we have to:
1. Craete a class named Triangle
2. Encapsulate a, b, c - the triangle side lengths
3. bool Set(double aa, double bb, double cc); - sets the values and returns true or false if such a triangle is possible.
4. double Perim(); - calculates the perimeter of the triangle
5. double Area(); - calculates the triangle area
6. bool isRect(); - checks whether this is a right-angle triangle.

I hope that makes sense?

Heres what I have so far:

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
#include <iostream>
#include <cmath>
using namespace std;

class Triangle{
public:
  Triangle();
  bool Set(double a, double b, double c);
  double Perim();
  double Area();
  bool isRect();

private:
  double a; //triangle side lengths
  double b;
  double c;

};

Triangle::Triangle(){
  a = b = c = 0;
}

Triangle::Perim(){
  Perim = a + b + c;
}

Triangle::Area(){
  Area = (0.5)*a*c;
}

Triangle::isRect(){
a^2 + b^2 = c^2; //formula for right angled triangle check
}

Triangle::Set(double a, double b, double c){

Set = (((a + b) > c) && ((a + c) > b) && ((b + c) > a)) ? true : false;


// a + b > c --- if one of these is false, then triangle is not possible
// a + c > b
// b + c > a
}

main() {
Triangle t;

t.Set (15, 25, 35);
t.Perim();
t.Area();
t.isRect();

  return 0;
}



Sorry if this looks messy, im still in the progress of actually putting everything together and will clean up afterwards.

Im getting some errors - i.e. Triangle::Perim does not match any in class Triangle and Candidate is: double Triangle::Perim(); ?

Any pointers on what Im doing wrong or right for that matter?

Thank you in advance.
Last edited on
When defining the functions outside the class you need to mention the return type.

1
2
double Triangle::Perim() {
	...

And then inside the functions you have to use return statements to return the values that you calculate.

1
2
3
	...
	return a + b + c;
}
Line 25, 29 and so on is when I recall right Pascal notation. In C[++] you need the return notation like Peter87 mentioned.

Line 33 does not do what you think it does. The ^ is the binary xor operator. Not the power operator. Better write a * a + b * b... instead.
Thank you for the quick reply!
I think I've done it right now - mostly.

The only thing Im still working on is the bool values. They are not returning true or false.
The problem im having is that the Set method is supposed to return true or false, based on whether the triangle side lengths make a triangle based on that formula, but my lecturer gave me that weird prerequisite - bool Set(double aa, double bb, double cc); --- I just dont see how this works together with the rest of the program.

Any pointers on that?
Heres what I have now:

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
#include <iostream>
#include <cmath>
using namespace std;

class Triangle{
public:
  Triangle();
  bool Set(double aa, double bb, double cc);
  double Perim();
  double Area();
  bool isRect();

  double a; //triangle sides
  double b;
  double c;
};

Triangle::Triangle(){
 a = b = c = 0;
}

double Triangle::Perim(){
  return a + b + c;
}

double Triangle::Area(){
  return (0.5)*a*c;
}

bool Triangle::isRect(){
return ((a*a) + (b*b) == (c*c)) ? true : false; //---checks if this is a right angle triangle, and should return true or false.
}

bool Triangle::Set(double aa, double bb, double cc){ //---This is what Im having trouble with. This checks whether this triangle is possible and returns, true or false that should print on the screen.

return (((aa + bb) > cc) && ((aa + cc) > bb) && ((bb + cc) > aa)) ? true : false;


// a + b > c --- if one of these is false, a triangle cannot exist
// a + c > b
// b + c > a
}

main() {
Triangle t;

t.a = 10;
t.b = 10;
t.c = 10;

cout << t.Perim() <<endl;
cout << t.Area() <<endl;
cout << t.isRect() <<endl;
return 0;

}


Sorry if this is really straight forward and Im just being stupid at this point :D

Thank you again.
closed account (E0p9LyTq)
A couple of changes to one of your class member functions and main:
1
2
3
4
bool Triangle::isRect()
{
   return (((a*a) + (b*b)) == (c*c));
}

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
int main()
{
   Triangle t;

   t.a = 10;
   t.b = 10;
   t.c = 10;

   std::cout << t.Perim()  << '\n';
   std::cout << t.Area()   << '\n';
   std::cout << t.isRect() << '\n';

   if (t.isRect())
   {
      std::cout << "Is Right Triangle\n";
   }
   else
   {
      std::cout << "Is NOT Right Triangle\n";
   }
   std::cout << '\n';

   t.a = 3;
   t.b = 4;
   t.c = 5;

   std::cout << t.Perim() << '\n';
   std::cout << t.Area() << '\n';
   std::cout << t.isRect() << '\n';

   if (t.isRect())
   {
      std::cout << "Is Right Triangle\n";
   }
   else
   {
      std::cout << "Is NOT Right Triangle\n";
   }
}
30
50
0
Is NOT Right Triangle

12
7.5
1
Is Right Triangle

I don't know what your compiler is, but with modern C++ the main function must return int.
@FurryGuy thanks man, I see how I went wrong now.
closed account (E0p9LyTq)
Sides of 10, 10 & 10 is as much a valid triangle as 3, 4, & 5. One isn't a right angle triangle, the other is.

Your member function Set() probably should not be testing if the inputs make a valid triangle, especially using the method you have, it should just update the class data members that represent the sides.

If you are only looking for right angled triangles (RAT) Set() could call isRect() before the data members are updated. If the inputs don't make a valid RAT then output an error message and return without any changes. Otherwise update the data members.

Personally I wouldn't call a function that tests a triangle is a RAT IsRect(). Maybe something like IsRightTriangle().

IsRect() sounds like a function to check if a four-sided object is a rectangle.
You don't need a ternary operator to return the result of a boolean operation.

Your check for a right triangle is only allowing for one particular pair to include the right angle (and is likely to be very sensitive to floating point rounding).

Your formula for area is only correct for a tiny proportion of triangles - use Heron's formula instead.

If you want output of 'true' or 'false' (rather than 1 or 0) put << boolalpha in the output stream.
Last edited on
Topic archived. No new replies allowed.