Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem: Write a Circle class in Circle.h that has the following member variable

ID: 3879569 • Letter: P

Question

Problem: Write a Circle class in Circle.h 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 implemented in Circle.cpp Default constructor- a default constructor that set radius to o.o * Constructor accepts the radius of the circle as an argument setadius - a mutator function of the circle as an argument getRadius- an accessor function for the radius variable getArea - returns the area of the circle, which is calculated as area-pi radius radius Write a program that demonstrates the Circle class by asking the user for the circle radius and creating a Circle object. Print the radius. Then change the radius to a different value and print it again. Print the area of the circle Don't forget to add the appropriate headers and comments

Explanation / Answer

//circle.h

#ifndef CIRCLE_h
#define CIRCLE_h
class Circle
{
   private://declaring private variables
       double radius,pi;
   public:
       //constructors
       Circle()//default
       {
           radius=0;
           pi=3.14159;
          
       }
       Circle(double r)//takes input a radius
       {
           radius =r;
           pi=3.14159;  
          
       }
       void setRadius(double r)//method to set radius
       {
           radius =r;
       }
       //method to return radius
       double getRadius()
       {
           return radius;  
       }
       //method to calculate and return area
       double getArea()
       {
           return (pi*radius*radius);  
       }
  
};

#endif

//main.cpp

#include<iostream>
#include "circle.h"
using namespace std;
int main()
{
   Circle *c1=new Circle();
  
   double d;
   cout<<"Enter radius for circle1:";
   cin>>d;
   c1->setRadius(d);
   cout<<"Enter radius for circle2:";
   cin>>d;
   Circle *c2=new Circle(d);
   cout<<"The area of circle with radius "<<c1->getRadius()<<" is:"<<c1->getArea()<<endl;
  
   cout<<"The area of circle with radius "<<c2->getRadius()<<" is:"<<c2->getArea()<<endl;
   return 0;
}

output:

Enter radius for circle1:2
Enter radius for circle2:3
The area of circle with radius 2 is:12.5664
The area of circle with radius 3 is:28.2743


Process exited normally.
Press any key to continue . . .

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote