C++ Program to find the area and perimeter of a triangle using class and object.

In this example program, you will learn and get code on finding the area and perimeter of a triangle ith three sides in C++ programming. Here we are using the approach using class and object to create the program.

How to find the area of a triangle with the length of three sides?

The area of a triangle is the region enclosed within the sides of the triangle. The area of a triangle varies from one triangle to another depending on the length of the sides and the internal angles. The area of a triangle is expressed in square units, like, m2, cm2, in2, and so on
Heron's formula is used to find the area of a triangle when the length of the 3 sides of the triangle is known. To use this formula, we need to know the perimeter of the triangle which is the distance covered around the triangle and is calculated by adding the length of all three sides. Heron’s formula has two important steps.
•    Step 1: Find the semi perimeter (half perimeter) of the given triangle by adding all     three sides and dividing it by 2.
•    Step 2: Apply the value of the semi-perimeter of the triangle in the main formula called   'Heron’s Formula'.
Consider the triangle ABC with side lengths a, b, and c. To find the area of the triangle we use Heron's formula:
Area = √s(s−a)(s−b)(s−c)s(s−a)(s−b)(s−c)
Note that (a + b + c) is the perimeter of the triangle. Therefore, 's' is the semi-perimeter which is: (a + b + c)/2

How to find the perimeter of a triangle with the length of three sides?

The perimeter of a triangle is defined as the total length of its boundary. The perimeter of a triangle means the sum of all three sides. To calculate the perimeter of a triangle, we simply add the lengths of the sides given. The basic formula used to calculate the perimeter of a triangle is:
Perimeter = sum of the three sides

Consider the triangle ABC with sides a, b, and c. To find the perimeter we have to add the sides.

perimeter = a + b + c

C++ program to find the area and perimeter of a triangle with three sides using class and object.

Ask the user to enter the length of all three sides. With these side lengths, apply the formula as given above and initialize its value to a variable say the area, and print its value as output.
Create a class with a named triangle. Declare float type variables a, b, c, s, and aa privately to store the side lengths area semi-perimeter and area. Declare three functions for getting the input data, calculating the area, and calculating the perimeter.

The function sqrt() returns the square root of the value passed as its argument. For example, sqrt(25) returns 5. This function is defined in the cmath header file.
Class triangle
{
Private:
Float a, b, c, s, aa;
Public:
Void getdata();
Float area();
Float perimeter();
};

Define each function.
Getdata() to read the length of all sides to the variables a, b, c
cout<<"Enter the length of all three sides: ";
    cin>>a>>b>>c;

area() to compute the area of the triangle
s = (a+b+c)/2;
    aa = sqrt(s*(s-a)*(s-b)*(s-c));
    return aa;

perimeter() to compute the perimeter of the triangle
return (a+b+c);

within the main function create an object t for the class triangle.
triangle t;
Declare two float type variables ar and pr to get the area and perimeter of the triangle.
Call the function to get the input. 
Call the function area with the object t and store the value into the variable ar and call the function perimeter with the object t and store the value into the variable pr. Display the result.

Algorithm

Step 1:  Call the header file iostream.

Step 2: Use the namespace std.

Step 3:Create a class triangle;

Step 4: Declare  float type variable a, b, c, s, aa;

Step 5: Define the function for getting the input.
             Ask the user to enter the length of the three sides
             Read the value to the variable a, b, c;

Step 6: Define the function for calculating the area of the square.
              s = (a+b+c)/2;
              aa = sqrt(s*(s-a)*(s-b)*(s-c));

Step 7: Define the function for calculating the perimeter of the square.
            a + b + c;

Step 8: Open the integer type main function; int main().

Step 9: Create an object t for the class triangle;

Step 10: Declare two float type variables ar and pr;

Step 11: get the function to read input
               t.step 5;

Step 12: get the area to the variable ar 
                ar = t.step 6;

Step 13: get the perimeter to the variable pr
               Pr = t.step 7;

Step 14: Display the value of ar and pr;

Step 15: Exit

C++ Source Code

                                          #include<iostream>
#include<cmath>
using namespace std;
class triangle
{
    private:
        float a, b, c, s, aa;
    public:
        void getdata();
        float area();
        float perimeter();
};
void triangle::getdata()
{
    cout<<"Enter the Length of all Three Sides: ";
    cin>>a>>b>>c;
}
float triangle::area()
{
    s = (a+b+c)/2;
    aa = sqrt(s*(s-a)*(s-b)*(s-c));
    return aa;
}
float triangle::perimeter()
{
    return (a+b+c);
}
int main()
{
   triangle t;
    float ar, pr;
    t.getdata();
    ar = t.area();
    cout<<"\nArea = "<<ar;
    pr = t.perimeter();
    cout<<"\nPerimeter = "<<pr;
    cout<<endl;
    return 0;
}
                                      

OUTPUT

Enter the Length of all Three Sides: 6
5
9
Area = 14.1421
Perimeter = 20