मराठी
महाराष्ट्र राज्य शिक्षण मंडळएचएससी विज्ञान (संगणक विज्ञान) इयत्ता १२ वी

Write an OOP in C++ to find area of triangle. (hint = a = 1/2 * b * h) - Computer Science 1

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: base and height
  • 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
  या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
2024-2025 (July) Official Board Paper
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×