Answer to Question #291150 in C++ for satu

Question #291150

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.



1
Expert's answer
2022-01-27T12:35:12-0500
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog