Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

C++ Circle class that asks the user for the circle’s radius creating a Circle object and then reporting the circle’s area- diameter and circumference


 

Download  file 

Not a member!
Create a FREE account here to get access and download this file 


 

Circle Class
Write a C++ Circle class that has the following member variables:
• radius: a double
• pi: a double initialized with the value 3.14159
The class should have the following member functions:
• Default Constructor. A default constructor that sets radius to 0,0,
• Constructor. Accepts the radius of the circle as an argument.
• setRadi us. A mutator function for the radius variable.
• get Radi us. An accessor function for the radius va riable.
• ge t Area. Returns the area of the circle. which is calculated as
area = pi * radius * radius
• g etDiatQoter. Returns the diameter of the circle, which is calculated as
diameter = radius * 2
• getCircumference. Returns the circumference of the circle, which is ca lculated as
circumference = 2 * pi * radius
Write a program that demonstrates the Circle class by asking the user for the circle’s
radius, creating a Circle object) and then reporting the circle’s area, diameter, and
circumference.

Answer:


//  Circle Class
#include <iostream>
using namespace std;

// Circle class declaration
class Circle
{
private:
   double pi;     // To hold a value for pi
   double radius; // To hold the radius
   
public:
   // The default constructor sets
   // radius to 0.0 and pi to 3.14159.
   Circle()
      { radius = 0.0; pi = 3.14159; }
   
   // The overloaded constructor accepts
   // the radius as an arguemnt.
   Circle(double r)
      { radius = r; pi = 3.14159; }
      
   // Mutator function for the radius
   void setRadius(double r)
      { radius = r; }
   
   // Accessor function for the radius
   double getRadius() const
      { return radius; }
      
   // The getArea function returns the
   // circle's area.
   double getArea() const
      { return pi * radius * radius; }

   // The getDiameter function returns the
   // circle's diameter.
   double getDiameter() const
      { return radius * 2; }

   // The getCircumference function returns
   // the circle's circumference.
   double getCircumference() const
      { return 2 * pi * radius; }
};

// Demo program
int main()
{
   double radius;  // To hold a radius
   
   // Get the radius.
   cout << "Enter the circle's radius: "; cin >> radius;
   
   // Create a Circle object with the 
   // specified radius.
   Circle c(radius);
   
   // Display the circle's data.
   cout << "Radius: " << c.getRadius()
        << endl;
   cout << "Area : " << c.getArea()
        << endl;
   cout << "Diameter: " << c.getDiameter()
        << endl;
   cout << "Circumference: " << c.getCircumference()
        << endl;
   return 0;
}




About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!