Problem: Write a Circle class in Circle.h that has the following member variable
ID: 3879647 • 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 commentsExplanation / 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();//creatiing object
double d;
cout<<"Enter radius for circle1:";
cin>>d;//readin radius
c1->setRadius(d);//setting radius of c1
cout<<"Enter radius for circle2:";
cin>>d;//readin radius
Circle *c2=new Circle(d);//creating object with radius d
//printing output
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 . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.