Need Help. Class with constructors

This is the problem:

Create a class called 'Triangle' with the following member
functions (public) and variables (private).

Variables -- 3 lengths -- all 3 values are floats.
1 bool -- tells if current lengths are valid.

Functions --
Constructor with no arguments (default constructor) -- asks user for
3 values and verifies they are valid. (all 3 > 0, sum of any 2
greater than the 3rd.) Keeps asking until it gets valid values.

Constructor with 3 lengths -- (3 arguments).
These can be used to "set" values from within a function.
It needs to verify the values are legitimate (all 3 > 0, sum
of any 2 greater than the 3rd.) Stores them even if they are
invalid (stores false in bool argument in this case.)

GetTriangle -- "returns" the 3 lengths and boolean value.

Perimeter -- returns the sum (float) of the 3 sides.

Equilateral -- returns if the triangle is/isn't equilateral (bool)

Right -- returns if the triangle is/isn't right (bool)

(overloaded) == operator to test if 2 triangles are similar in size.

Write the classes functions/constructors.

Also write a main function to test the above.


Here is my code so far, I don't know how to use constructors. Can someone please help me.

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

class Triangle
{
public:
   void GetTriangle();
   void Perimeter();
   void TriangleType();
private:
   float sa, sb, sc;
   void check_data();
};

int main()
{
   Triangle triangle;
   triangle.GetTriangle();
   triangle.Perimeter();
   triangle.TriangleType();

return 0;
}

   void Triangle::GetTriangle()
   {
      // stores the values input by the user.
      do
      {
         cout << "Enter sides of a triangle." << endl;
         cin >> sa >> sb >> sc;
         check_data();

      }while(sa <= 0 || sb <= 0 || sc <= 0 || (sa + sb) < sc);
   }

   void Triangle::Perimeter()
   {
      // calculates the perimeter of the triangle.
      float perimeter = sa + sb + sc;
      cout << "The perimeter is " << perimeter << " units." << endl;
   }

   void Triangle::check_data()
   {
       // checks if the values are correct.
       if(sa <= 0 || sb <= 0 || sc <= 0 || (sa + sb) < sc)
       {
          cout << "Incorrect values, try again." << endl;
       }
   }

   void Triangle::TriangleType()
   {
      // check if the triabgle is an equilateral triangle.
      if(sa == sb && sb == sc)
      {
         cout << "The triangle is an equilateral triangle." << endl;
      }
      else
      {
         cout << "The triangle is not an equilateral triangle." << endl;
      }
      // check if the triangle is a right triangle.
      if(sqrt((sa * sa) + (sb * sb)) == sc)
      {
         cout << "The triangle is a right triangle." << endl;
      }
      else
      {
         cout << "The triangle is not a right triangle." << endl;
      }

   }

http://www.cplusplus.com/doc/tutorial/classes/
See section "Constructors and destructors".
I have read that page multiple times but I still do not understand how to use constructors.
To write a constructor, write it like you would any other function with the following exceptions: the constructor must have the same name as the class and have no return value (so no 'void class_name' or 'int class_name' just class_name).

A constructor is called when an object is created. So when the program runs and it initializes an object, it calls the constructor for the corresponding class.

Decontructors work in a similar way. They are called when an object is deleted. This usually happens when a program returns 0 then terminates.

I found this video helpful when I learned about this :)
http://www.youtube.com/watch?v=_b7odUc7lg0&feature=edu&list=PLAE85DE8440AA6B83

I hope I helped :) Good luck!
That video helped a lot. I was able to fix my program. Thanks a lot.
Topic archived. No new replies allowed.