In a header file named circle.h, (a) Define a class Circle that has two member v
ID: 3821300 • Letter: I
Question
In a header file named circle.h, (a) Define a class Circle that has two member variables radius and pi (both of type double). The class should have the following member functions: setRadius: function to set the radius getRadius: value-returning function to return the radius getArea: value-returning function to return the area of the circle, which is calculated using the formula: area = pi * radius * radius getDiameter: value-returning function to return the diameter of the circle, which is calculated using the formula: diameter = radius * 2 getCircumference: value-returning function to return the circumference of the circle, which is calculated using the formula: circumference = 2* pi * radius constructor with default parameters: The default value of radius is 0, and the default value of pi is 3.14159 Write the definition of the member functions of the class Circle as described above (in the header file). (b) Write a driver program (named circle.cpp) that demonstrates the Circle class by (1) creating one instance/object of the Circle class, (2) asking the user for the circle's radius, and then (3) displaying the circle's area, diameter, and circumference.Explanation / Answer
I am providing the code below. You have mentioned that I need to define all member functions in header file itself. I have done the same , but I need to mention that it is bad coding practice. Header file must contain the prototype of the function. definations must be there in cpp file. your code is as below: -
circle.h
#ifndef CIRCLE_H
#define CIRCLE_H
#include <iostream>
using namespace std;
class circle
{
private:
double radius;
double pi;
public:
void setRadius(double radius){
this->radius = radius;
};
double getRadius(){
return this->radius;
};
double getArea(){
return this->pi*this->radius*this->radius;
};
double getDiameter(){
return 2*this->radius;
};
double getCircumference(){
return 2*this->pi*this->radius;
};
circle(){ // default constructor
this->radius = 0;
this->pi = 3.14159;
};
};
#endif
circle.cpp
#include "circle.h"
#include <iostream>
using namespace std;
int main() {
circle cir;
double rad;
cout<<"Enter the radius of the circle:";
cin>>rad;//taking input from the user
cout<<endl;
cir.setRadius(rad);
cout<<"Area of circle:"<<cir.getArea()<<endl;//printing area
cout<<"Diameter of circle:"<<cir.getDiameter()<<endl;//printing diameter
cout<<"Circumference of circle:"<<cir.getCircumference()<<endl;//printing circumference
return 0;
}
sample outputs
Enter the radius of the circle:5
Area of circle:78.5397
Diameter of circle:10
Circumference of circle:31.4159
--------------------------------
Process exited after 3.803 seconds with return value 0
Press any key to continue . . .
Enter the radius of the circle:45
Area of circle:6361.72
Diameter of circle:90
Circumference of circle:282.743
--------------------------------
Process exited after 2.811 seconds with return value 0
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.