Please solve the problem 12. (30 pts) Right Triangle with Sides (Parts a - d). L
ID: 3730233 • Letter: P
Question
Please solve the problem12. (30 pts) Right Triangle with Sides (Parts a - d). Let's say we need to design two classes, one Side and one named RightTriangle. Each Side consists of one double precision mLength, which is stored in centimeters (cm). A RightTriangle is defined by three sides. We assume that the sides always form a RightTriangle (i.e. two of the sides form a 90-degree angte value representing will a. (7 pts) Declare the Side class based on the description abov e. There are many operations that we could define for a side, but we are only going to look at a subset for this problem. attributes/data members should be private. The class should declare the following public member operations/functions only: i. (2 pts) A constructor that specifies one arguments newlength sets it by default to o. i. (2 pts) A const function called getLength() for getting the miength. ii. (2 pts) A function called displayLength() for displaying to the screen the length of the side in the form: mlength inches. Note you need to display the length in inches! Recall, 1 inch-2.54 cm class Side public:// Should contain three prototypes/declarations Rashenal C) private: // Should contain one attributes Joublt side 3:
Explanation / Answer
#include<string> //added extra to test
#include<ostream> //added extra to test
#include<iostream> //added exra to test
using namespace std; // added extra to test the code
//this is the side class
class Side
{
public:
Side(double length=0) // the side constructor which initializes default value to 0
{
mLength = length;
}
const double getLength() //it returns the lenght of the side
{
return mLength;
}
string displayLength() //it returns a string to display the length in inches
{
double toInches = mLength/2.54;
return to_string(toInches) + " inches";
}
private:
double mLength; // the private variable
};
class RightTriangle
{
public:
RightTriangle(double a,double b,double c) // extra added to test the code the quesntion doesnot ask for this.
{
sides = new Side[3]{a,b,c};
}
RightTriangle(RightTriangle &rt)
{
sides = new Side[3]{rt.sides[0].getLength(),rt.sides[1].getLength(),rt.sides[2].getLength()}; // allocate memory for 3 Side objects with values
}
~RightTriangle()
{
delete[] sides; // deleting the allocated memory in the distructor
}
double computeArea()
{
double a,b,c;
a=sides[0].getLength();
b=sides[1].getLength();
c=sides[2].getLength();
if(a>b) //check which is the hypotenuse and use the other two to calculate the area
{
if(a>c)
{
return (0.5*(b*c));
}
else
{
return (0.5*(b*a));
}
}
else
{
if(b>c)
{
return (0.5*(a*c));
}
else
{
return (0.5*(a*b));
}
}
}
private:
Side *sides;
friend ostream & operator << (ostream &out, const RightTriangle &rt); //use friend keyword to get access to the private members
};
//declaration of output stream operator
ostream & operator << (ostream &out, const RightTriangle &rt)
{
for(int i =0; i < 3; i++)
{
out << "length side " ;
out << rt.sides[i].displayLength(); // using the display function we created in the Side class.
if(i <2)
out <<", ";
}
return out;
}
int main() //added extra for testing code
{
RightTriangle rt(3,4,5); //creating first object
RightTriangle rt2(rt); // checking the copy constructor
cout << rt2.computeArea()<<endl; // checking the computeArea function
cout << rt2 << endl; // checking the output stream operator.
}
Output:
./a.out
6
lenght side 1.181102 inches, lenght side 1.574803 inches, lenght side 1.968504 inches