In: Computer Science
Computer Science
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. The 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 must be written in C++ thanks.
#include <iostream>
#include <cmath>
using namespace std;
class triangle{
private:
int sideA; //height
int sideB; //base
int sideC; //hypo
public:
triangle(){ //default constructor
sideA=0;
sideB=0;
sideC=0;
}
triangle(int A, int B, int C){ //parameterize
sideA=A;
sideB=B;
sideC=C;
}
//mutator
void setSideA(int A){
sideA=A;
}
void setSideB(int B){
sideB=B;
}
void setSideC(int C){
sideC=C;
}
//accessors
int getSideA(){
return sideA;
}
int getSideB(){
return sideB;
}
int getSideC(){
return sideC;
}
//get type of triangle
void getTriangleType(){
int sideAA,sideBB,sideCC;
sideAA=sideA*sideA;
sideBB=sideB*sideB;
sideCC=sideC*sideC;
if(sideA==sideB && sideB==sideC &&
sideC==sideA){
cout<<"Triangle is Equilateral "<<endl;
}
else if(sideA==sideB || sideB==sideC || sideC==sideA){
cout<<"Triangle is Isosceles "<<endl;
}
else if(sideAA==sideBB+sideCC || sideBB==sideCC +sideAA || sideCC==
sideAA+sideBB){
cout<<"Triangle is Right Angled "<<endl;
}
else{
cout<<"Triangle is either of type "<<endl;
}
}
float getArea(){
float S;
float area;
S= (sideA+sideB+sideC)/2; //perimeter
area= sqrt(S*(S-sideA)* (S-sideB)* (S-sideC)); //area
return area;
}
};
int main()
{
triangle t1(25,25,9);
triangle t2(3,4,5);
triangle t3(2,2,2);
cout<<"Triangle T1 Area:
"<<t1.getArea()<<endl;
cout<<"Triangle T2 Area:
"<<t2.getArea()<<endl;
cout<<"Triangle T3 Area:
"<<t3.getArea()<<endl;
cout<<endl;
t1.getTriangleType();
t2.getTriangleType();
t3.getTriangleType();
return 0;
}
OUTPUT:
IF YOU HAVE ANY QUERY PLEASE COMMENT DOWN BELOW
PLEASE GIVE A THUMBS UP