Given class Triangle (in files Triangle.h and Triangle.cpp), complete main() to read and set the base and height of triangle1 and of triangle2, determine which triangle's area is larger, and output that triangle's info, making use of Triangle's relevant member functions.
Ex: If the input is:
3.0 4.0
4.0 5.0
where 3.0 is triangle1's base, 4.0 is triangle1's height, 4.0 is triangle2's base, and 5.0 is triangle2's height, the output is:
Triangle with larger area:
Base: 4.00
Height: 5.00
Area: 10.00
main.cpp
#include <iostream>
#include "Triangle.h"
using namespace std;
int main()
{
Triangle Tri1;
Triangle Tri2;
double base1, height1, base2, height2;
cout << "Enter a base for your Triangle1: ";
cin >> base1;
cout << "Enter a height for your Triangle1: ";
cin >> height1;
cout << endl;
cout << "Enter a base for your Triangle2: ";
cin >> base2;
cout << "Enter a height for your Triangle2: ";
cin >> height2;
cout << endl;
cout << "################################" << endl;
cout << "Triangle with larger area:" << endl;
if ((0.5)*base1*height1 > (0.5)*base2*height2){
Tri1.setValues(base1, height1);
Tri1.getValues();
cout << "Area: " << Tri1.getArea() << endl << endl;
}
else{
Tri2.setValues(base2, height2);
Tri2.getValues();
cout << "Area: " << Tri2.getArea() << endl;
}
return 0;
}
Triangle.cpp
#include "Triangle.h"
using namespace std;
void Triangle::setValues(double a, double b)
{
base = a;
height = b;
}
void Triangle::getValues()
{
cout << "Base: " << base << endl;
cout << "Height: " << height << endl;
}
double Triangle::getArea()
{
return (0.5)*base*height;
}
Triangle.h
#include <iostream>
class Triangle
{
private:
double base;
double height;
public:
void setValues(double a, double b);
void getValues();
double getArea();
};
Comments
Leave a comment