Task: Design and Use Triangle Class – C++ Program
Detail of C++ Program for Design and Use Triangle Class
Write a C++ Program to design a class Triangle with two float variables ‘base’ and ‘height’ and three member functions
1. void setBase(float b) to set value of base
2. void setHeight(float h) to set value of base
3. float area() to calculate area of triangle
with formula area = 1/2 x base x height.
How this Program works?
- We will define a class Triangle using class keyword.
- Two member variables are base and height of triangle.
- There are two member functions: setBase() and setHeight() to set or change the values of member variables.
- There is a member function to calculate area of triangle, called area() that will return area of the triangle.
- The main function has statements to get two values from the user for base and height.
- There are statements to create an object triangle1 of Triangle class.
- We will set base and height by calling the corresponding memebr functions.
- Then we call the function area() that will calculate and return the area of triangle1 object with given base and height.
- The area is printed using cout.
Enter base and height of triangle=4.4
Area of Triangle=14.3
You may also like:
8,057 total views, 19 views today