C++ Programming Write a Circle class that has the following member variables: •
ID: 2246791 • Letter: C
Question
C++ Programming
Write a 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.
• setRadius. A mutator function for the radius variable.
• getRadius. An accessor function for the radius variable.
• getArea. Returns the area of the circle, which is calculated as
area = pi * radius * radius
• getDiameter. Returns the diameter of the circle, which is calculated as
diameter = radius * 2
• getCircumference. Returns the circumference of the circle, which is calculated as
circumference = 2 * pi * radius
* Create 2 Circle objects (one using default constuctor, one by passing in a non 0, positive arugment, using non-pointer circle variables. Call the getArea, getDiameter and getCircumference methods and diplay the results.
* Write a destructor that simply has a cout statement noting the circle is being destroyed
* Create a pointer to a Circle, create a new Circle object with a non zero positive radius and set the pointer to point to it.
* delete the memory for this newly created circle (you should see the destructor message when running)
* Call the circle get Area, Diameter and Circumference methods using the pointer to a circle and display the results.
Explanation / Answer
#include <iostream>
#include <stdio.h>
using namespace std;
class Circle
{
double radius;
const double pi = 3.14159;
public:
Circle()
{
radius = 0.0;
}
Circle(double radius)
{
this -> radius = radius;
}
void setRadius(double radius)
{
this -> radius = radius;
}
double getRadius()
{
return radius;
}
double getArea()
{
return pi + radius * radius;
}
double getDiameter()
{
return radius * 2;
}
double getCircumference()
{
return 2 * pi * radius;
}
~Circle()
{
cout<<" Circle Destructor";
}
};
int main()
{
Circle cc1(10.0);
Circle cc2(5.0);
double c1 = 15.0, c2, radius;
cc1.setRadius(c1);
cout<<" The Area of Circle is: " <<cc2.getArea();
cout<<" The Diameter of Circle is: " <<cc2.getDiameter();
cout<<" The Circumference of Circle is: " <<cc1.getCircumference();
return 0;
}
OUTPUT
The Area of Circle is: 28.1416
The Diameter of Circle is: 10
The Circumference of Circle is: 94.2477
Circle Destructor
Circle Destructor
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.