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

Define a C++ class that will represent a triangle. Declare 3 private member vari

ID: 675376 • Letter: D

Question

Define a C++ class that will represent a triangle. Declare 3 private member variables (doubles) for the 3 sides. Declare and define the following public member functions:

3 public mutator functions one each for each of the 3 sides, but unlike the Rectangle class, make sure the parameter is > 0 (if not, assign 1)

3 accessor functions for the 3 sides (each will return one of the sides, one double)

a function that calculates and returns the area of the triangle using Heron's formula:

In main, declare a triangle object variable. call the mutators (one at a time) to assign to the 3 sides, then print the area calling the area function on the triangle.

Let a,b,c be the lengths of the sides of a triangle. The area is given by:

where p is half the perimeter, or

In main, declare a triangle object variable. call the mutators (one at a time) to assign to the 3 sides, then print the area calling the area function on the triangle.

Explanation / Answer

#include <iostream>
#include <cmath>
using namespace std;

class Triangle
{
private:
double side1;
double side2;
double side3;
public:
void setSide1(double s)
{
if(s > 0)
side1 = s;
else
side1 = 1;
}
void setSide2(double s)
{
if(s > 0)
side2 = s;
else
side2 = 1;
}
void setSide3(double s)
{
if(s > 0)
side3 = s;
else
side3 = 1;
}   
double getSide1()
{
return side1;
}
double getSide2()
{
return side2;
}
double getSide3()
{
return side3;
}
};

void area(Triangle t)
{
double p, A;
p = (t.getSide1() + t.getSide2() + t.getSide3()) / 2;
A = sqrt(p * (p - t.getSide1()) * (p - t.getSide2()) * (p - t.getSide3()));
cout<<"The area of the sides with sides "<<t.getSide1()<<" "<<t.getSide2()<<" "<<t.getSide3()<<" "<<" is: "<<A<<endl;
}   

int main()
{
Triangle t;
double side;
cout<<"Enter the sides of a triangle: "<<endl;
cout<<"Side1: ";
cin>>side;
t.setSide1(side);
cout<<"Side2: ";
cin>>side;
t.setSide2(side);
cout<<"Side3: ";
cin>>side;
t.setSide3(side);
area(t);
}
  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote