Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Let us consider a class Triangle with the following data members: base (float) a

ID: 3578163 • Letter: L

Question

Let us consider a class Triangle with the following data members: base (float) and height
(float). The class has a function to calculate the area of the triangle, in addition to the
setters (to set the area value) and getters functions (to get the set area).


Implement the Triangle class (header and implementation file)


Write a main program that creates 2 objects of type Triangle, assigns values to base,
and height of each object using inputs given by the user, then displays the larger of the
two areas.


Restrictions:
All variables in the class Triangle must be private.
All functions in the class Triangle must be public.
Must use gets/sets

Deliverable:
- Source code of the class, header and main program (specify each one of them)
- 1 Screenshot

Explanation / Answer

#include <iostream>

using namespace std;
class Triangle {
private:
float base;
float height;
float area;
public:
Triangle(){
  
}
void setHeight(float h){
height = h;
}
float getHeight(){
return height;
}
void setBase(float b){
base = b;
}
float getBase(){
return base;
}
void setArea(float a){
area = a;
}
float getArea(){
return area;
}
void calcArea(){
area = (height * base)/2;

}
};
int main()
{
Triangle t;
float base, height;
cout << "Enter base: ";
cin >> base;
cout<<"Enter height: ";
cin >> height;
t.setHeight(height);
t.setBase(base);
t.calcArea();
cout<<"Area is "<<t.getArea()<<endl;
return 0;
}

Output:

sh-4.2$ g++ -o main *.cpp                                                                                                                                                                                                                              

sh-4.2$ main                                                                                                                                                                                                                                           

Enter base: 6                                                                                                                                                                                                                                          

Enter height: 6                                                                                                                                                                                                                                        

Area is 18