Design and Use Triangle Class – C++ Program

By | July 9, 2019

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.

Design and Use Triangle Class - C Plus Plus Program

Design and Use Triangle Class – C++ Program

Source code 

/* 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

*/
#include<iostream>

using namespace std;

// define a class Temperature

class Triangle
{
	private:
	float base, height;
	public:
	void setBase(float b)
	{
		base = b;
	}
	void setHeight(float h)
	{
		height = h;
	}
	float area()
	{
		float a;
		a= 1.0 / 2.0 * base * height;
		return a;
	}
	
};
int main()
{
	// define an object of Triangle class
	Triangle triangle1;
	float b,h;
	cout<<"Enter base and height of triangle=";
	cin>>b>>h;
	// set value of base
	triangle1.setBase(b);
	// set value of height
	triangle1.setHeight(h);
	// call area() function with object triangle1
	cout<<"Area of Triangle="<<triangle1.area();

return 0;
}

Compute area of Triangle Input Three Sides

Java Program Area of Triangle by Three Sides

Leave a Reply

Your email address will not be published. Required fields are marked *