Advertisements
Advertisements
प्रश्न
Write an OOP in C++ to find area of triangle.
(hint = a = 1/2 * b * h)
कोड लेखन
Advertisements
उत्तर
#include <iostream>
using namespace std;
class Triangle
{
float base, height;
public:
// Function to input values
void getData()
{
cout << "Enter base: ";
cin >> base;
cout << "Enter height: ";
cin >> height;
}
// Function to calculate area
float area()
{
return 0.5 * base * height;
}
};
int main()
{
Triangle t; // Creating object
t.getData();
cout << "Area of triangle is: " << t.area();
return 0;
}
Explanation
Class Triangle contains:
- Data members:
baseandheight - Member function
getData()to accept input - Member function
area()to calculate area using the formula:
`Area = 1/2 xx "base" xx "height"`
Example:
Input:
Base = 10
Height = 5
Output:
Area of triangle is: 25
shaalaa.com
या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
