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
Circlehas: - A data member
radiusof typefloat. - 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
या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
