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

Implement a circle class. Each object of this class will represent a circle accepting its radius value as of float. Evaluate an area() function which will calculate the area of circle. - Computer Science 1

Advertisements
Advertisements

प्रश्न

Implement a circle class. Each object of this class will represent a circle accepting its radius value as of float. Evaluate an area() function which will calculate the area of circle.

कोड लेखन
दीर्घउत्तर
Advertisements

उत्तर

#include <iostream>
using namespace std;
class Circle
{
    float radius;

public:
    // Constructor to initialize radius
    Circle(float r)
    {
        radius = r;
    }

    // Function to calculate area
    float area()
    {
        return 3.1416 * radius * radius;
    }
};

int main()
{
    float r;

    cout << "Enter radius of circle: ";
    cin >> r;

    Circle c(r);   // Creating object

    cout << "Area of circle is: " << c.area();

    return 0;
}

Explanation:

  • The class Circle has:
  • A data member radius of type float.
  • A constructor to accept radius value.
  • A member function area() that calculates area using the formula:

Area = πr2

Example:

Input: → 5

Output: → Area of circle is: 78.54

shaalaa.com
  या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
2024-2025 (July) Official Board Paper
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×