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

January 30, 2023, Learn eTutorial

How do we calculate the area of a triangle, with three sides known?

The area of a triangle is the region enclosed within the sides. The area varies from one triangle to another depending on the length of the sides and the internal angles.

Heron's formula is used to calculate the triangle's area when the length of the 3 sides is known. To use this formula, we need to know the perimeter, which is the distance covered around the triangle and is calculated by adding the length of all three sides.

triangle area calculation formula
Heron’s formula has two important steps,
  • Step 1: calculate 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 in the 'Heron’s Formula' for the area calculation.

How to calculate the perimeter of a triangle?

The perimeter of a triangle is defined as the total length of its boundary, which means the sum of all three sides. To calculate the perimeter, we simply add the lengths of the three sides given. 

Perimeter = a + b + c, where a, b, c are sides 

How to implement the area and perimeter logic in a C++ program?

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 are semi-perimeter and area. Declare three functions for getting the input data, calculating the area, and calculating the perimeter.

  • sqrt() returns the square root of the value passed as its argument. 
  • Getdata() to read the length of all sides to the variables a, b, c
  • area() to compute the area of the triangle
  • perimeter() to compute the perimeter of the triangle

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. Now call the function area with the object t and store the value in the variable ar and call the function perimeter with the object t and store the value into the variable pr. Display the result.


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 the 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

using namespace std;
class triangle
        float a, b, c, s, aa;
        void getdata();
        float area();
        float perimeter();
void triangle::getdata()
    cout<<"Enter the Length of all Three Sides: ";
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;
    ar = t.area();
    cout<<"\n Area = "<<ar;
    pr = t.perimeter();
    cout<<"\nPerimeter = "<<pr;
    return 0;


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