Write a program which will do the following:
Create a class called
Triangle
. The three sides of the triangle
are private
members of the class. The
class should contain a member f
unction to determine whether a
triangle is equilateral, isosceles or
scalene.
The class
should
also
include member functions which will give area and perimeter of the
triangle. The side
s of the triangle should be taken as input from the user in the main function.
#include <iostream>
#include<math.h>
using namespace std;
class Triangle {
private:
double a, b, c;
int triangleShape(){
if(a<=0 || b<=0 || c<=0){
return 1;//noTriangle
}
//If all sides are equal
if(a==b && b==c){
return 2;//equilateral
}
//If any two sides are equal
if(a==b || a==c || b==c){
return 3;//isosceles
}
//If none sides are equal
return 4;//scalene
}
public:
Triangle(double a,double b,double c){
this->a=a;
this->b=b;
this->c=c;
}
void display(){
cout <<"a="<<a<<"\nb="<<b<<"\nc="<<c<<"\n";
if(triangleShape()==4){
cout <<("Triangle is scalene\n\n");
}
if(triangleShape()==3){
cout <<("Triangle is isosceles\n\n");
}
if(triangleShape()==2){
cout <<("Triangle is equilateral\n\n");
}
if(triangleShape()==1){
cout <<("It is not a triangle\n\n");
}
}
double area(){
double s = (a+b+c)/2;
return sqrt(s*(s-a)*(s-b)*(s-c));
}
double perimeter(){
return a+b+c;
}
};
//The start point of the program
int main(){
double a, b, c;
cout<<"Ente a: ";
cin>>a;
cout<<"Ente b: ";
cin>>b;
cout<<"Ente c: ";
cin>>c;
if(a+b<=c){
cout<<"\nERROR - SIDES "<<a<<", "<<b<<", "<<c<<" ARE INVALID FOR A TRIANGLE\n";
}
Triangle triangle(a,b,c);
triangle.display();
cout<<"Area: "<<triangle.area()<<"\n";
cout<<"Perimeter: "<<triangle.perimeter()<<"\n";
system("pause");
return 0;
}
Comments
Leave a comment