Hi! I have an assignment about classes and when I compile file I got the wrong answer. Can't find where mistake is but feel like it is in the header part. When I put different values the area shows as 0, and everything else is some crazy numbers. Thank you for help!!
Assignment:
Circle Class
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
Write a program to test your Circle class by asking the user for the circle’s radius, creating a Circle object, and then reporting the circle’s area, diameter, and circumference.
Use inline function definitions only. You will only turn in the .h file with the class implementation and the main.cpp.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
//////////////////////////////////////////Circle.h
#ifndef CIRCLE_H
#define CIRCLE_H
class Circle
{
private:
double radius;
static const double PI = 3.14159;
public:
Circle() {radius = 0.0;}
Circle (double r);
virtual ~Circle();
double getRadius() const { return radius;}
void setRadius( double r);
double getArea() const {return PI*radius*radius; }
double getDiameter() const {return radius*2; }
double getCircumference() const {return 2*PI*radius; }
};
#endif // CIRCLE_H
///////////////////////////////////////////////////////////Circle.cpp
#include "Circle.h"
Circle::Circle(double r)
{
setRadius(r);
}
Circle::~Circle()
{
}
void Circle::setRadius( double r)
{
if (r < 0)
r = 0.0;
}
/////////////////////////////////////////////////////main.cpp
#include <iostream>
#include "Circle.h"
using namespace std;
int main()
{
double r;
cout << "Enter the radius of the circle: ";
cin >> r;
Circle object(r);
cout << "\n******************************************\n\n";
cout << "The area is: " << object.getArea() << endl;
cout << "The diameter is: " << object.getDiameter() << endl;
cout << "The circumference is: " << object.getCircumference() << endl;
cout << "\n******************************************\n";
return 0;
}
|