Write a Circle class that has the followingmember variables: • radius : a double
ID: 3610371 • Letter: W
Question
Write a Circle class that has the followingmember variables:
• radius: a double
• pi: a double initialized with the value3.14159
The class should have the following member functions:
• Default Constructor. A defaultconstructor that sets radius to 0.0.
• Constructor. Accepts the radius of thecircle as an argument.
• setRadius. A mutator function for theradius variable.
• getRadius. An accessor function for theradius variable.
• getArea. Returns the area of thecircle, which is calculated as
area = pi * radius * radius
• getDiameter. Returns the diameter ofthe circle, which is calculated as
diameter = radius * 2
• getCircumference. Returns thecircumference of the circle, which is calculated as
circumference = 2 * pi * radius
Write a program that demonstrates the Circleclass by asking the user for the circle’s radius, creating aCircle object, and then reporting thecircle’s area, diameter, and circumference.
Explanation / Answer
#include<iostream.h> #include<conio.h> double pi=3.14159;class Circle { private: double Radius; public: Circle()//defualt constructor { Radius=0.0; } Circle(double radius)//defualtconstructor { Radius=radius; } void setRadius(double radius) { Radius=radius; } double getRadius() { return Radius; } double getArea (){ returnpi*Radius*Radius; } double getDiameter(){ return Radius*2; } double getCircumference() { return 2*pi*Radius; } };
main() { double radius; cout<<"Enter the Radius ofCircle: "; cin>>radius; cout<<"---------------------------------"; Circle obj(radius);//creating object; cout<<" Area ofCircle: "<<obj.getArea(); cout<<" Diameter ofCircle: "<<obj.getDiameter(); cout<<" Circumference ofCircle: "<<obj.getCircumference(); getch(); }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.