In: Computer Science
Note- can you please rewrite the code in C++
Write a class declaration named Circle with a private member variable named radius. Write set and get functions to access the radius variable, and a function named getArea that returns the area of the circle. The area is calculated as
3.14159 * radius * radius
The code in C++ is given below along with the screenshot of the output.
#include <iostream>
using namespace std;
class Circle
{
private:
//private attribute (variable)
float radius;
public:
float area;
//setter
void setRadius(int r)
{
radius = r;
}
//getter
float getRadius()
{
return radius;
}
float getArea() //function that returns area of circle
{
area = 3.14159*getRadius()*getRadius();//formula to calculate area
return area;
}
};
int main()
{
Circle var;
var.setRadius(5); //set the value of radius
std::cout << "\nRadius = " << var.getRadius();
std::cout << "\nArea = "<<var.getArea();
return 0;
}
OUTPUT:
*NOTE: Drop a comment for queries.