Write a program to create a class Circle. Perform the following operations on it.
a. Define the attribute radius.
b. Define the constructor with one argument containing radius.
c. Define the method named get_radius() which returns the radius of the circle.
d. Define the method named calc_area() which returns the area of the circle.
Answers
Answered by
1
Explanation:
Python
Object Oriented Programming (OOP)
Answered by
0
Answer:
#include <iostream>
using namespace std;
class Circle{
private:
double radius;
public:
Circle(double r = 5.1){
radius = r;
}
double get_radius() {
return radius;
}
double calc_area() {
return radius*radius*3.14;
}
};
int main() {
Circle c1(2.5);
cout << "Radius=" << c1.get_radius()<<endl;
cout << "Area=" << c1.calc_area();
return 0;
}
Explanation:
- A class Circle is created having radius as the private variable.
- The constructor which has the same name as the class name is declared as a public variable.
- The constructor takes the radius as the argument.
- The get_radius() method returns the entered value of the radius. If no value is specified, the default value, i.e., 5.1 is returned.
- The method calc_area() calculated the area and returns the value.
- In the main function, an instance of circle class is created with radius = 2.5 as the argument.
- The get_radius() method is called and the value of the radius is printed.
- The calc_area() method is called and the value of the area is printed.
#SPJ3
Similar questions