I have no idea where to start or what to do for this program. Any help would be
ID: 3569380 • Letter: I
Question
I have no idea where to start or what to do for this program. Any help would be greatly appreciated.
Write a complete program (including all necessary include directives) that implements
the following. (25 points)
a) Write the declaration and definition for a class called Triangle.
A Triangle has the following attributes (member variables):
? base (double)
? height (double)
? area (double)
A Triangle has the following methods (member functions):
No member function may use any cin or cout statements unless otherwise specified.
? setBase (this function takes in a double and sets the base of the triangle)
? setHeight (this function takes in a double and sets the height of the triangle)
? calculateArea(this function does not take any arguments but just uses base
and height member variables to calculate the area using the following formula
area of triangle =
Explanation / Answer
#include<iostream>
using namespace std;
class Triangle
{
private:
double base;
double height;
double area;
friend bool isIsosceles(Triangle t);
public:
Triangle()
{
base=0;
height=0;
area=0;
}
Triangle(double b, double h)
{
base=b;
height = h;
}
void setBase(double b)
{
base = b;
}
void setHeight(double h)
{
height = h;
}
void calculateArea()
{
area = base*height*(0.5);
}
double getBase()
{
return base;
}
double getHeight()
{
return height;
}
double getArea()
{
return area;
}
};
bool isIsosceles(Triangle t)
{
bool val = (t.base == t.height);
return val;
}
int main()
{
double base = 10;
double height = 10;
Triangle t = Triangle(base, height);
cout<<"Triangle is created: ";
cout<<"Height = "<<t.getHeight()<<" ";
cout<<"Base = "<<t.getBase()<<" ";
t.calculateArea();
cout<<"Area = "<<t.getArea()<<" ";
if(isIsosceles(t))
cout<<"The triangle is isosceles !! ";
else
cout<<"The triangle is NOT isosceles !! ";
return 0;
}
--------------------------------------------------------
OUTPUT
Triangle is created:
Height = 10
Base = 10
Area = 50
The triangle is isosceles !!