asked 85.2k views
5 votes
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

1 Answer

1 vote

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;

}

Step-by-step 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

answered
User Chitwarnold
by
7.7k points