In: Computer Science
In C++, Design and implement an ADT that represents a triangle. The data for the ADT should include the three sides of the triangle but could also include the triangle’s three angles. This data should be in the private section of the class that implements the ADT. Include at least two initialization operations: one that provides default values for the ADT’s data, and another that sets this data to client-supplied values. These operations are the class’s constructors. The ADT also should include operations that look at the values of the ADT’s data; change the values of the ADT’s data; compute the triangle’s area; and determine whether the triangle is a right triangle, an equilateral triangle, or an isosceles triangle.
Code
triangle.h
#ifndef TRIANGLE_H
#define TRIANGLE_H
/** Header file for the class Triangle. */
#include <cmath>
#include <iostream>
using namespace std;
template<class T>
class Triangle
{
private:
T side1,side2,side3;
T angle1,angle2,angle3;
public:
Triangle();
Triangle(T, T, T, T, T, T);
virtual void setSides(T newSide1, T newSide2, T newSide3,T ang1, T
ang2, T ang3);
virtual T getArea() const;
virtual T getPerimeter() const;
virtual bool isRightTriangle() const;
virtual bool isEquilateral() const;
virtual bool isIsosceles() const;
};
template<class T>
Triangle<T>::Triangle()
{
}
template<class T>
Triangle<T>::Triangle(T side1, T side2, T side3,T ang1, T
ang2, T ang3)
{
Triangle<T>::setSides(side1, side2,
side3,ang1,ang2,ang3);
}
template<class T>
void Triangle<T>::setSides(T newSide1, T newSide2, T
newSide3,T ang1, T ang2, T ang3)
{
side1 = newSide1;
side2 = newSide2;
side3 = newSide3;
angle1=ang1;
angle2=ang2;
angle3=ang3;
}
template<class T>
T Triangle<T>::getArea() const
{
double p = Triangle<T>::getPerimeter() / 2;
T area = sqrt(p*(p-side1)*(p-side2)*(p-side2));
return area;
}
template<class T>
T Triangle<T>::getPerimeter() const
{
T perimeter = side1+side2+side3;
return perimeter;
}
template<class T>
bool Triangle<T>::isRightTriangle() const
{
if(side1*side1 == (side2*side2) + (side2*side2))
return true;
if(side3*side3 == (side1*side1) + (side2*side2))
return true;
if(side2*side2 == (side1*side1) + (side3*side3))
return true;
else
return false;
}
template<class T>
bool Triangle<T>::isEquilateral() const
{
if(side1 == side2 && side3 == side2)
return true;
else
return false;
}
template<class T>
bool Triangle<T>::isIsosceles() const
{
if(side1 == side2 || side2 == side3 || side1 == side3)
return true;
else
return false;
}
#endif
main.cpp
#include <iostream>
#include "triangle.h"
using namespace std;
int main() {
Triangle<float> t1;
Triangle<float> t2(4.0, 5.0, 3.0,90,45,45);
Triangle<int> t3(2, 3, 400,60,45,75);
cout<<"Area of trinable is
:"<<t2.getArea()<<endl;
if(t2.isEquilateral())
cout<<"Its
Equilateral"<<endl;
if(t2.isIsosceles())
cout<<"Its
Isosceles"<<endl;
if(t2.isRightTriangle())
cout<<"Its Right
angle"<<endl;
return 0;
}
ouput
If you have any query regarding the code please ask me in the
comment i am here for help you. Please do not direct thumbs down
just ask if you have any query. And if you like my work then please
appreciates with up vote. Thank You.