Write a complete C++ program in one file here that asks the user to enter a radius for a circle then calculates and prints its' area. The area should be calculated using the formula area = 3.14 * radius * radius. You must have a class called Circle that stores the radius as a double value and can calculate its' own area via a method called calcArea that takes no parameters and returns the area

Respuesta :

Answer:

#include <iostream>

using namespace std;

class Circle{

 public:    

   double radius;

 

 Circle(double r) {

     radius = r;

 }

 

 double calcArea() {

     return 3.14 * radius * radius;

 }

};

int main()

{

   

   double radius;

   cout<<"Enter the radius: ";

   cin >> radius;

   

   Circle ob = Circle(radius);

   cout << ob.calcArea() << endl;

   

   return 0;

}

Explanation:

Create a class called Circle that has one variable, radius

Create a constructor that takes one parameter, radius

Create a method called calcArea that calculates and returns the area of a circle using given formula

In the main:

Ask the user for the radius

Create an object for Circle with given radius

Call calcArea method to calculate the area, and print the result