Connect with Facebook Lost Password?


Go Back   CodeCall Programming Forum > Software Development > Classes and Code Snippets

Classes and Code Snippets Post your source code and classes here

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-03-2008, 10:10 PM
Newbie
 
Join Date: Dec 2008
Posts: 3
Rep Power: 0
Disharmony is an unknown quantity at this point
Default Basic Triangle Math [c++]

I'm just starting to get used to object oriented programming. So I figured one of the easiest ways to practice would be to make a program to find the perimeter and/or area of a triangle.

I was going to add a "do...while loop" to exit, as well as a menu for whether or not you wanted to find the area or perimeter, but I figured that would be a waste of time, because the point was for me to learn about Objects, not deal with loops.

Code:
/* Basic Triangle functions code by Disharmony */ 
// DisHarm0ny[at]yahoo.com

#include <cstdlib>
#include <iostream>

//defining the class "triangle"
class triangle
{
      public:
             int getSide1();
             int getSide2();
             int getSide3();
             float getBase();
             float getHeight();
             void setVars();
              
             
             
      private:
             float height;
             int base;
             int side1;
             int side2;
             int side3;
             float area_value;
             int perimeter_value;
};

void triangle::setVars()
{
    std::cout << "What is the value of side1? \n";
    std::cin >> side1;
    std::cout << "What is the value of side2? \n";
    std::cin >> side2;
    std::cout << "What is the value of side3? \n";
    std::cin >> side3;
    std::cout << "What is the value of base? \n";
    std::cin >> base;
    std::cout << "What is the height of the triangle? \n";
    std::cin >> height;
    
}



int triangle::getSide1()
{
    return side1;
}

int triangle::getSide2()
{
    return side2;
}

int triangle::getSide3()
{
    return side3;
}

float triangle::getBase()
{
    return base;
}

float triangle::getHeight()
{
    return height;
}
// Done defining the "triangle" class






float area(float,float);
int perimeter(int ,int ,int );
triangle tri;
using std::cout;
using std::cin;
   

int main()
{
    
   tri.setVars(); //prompt for variables
   
   int side1=tri.getSide1(); //getting the private member variables from public methods
   int side2=tri.getSide2();
   int side3= tri.getSide3();
   float base = tri.getBase();
   float height = tri.getHeight();
   

   int perimeter_value = perimeter(side1, side2, side3);// calculate perimeter and area
   float area_value = area(height, base);
   
   
   cout << "The triangle's perimeter is: " << perimeter_value <<"\n";
   cout << "The triangle's area is: " << area_value << "\n";
   cout << "------------------------------------\n";
   
    
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

float area(float height,float base)
{
    float result = height*base;
    float area_value = result / 2;
    return area_value;
}

int perimeter(int one,int two, int three)
{
    int perimeter_value;
    perimeter_value = one + two + three;
    
    return perimeter_value;
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 12-04-2008, 09:08 AM
WingedPanther's Avatar   
Super Moderator
 
Join Date: Jul 2006
Age: 36
Posts: 8,079
Blog Entries: 48
Rep Power: 20
WingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to behold
Default Re: Basic Triangle Math [c++]

I would only have the user enter the three sides. The software can then calculate the three corresponding heights and the three angles from that information.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Aurel Basic 1.1 build 82 aurelABHR Affiliate Marketplace 15 07-01-2009 04:02 PM
Basic JFrame with a basic JButton. Paradox Java Tutorials 0 07-18-2008 10:46 AM
On This Date in 1964, the First BASIC Program Kernel News 1 05-02-2008 11:24 AM
Conversion of function from C to Basic badkarma General Programming 1 02-27-2008 02:39 PM
Error on My Basic Program Copied from Programming Book kidcash General Programming 6 01-30-2008 11:03 AM


All times are GMT -5. The time now is 09:17 PM.

Freelance Jobs

XML/XSL: Need code for Book with Chapers using XML
Create an XML file for a book of your creation, and a basic CSS file that will format it to display ...
Earn: $40.00


C++/C: Simple firework cue sequencer
What I require is a rework of a simple cue sequencer. I have a piece of hardware (an Arduino boar...
Earn: $50.00


HTML/XHTML: Menu Rework - ASCIIBin
I'm placing this in the HTML/XHTML section of the Freelance site but you are not limited to HTML. Wh...
Earn: $20.00



CodeCall Goal

Goal #1: 1,000 Blogs
Goal #2: 1,000 Wiki Pages
Goal #3: 300,000 Posts
Goal #4: 20,000 Threads
Done: 30%, 23%, 55%, 75%

Ads