View Full Version: Define a class for triangles

C++ Learning Community > C++ Works in Progress > Define a class for triangles


Title: Define a class for triangles
Description: define a class for triangles


Gustavo - October 10, 2003 12:54 AM (GMT)
am to define a class for triangles, and once I have defined it I need to do tese procedurse:

The constructor (with sides) is to call order to order the sides from smallest to largest.
and
The SetTriangle function is to call order to order the sides from smallest to largest

and then display the sides and the type it is... scalene and right, isosceles and acute, ....

I think I may need to give more detail. Triangle1 will not have any values defined. Triangle2 has sides 6.0, 6.0, 6.0. Triangle3: 5.0, 4.0, 3.0. Triangle4: 6.0, 4.0, 4.0

And then for Triangles 2, 4, and 4... I

1) Use friend operator function << to display the three sides of each

2) Test to see if the triangle is Equilateral, Isosceles, or Scalene. When one of the functions returns true, display message. Message should not be displayed within function.

3) Test to see if Right, Acute, Obtuse

For the copy test

1) Use the friend copy function to copy triangle 4 to triangle 1

2) Use friend operator function << to display the three sides of triangle 4 and triangle 1.

3)Use friend operator function == to see if triangle 1 is equal to triangle 4. If the two are, display " are equal", otherwise " are not equal.

For another == test, do the following

1)Use friend operator function << to display three sides of triangle2 and triangle3.

2)Use friend operator function ++ to see if triangle2 is equal to triangle3 and display appropriate message

For the SetTriangle test, do the following:

1)set Triangle3's sides to (5, 4, 3 (in that order))

2)use friend operator function << to disply the three sides of triangle3.

There are all the specifications and what not. It has been like a week... and I have only gotten as far as this Could I please get some help all around on this one. I would so much appreciate it. Please, could I get some help. I have done this much on my own, and now I have just gotten stuck and don't really have a clue as what to do.

CODE
#include <iostream.h>

class Triangle
{
public:
friend TriangleCopy(Triangle triangle1);
friend bool operator ==(const Triangle& triangle1, const Triangle& triangle2);
friend ostream& operator <<(ostream& outs, Triangle& the_triangle);

Triangle(float side1, float side2, float side3);
Triangle();

void SetTriangle(float side1, float side2, float side3);

bool IsEquilateral(Triangle side_1, Triangle side_2, Triangle side_3);
bool IsIsosceles(Triangle side_1, Triangle side_2, Triangle side_3);
bool IsScalene(Triangle side_1, Triangle side_2, Triangle side_3);

bool IsRight(Triangle side_1, Triangle side_2, Triangle side_3);
bool IsActue(Triangle side_1, Triangle side_2, Triangle side_3);
bool IsObtuse(Triangle side_1, Triangle side_2, Triangle side_3);

float get_side1();
float get_side2();
float get_side3();

private:
   void swap();
void order();
float side_1, side_2, side_3;
};

void main()
{

Triangle triangle1, triangle2(6.0, 6.0, 6.0), triangle3(5.0, 4.0, 3.0), triangle4(6.0, 4.0, 4.0);
cout << "Display triangle 1:\n";
cout << "Side 1 = " << triangle1.get_side1() << endl;
cout << "Side 2 = " << triangle1.get_side2() << endl;
cout << "Side 3 = " << triangle1.get_side3() << endl;

cout << "Display triangle 2:\n";
cout << "Side 1 = " << triangle2.get_side1() << endl;
cout << "Side 2 = " << triangle2.get_side2() << endl;
cout << "Side 3 = " << triangle2.get_side3() << endl;

cout << "Display triangle 3:\n";
cout << "Side 1 = " << triangle3.get_side1() << endl;
cout << "Side 2 = " << triangle3.get_side2() << endl;
cout << "Side 3 = " << triangle3.get_side3() << endl;

cout << "Display triangle 4:\n";
cout << "Side 1 = " << triangle4.get_side1() << endl;
cout << "Side 2 = " << triangle4.get_side2() << endl;
cout << "Side 3 = " << triangle4.get_side3() << endl;
}

Triangle::Triangle(float side1, float side2, float side3)
{
side_1 = side1;
side_2 = side2;
side_3 = side3;
order (side_1, side_2, side_3);
}

void swap(float side_1, float side_2)
{
float temp;
side_1 = temp;
side_1 = side_2;
side_2 = temp;
}
void order(float side_1, float side_2, float side_3)
{
if (side_1 > side_2)
 swap (side_1, side_2);
if (side_1 > side_3)
 swap (side_1, side_3);
if (side_2 > side_3)
 swap (side_2, side_3);
}


Triangle::Triangle() : side_1(0), side_2(0), side_3(0)
{
//empty
}
float Triangle::get_side1()
{
return side_1;
}
float Triangle::get_side2()
{
return side_2;
}
float Triangle::get_side3()
{
return side_3;
}

/*bool IsEquilateral(Triangle side_1, Triangle side_2, Triangle side_3)
{
if(side_1 == side_2 && side_1 == side_3 && side_2 == side_3);
return true;
}*/






TheHawgMaster - October 10, 2003 01:58 AM (GMT)
It looks like an interesting exercise.

Gustavo - October 10, 2003 02:06 AM (GMT)
Please help me. I am still working on it, and still am yet to get past what I posted earlier... like 2 hours ago. I just don't know what to do. I need help. I have been working on this for like 2 days now. Thanks for those who might help. Bye.

TheHawgMaster - October 10, 2003 02:28 AM (GMT)
Hey wait, I looked it over a little more and it stopped making sense. Why do you only store the lengths of the sides? What use does that have in a typical program? What are you trying to do with this? Why is do you seem to have "Triagles" as all of your arguments for functions that should probably have void argument lists?

It just doesn't seem that useful. Maybe you should first just try to create a triangle class that can draw its self to a DC or something simpler like that. Then you might be able to add methods for determining its properties like the ones that you seem to be planning.

(Sorry if I sounded a little negitive there)

Gustavo - October 10, 2003 02:39 AM (GMT)
This is just to see that I know how to do it. Ok Hawg, maybe this will help you just that it shows I am all good with functions, which obviously not as good as I thought
And this program will put out a display of

Example: display triangle 2
side 1: 3.0
side 2: 4.0
side 3: 5.0
Is scalene.
Is right.

The functions fo the class just need to show that they work properly. I thought I explained it well enough at first. Maybe not, I'm sorry. Peace

TheHawgMaster - October 10, 2003 04:07 AM (GMT)
I'm just saying that shouldn't you be using at least 3 coordinate pairs (triples in 3d) to store a triangle effectivly? There is no way that you could do much just storing the lengths of the sides. You should make sure you have enough information so that their is only one way that you could re-construct the triangle.

And just curious about your technique, why do you pass three triangles to most of your methods? I cannot think of any good reason for this.

Gustavo - October 10, 2003 04:22 AM (GMT)
Hey Hawg. It seems we are not really comprhending each other. I am writing this program for a C++ class that I have just started taking. Well, I am comming along more and more with this program, but I could still obviously use some help. I am going to put the specifications in full out here.

Define a class for triagnles. A triangle is defined by three sides. Class should have following capabilities:

A constructor that will allow definining a triangle. (float side1, float side2, float side3). The constructor is to call Order to place the sides in order from smallest to largest.

A constructo with no arguments: initialize the three sides to zero.

Public member functions needed.

void SetTriangle(float side1, float side2, float side3);
bool IsEquilateral();
bool IsIsosceles();
bool IsScalene();
bool IsRight();
bool IsAcute();
bool IsObtuse();
friend Triangle Copy(Triangle triangle1);
friend bool operator ==(const Triangle& triangle1, const Triangle& triangle2);
friend ostream& operator <<(ostream& outs, Triangle& the_triangle);

Private member functions needed.

void Order;
void Swap;

I need to write a program that will define triangle1, triangle2, triangle3, triangle4

am to define a class for triangles, and once I have defined it I need to do tese procedurse:

The constructor (with sides) is to call order to order the sides from smallest to largest.
and
The SetTriangle function is to call order to order the sides from smallest to largest

and then display the sides and the type it is... scalene and right, isosceles and acute, ....

I think I may need to give more detail. Triangle1 will not have any values defined. Triangle2 has sides 6.0, 6.0, 6.0. Triangle3: 5.0, 4.0, 3.0. Triangle4: 6.0, 4.0, 4.0

And then for Triangles 2, 4, and 4... I

1) Use friend operator function << to display the three sides of each

2) Test to see if the triangle is Equilateral, Isosceles, or Scalene. When one of the functions returns true, display message. Message should not be displayed within function.

3) Test to see if Right, Acute, Obtuse

For the copy test

1) Use the friend copy function to copy triangle 4 to triangle 1

2) Use friend operator function << to display the three sides of triangle 4 and triangle 1.

3)Use friend operator function == to see if triangle 1 is equal to triangle 4. If the two are, display " are equal", otherwise " are not equal.

For another == test, do the following

1)Use friend operator function << to display three sides of triangle2 and triangle3.

2)Use friend operator function ++ to see if triangle2 is equal to triangle3 and display appropriate message

For the SetTriangle test, do the following:

1)set Triangle3's sides to (5, 4, 3 (in that order))

2)use friend operator function << to disply the three sides of triangle3.

There are all the specifications and what not. It has been like a week... and I have only gotten as far as this Could I please get some help all around on this one. I would so much appreciate it. Please, could I get some help. I have done this much on my own, and now I have just gotten stuck and don't really have a clue as what to do.

I could very much use help with the code.


TheHawgMaster - October 10, 2003 06:29 AM (GMT)
OK you really threw me off by turning
CODE
bool IsEquilateral();
bool IsIsosceles();
bool IsScalene();
bool IsRight();
bool IsAcute();
bool IsObtuse();

into
CODE
bool IsEquilateral(Triangle side_1, Triangle side_2, Triangle side_3);
bool IsIsosceles(Triangle side_1, Triangle side_2, Triangle side_3);
bool IsScalene(Triangle side_1, Triangle side_2, Triangle side_3);
bool IsRight(Triangle side_1, Triangle side_2, Triangle side_3);
bool IsActue(Triangle side_1, Triangle side_2, Triangle side_3);
bool IsObtuse(Triangle side_1, Triangle side_2, Triangle side_3);


I guess I'll try and implement some of them for you:
CODE
void Triangle::swap(float& side_1, float& side_2)
{
float temp = side_1;
side_1 = side_2;
side_2 = temp;
}

void Triangle::order()
{
if (side_1 > side_2)
swap (side_1, side_2);
if (side_1 > side_3)
swap (side_1, side_3);
if (side_2 > side_3)
swap (side_2, side_3);
}

bool Triangle::IsEquilateral()
{
 return(side_1 == side_2 && side_2 == side_3);
}

bool Triangle::IsIsosceles()
{
 if(side_1 == side_2 && side_1 != side_3 && side_2 != side_3)
   return(true);
 if(side_2 == side_3 && side_2 != side_1 && side_3 != side_1)
   return(true);
 if(side_1 == side_3 && side_1 != side_2 && side_3 != side_2)
   return(true);
 return(false);
}

bool Triangle::IsScalene()
{
 return(side_1 != side_2 && side_2 != side_3 && side_1 != side_3);
}

bool Triangle::IsAcute()
{
 return(side_1 + side_2 < size_3);
}


You weren't telling the compiler what class your functions were for! When defineing a method for a class outside of the class you use the syntax:

ReturnType ClassName::Methodname(Arguments) {}


Oh well, I hpoe that helps.

Gustavo - October 10, 2003 07:03 AM (GMT)
Thanks for all this wonderful help Hawg, but what about all of my friend functions that I need to use. And also the SetTriangle function. Could you show me how to do that. Where I have some idea, not much though, of how to set up the ostream& operator, I still need help with that. I have no clue how to set up the other two, Copy and ==. And again, thanks for the help.

Gustavo - October 10, 2003 07:16 AM (GMT)
And also, I have implemented some code into your implementations and I am getting two unresolved external symbols

CODE

#include <iostream.h>
#include <stdlib.h>

class Triangle
{
public:
friend TriangleCopy(Triangle triangle1);
friend bool operator ==(const Triangle& triangle1, const Triangle& triangle2);
friend ostream& operator <<(ostream& outs, Triangle& the_triangle);

Triangle(float side1, float side2, float side3);
Triangle();

void SetTriangle(float side1, float side2, float side3);

bool IsEquilateral();
bool IsIsosceles();
bool IsScalene();

bool IsRight();
bool IsAcute();
bool IsObtuse();
float side_1, side_2, side_3;
private:
void swap(float& side_1, float& side_2);
void order();
};

void main()
{
Triangle triangle1, triangle2(6.0, 6.0, 6.0), triangle3(5.0, 4.0, 3.0), triangle4(6.0, 4.0, 4.0);
cout << "display triangle 2: ";

}
void Triangle::swap(float& side_1, float& side_2)
{
float temp = side_1;
side_1 = side_2;
side_2 = temp;
}

void Triangle::order()
{
if (side_1 > side_2)
swap (side_1, side_2);
if (side_1 > side_3)
swap (side_1, side_3);
if (side_2 > side_3)
swap (side_2, side_3);
}

bool Triangle::IsEquilateral()
{
return(side_1 == side_2 && side_2 == side_3);
}

bool Triangle::IsIsosceles()
{
if(side_1 == side_2 && side_1 != side_3 && side_2 != side_3)
  return(true);
if(side_2 == side_3 && side_2 != side_1 && side_3 != side_1)
  return(true);
if(side_1 == side_3 && side_1 != side_2 && side_3 != side_2)
  return(true);
return(false);
}

bool Triangle::IsScalene()
{
return(side_1 != side_2 && side_2 != side_3 && side_1 != side_3);
}

bool Triangle::IsAcute()
{
return(side_1 + side_2 < side_3);
}






TheHawgMaster - October 10, 2003 12:42 PM (GMT)
About the unresolved externals, it would help alot I you told me what they were.

Did you see what I did with the others to make them work? Anyway, here are a few others:
CODE
Triangle::Triangle(float side1, float side2, float side3)
{
 SetTriangle(side1, side2, side3);
}

void Triangle::SetTriangle(float side1, float side2, float side3)
{
 side_1 = side1;
 side_2 = side2;
 side_3 = side3;
 order (side_1, side_2, side_3);
}

friend bool Triangle::operator==(const Triangle& Triangle1, const Triangle& Triangle2)
{
 return(Triangle1.get_side1() == Triangle2.get_side1() && Triangle1.get_side2() == Triangle2.get_side2() && Triangle1.get_side3() == Triangle2.get_side3);
}

friend ostream& operator<<(ostream& outs, Triangle& the_triangle)
{
 return(outs << the_triangle.get_side1() << the_triangle.get_side2() << the_triangle.get_side3());
}


I feel like I'm doing all your work for you... I hope that's how you declare friend functions because I have never used them before.

Gustavo - October 10, 2003 01:20 PM (GMT)
Yea Hawg, I see what ya did. And I do appreciate all the help you have given me. Well here is what I got now:

CODE
#include <iostream.h>
#include <stdlib.h>

class Triangle
{
public:
friend TriangleCopy(Triangle triangle1);
friend bool operator ==(const Triangle& triangle1, const Triangle& triangle2);
friend ostream& operator <<(ostream& outs, Triangle& the_triangle);

Triangle(float side1, float side2, float side3);
Triangle();

void SetTriangle(float side1, float side2, float side3);

bool IsEquilateral();
bool IsIsosceles();
bool IsScalene();

bool IsRight();
bool IsAcute();
bool IsObtuse();
float side_1, side_2, side_3;
float get_side1();
float get_side2();
float get_side3();

private:
void swap(float& side_1, float& side_2);
void order();
};

void main()
{
//Triangle triangle1, triangle2(6.0, 6.0, 6.0), triangle3(5.0, 4.0, 3.0), triangle4(6.0, 4.0. 4.0)
cout << "display triangle 2: ";

}

Triangle::Triangle(float side1, float side2, float side3)
{
SetTriangle(side1, side2, side3);
}

void Triangle::SetTriangle(float side1, float side2, float side3)
{
side_1 = side1;
side_2 = side2;
side_3 = side3;
order (side_1, side_2, side_3);
}

void Triangle::swap(float& side_1, float& side_2)
{
float temp = side_1;
side_1 = side_2;
side_2 = temp;
}

void Triangle::order()
{
if (side_1 > side_2)
swap (side_1, side_2);
if (side_1 > side_3)
swap (side_1, side_3);
if (side_2 > side_3)
swap (side_2, side_3);
}

float Triangle::get_side1()
{
return side_1;
}
float Triangle::get_side2()
{
return side_2;
}
float Triangle::get_side3()
{
return side_3;
}

friend bool Triangle::operator==(const Triangle& Triangle1, const Triangle& Triangle2)
{
return(Triangle1.get_side1() == Triangle2.get_side1() && Triangle1.get_side2() == Triangle2.get_side2() && Triangle1.get_side3() == Triangle2.get_side3);
}

friend ostream& operator <<(ostream& outs, Triangle& the_triangle)
{
return(outs << the_triangle.get_side1() << the_triangle.get_side2() << the_triangle.get_side3());
}

bool Triangle::IsEquilateral()
{
return(side_1 == side_2 && side_2 == side_3);
}

bool Triangle::IsIsosceles()
{
if(side_1 == side_2 && side_1 != side_3 && side_2 != side_3)
  return(true);
if(side_2 == side_3 && side_2 != side_1 && side_3 != side_1)
  return(true);
if(side_1 == side_3 && side_1 != side_2 && side_3 != side_2)
  return(true);
return(false);
}

bool Triangle::IsScalene()
{
return(side_1 != side_2 && side_2 != side_3 && side_1 != side_3);
}

bool Triangle::IsRight()
{
return(side_3 * side_3 == side_1 * side_1 + side_2 * side_2);
}
bool Triangle::IsAcute()
{
return(side_1 + side_2 < side_3);
}
bool Triangle::IsObtuse()
{
return (side_3 * side_3 > side_1 * side_1 + side_2 * side_2);
}



But I am getting errors with this, and here they are:

error C2660: 'order' : function does not take 3 parameters
error C2039: '==' : is not a member of 'Triangle'
: see declaration of 'Triangle'
error C2255: '==' : a friend function can only be declared in a class
error C2556: 'void __cdecl ==(const class Triangle &,const class Triangle &)' : overloaded function differs only by return type from 'bool __cdecl operator ==(const class Triangle
&,const class Triangle &)'
: see declaration of '=='
error C2662: 'get_side1' : cannot convert 'this' pointer from 'const class Triangle' to 'class Triangle &'
Conversion loses qualifiers
error C2662: 'get_side1' : cannot convert 'this' pointer from 'const class Triangle' to 'class Triangle &'
Conversion loses qualifiers
error C2662: 'get_side2' : cannot convert 'this' pointer from 'const class Triangle' to 'class Triangle &'
Conversion loses qualifiers
error C2662: 'get_side2' : cannot convert 'this' pointer from 'const class Triangle' to 'class Triangle &'
Conversion loses qualifiers
error C2662: 'get_side3' : cannot convert 'this' pointer from 'const class Triangle' to 'class Triangle &'
Conversion loses qualifiers
error C2255: '<<' : a friend function can only be declared in a class
error C2556: 'void __cdecl operator <<(class ostream &,class Triangle &)' : overloaded function differs only by return type from 'class ostream &__cdecl operator <<(class ostream
&,class Triangle &)'
see declaration of '<<'
Error executing cl.exe.

Any help here?

TheHawgMaster - October 10, 2003 01:39 PM (GMT)
Remove all arguments you are passing to order() since it takes no arguments now.

Move the definitions of the friend operators into the class interface.

I think the other problems are stemming from those...

Gustavo - October 10, 2003 02:00 PM (GMT)
I thought the definitions were already in the class interface. Would you mind showing me what to do? I know it probably does feel like you are doing it all for me, but you are really really helping me out. Thank You.

TheHawgMaster - October 10, 2003 07:37 PM (GMT)
I mean to put the actual code for the operator into the class interface rather than outside like I had shown it before, like:

class c {

float a;
friend operator==(c z, c x) {
if(z.a

TheHawgMaster - October 10, 2003 07:38 PM (GMT)
I mean to put the actual code for the operator into the class interface rather than outside like I had shown it before, like:
CODE

class c {
 public:
 float a;
 friend operator==(c z, c x) {
   return(z.a == x.a);
 }




* Hosted for free by InvisionFree