Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Create a Triangle class that has the following member variables: side1 - a d

ID: 649663 • Letter: C

Question

C++
Create a Triangle class that has the following member variables: side1 - a double, side2 - a double, side 3 - a double perimeter area

The class should have the following member functions:

- default constructor that sets the value of all 3 sides of a triangle to 0.0
- a constructor with arguments that accepts values for the three sides of a triangle (member variables) as arguments
- setDimensions - a function that allows the value of the three sides to be entered by the user through the keyboard
- testSides - a function that determines if the 3 values entered can actually be the sides of a triangle. If they do not create a triangle, print the values entered and an appropriate message
            -The sum of any two side lengths of a triangle must always be greater than the length of the third side: so side 1 + side 2 > side 3 and side 1 + side 3 > side 2 and side 2 + side 3 > side 1 ( all three must be true for the 3 values to make a tirangle)
- getSide1 - a function that returns the value of side 1, getSide2 - a function that returns the value of side 2, getSide3 - a function that returns the value of side 3
- getArea - a function that returns the area of a triangle: The formula for the area of a triangle (when the height is not known) is: A = sqrt (p(p-side1)(p-side2)(p-side3)) where p = (side1+side2+side3)/2
- getPerimeter - a function that returns the perimeter of a triangle: Perimeter = side1 + side2+ Side 3
- A displayTriangleInfo function that displays side1, side2, side3, area, and perimeter for a triangle object.

Explanation / Answer

#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;

class Triangle{
public:
double side1, side2, side3, perimeter, area;
Triangle(){
side1 = 0;
side2 = 0;
side3 = 0;
}
Triangle(double a, double b, double c){
side1 = a;
side2 = b;
side3 = c;
}
void setDimensions(){
cout << "Enter the 3 sides: ";
cin >> side1 >> side2 >> side3;
}
bool testSides(){
if(side1 + side2 > side3 && side2 + side3 > side1 && side3 + side1 > side2) return true;
else{
cout << "The 3 sides don't form a triangle ";
return false;
}
}
double getSide1(){
return side1;
}
double getSide2(){
return side2;
}
double getSide3(){
return side3;
}
double getArea(){
double p = (side1 + side2 + side3) / 2.0;
area = sqrt(p * (p - side1) * (p - side2) * (p - side3));
return area;
}
double getPerimeter(){
perimeter = side1 + side2+ side3;
return perimeter;
}
void displayTriangleInfo(){
cout << fixed << setprecision(2);
cout << "The sides of the triangle are: " << side1 << ", " << side2 << ", " << side3 << endl;
cout << "Area: " << getArea() << endl;
cout << "Perimeter: " << getPerimeter() << endl;
}
};


int main(){
Triangle object[5];
for(int i = 0; i < 5; i++){
cout << "Details of triangle " << i + 1 << " ";
object[i].setDimensions();
if(object[i].testSides() == true){
object[i].displayTriangleInfo();
}
cout << endl;
}
}