Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Define triangle class

Define triangle class

Scheduled Pinned Locked Moved C / C++ / MFC
help
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Anonymous
    wrote on last edited by
    #1

    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 Fo

    W 1 Reply Last reply
    0
    • A Anonymous

      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 Fo

      W Offline
      W Offline
      W Hammer sledge
      wrote on last edited by
      #2

      OK, let me see, if i understood you correctly. You want to use an overloaded operator << . But what you have done, is only using the normal output of an float. The overloading would look like this:

      ostream& operator<< (ostream& ostr, CTriangle& triangle)
      {
      ostr << "Side1 = " << triangle.side1 << endl
      << "Side2 = " << triangle.side2 << endl
      << "Side3 = " << triangle.side3 << endl;
      return ostr;
      }

      The overloaded operator== looks like this:

      bool operator ==(const CTriangle& tri1, const CTriangle& tri2)
      {
      if (tri1.side1 == tri2.side1
      && tri1.side2 == tri2.side2
      && tri1.side3 == tri2.side3)
      return true;
      else
      return false;
      }

      My personal advice: You should divide your program into three files. ----------------------- First file - a file containing the declaration of the triangle class. Name this file triangle.h !! Remember that triangle is a class, so name the class CTriangle. !! ----------------------- Second file - Name this file triangle.cpp include your triangle.h by using #include "triangle.h" Now put all the definitions of your CTriangle class in here (for instance yout get-functions etc. and the overloaded operators). Don't forget neccessary inclusions like iostream.h ----------------------- Third file - name this file main.cpp include your triangle.h by using

      #include "triangle.h"
      #include <iostream.h>

      void main()
      {
      CTriangle triangle1, triangle2(5.0, 4.0, 3.0), triangle3(6.0, 4.0, 4.0);
      cout << "Display triangle 1:\n";
      cout << triangle1 ;

      cout << "Display triangle 2:\n";
      cout << triangle2 ;

      cout << "Display triangle 3:\n";
      cout << triangle3;

      if (triangle2 == triangle3)
      cout << "identical" << endl;
      else
      cout << "not identical" << endl;
      <

      }

      I havent compiled it, but this should work. Test it by changing the values of the triangle2 and triangle3. Hope this helps sledge

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups